How do I connect to a database in IronWorker using ActiveRecord?
Asked Answered
E

2

5

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?

Esbenshade answered 15/6, 2012 at 18:26 Comment(0)
E
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'])
Esbenshade answered 15/6, 2012 at 18:26 Comment(2)
Would you happen to know how I could test such a worker (one that connects to a database) in a development environment?Punkah
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. 12factor.net/dev-prod-parityEsbenshade
H
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.

Hashish answered 16/6, 2015 at 17:50 Comment(2)
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.Hashish
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 storyHashish

© 2022 - 2024 — McMap. All rights reserved.