I have a Rails application that is using IronWorker and I need to connect to my database from the worker. How do I do that?
Asked
Active
Viewed 1,073 times
2 Answers
7
The worker needs to make a connection to the database explicitly since it is not running within your application so you need to pass the connection information to your worker. You can do this in the worker payload like so:
client = IronWorkerNG::Client.new
task = client.tasks.create('MyWorker', 'database' => Rails.configuration.database_configuration[Rails.env])
Then inside your worker:
ActiveRecord::Base.establish_connection(params['database'])

Travis Reeder
- 38,611
- 12
- 87
- 87
-
Would you happen to know how I could test such a worker (one that connects to a database) in a development environment? – Gautam Aug 18 '13 at 03:47
-
You can try the `iron_worker run my_worker` command from the command line to test it out, but I generally recommend using the actual service as then you are sure it's the exact same environment. http://12factor.net/dev-prod-parity – Travis Reeder Aug 29 '13 at 16:47
2
I whipped up a blog post on this. Hopefully it helps!
In a nut shell though, storing your database configurations in environment variables makes it easy.

Chiedo
- 7,288
- 3
- 26
- 22
-
Definitely! What part didn't make sense? That will help me answer more effectively. Ultimately, you want to pass your database credentials to Iron Worker though which will allow Iron Worker to connect to your database using the credentials you pass. – Chiedo Jun 17 '15 at 18:57
-
Thanks for your input! ...but I'm sure people who understand iron.io and the question will find the post useful. If someone who is working rails and iron.io doesn't find it useful and lets me know, that's a different story – Chiedo Jun 18 '15 at 20:34