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?
How do I connect to a database in IronWorker using ActiveRecord?
Asked Answered
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'])
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-parity –
Esbenshade 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.
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 story –
Hashish
© 2022 - 2024 — McMap. All rights reserved.