Detect type of vapor worker at runtime

45 Views Asked by At

We have a Vapor API which uses a web worker, a queues worker and a scheduled queues worker. Every worker has the same configure(_ app: Application) function.

In this function, I'd like to find the type of the worker, rather it's the web or on of the queues worker.

// configures the worker
public func configure(_ app: Application) throws {
    // clear every middleware
    app.middleware = .init()

    // HERE I want to differentiate the worker
    if web worker {
        // do that
    } else {
        // do something other
    }
}

Is there a way or a method in the Application that tells me the type of it?

Our main problem is, that we want to try app.autoMigrate().wait(), but only on the web worker.

Currently all workers try to migrate the database and two of it fail, obviously.

1

There are 1 best solutions below

1
Nick On BEST ANSWER

You can use environment variables to do this. Use something like this in your start-up:

WEBWORKER=1 /home/vapor/myapp/.build/release/Run serve --env production

Only run the migrations if the instance has the correct variable/value by putting this:

if let worker = Environment.get("WEBWORKER") {
    try app.autoMigrate().wait()
}

See here for all the information about environment variables.