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?
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'])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With