Setting type of environment for an ironworker

306 views Asked by At

Looking into using IronWorker to run some common services, however I'm wondering if there is a way to know in the worker itself what environment it is running under. For example, in our main code base we set a server var ENV to equal one of 'development', 'staging', or 'production'. Based on this variable, our app knows which config files to load (via config files in dirs config/development, config/staging, and config/production). I would like to have something similar setup for a worker.

I've noticed that when I upload a worker via the cli, I can do the following:

iron_worker upload workers/example/hello_world --env staging

Which allows me to have one iron.json config file with dev, staging, and production keys in it. e.g.,

{
  "production": {
    "token": "AAAAAAAAAAAAAAAAAAAAAAAAAAA",
    "project_id": "000000000000000000000001"
  },
  "staging": {
    "token": "BBBBBBBBBBBBBBBBBBBBBBBBBB",
    "project_id": "000000000000000000000002"
  },
  "development": {
    "token": "CCCCCCCCCCCCCCCCCCCCCCCCCC",
    "project_id": "000000000000000000000003"
  },
  "test": {
    "token": "DDDDDDDDDDDDDDDDDDDDDDDDDD",
    "project_id": "000000000000000000000004"
  }
}

However, in the worker itself I don't have access to the env var from the cli so when the worker needs to load in some config vars it doesn't know which dir to look in.

In the documentation here http://dev.iron.io/worker/reference/dotworker/ they show that you can set an environment variable in the .worker file, but this means I would need to have a separate worker file for each environment and (maybe?) swap them out depending on which environment I am deploying to.

1

There are 1 answers

0
thousandsofthem On BEST ANSWER

There are some tricky way:

1) iron.json. Same format as described, no changes

2) .worker file. Pass environment through, from local system to IronWorker environment. Add this line to .worker file:

set_env 'IRON_ENV', ENV['IRON_ENV']

3) Upload code command. Instead of

iron_worker upload workers/example/hello_world --env staging

Use different syntax:

IRON_ENV=staging iron_worker upload workers/example/hello_world

4) Check environment on Worker side (any language-of-choice supported of course):

puts ENV['IRON_ENV']