Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 904
Travis Reeder Avatar asked Jun 15 '12 18:06

Travis Reeder


2 Answers

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'])
like image 154
Travis Reeder Avatar answered Nov 11 '22 06:11

Travis Reeder


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.

like image 23
Chiedo Avatar answered Nov 11 '22 04:11

Chiedo