There are a couple ways to configure access to MongoDB. Presumably you're using mapping gems such as Mongoid orMongoMapper to handle the interface. A simple and easy way is to set all the variables directly within your worker -- in an A more advanced way to do it is via the config setup within your application and then apply the settings as part of the worker initialization. This way lets you point to different databases depending on your environment (i.e. development or production). Blog Article on Connecting to MongoFirst we should point out a great article by Daniel Kunnath from GameAttain on MongoDB/Mongoid integration with SimpleWorker. MongoDB/Mongoid Integration with SimpleWorker MongoDB Worker ExampleSecond, take a look at the MongoDB example on GitHub. It uses MongoHQ but will just as easily with self-managed MongoDB. Configuring Basic Access in the WorkerOnce the configuration in place, you'd then read, write, and access the database as you would if your code is running locally.The basic approach is to explicitly set the variables in the Worker class. You might, for example, create an
For example, if you had a class called Person
You'd reference it in your worker just as you would in your application.
Setting Up Global AttributesA easiest way to pass in your database configuration if you're going to use it on several workers is to set global attributes which might change based on your environment. In a Rails environment files, you'd set them like this:
In your worker, add another attribute called
Configure Database before Initializing ModelsOne important point is that in Rails, you need to configure the database before initializing the model. Otherwise, you run the risk of a StackOverflow Issue: Mongoid With Rails Database Passing DB Config Info in via Worker attr_accessorsYou can also pass in db config settings within the worker data payloads. Again, you can either set them directly or read them from a config.yml or other type of file. In the code that instantiates and queues the worker, you'd have this.
In your worker, you'd have the following with the
Other ResourcesHere are some other resources on MongoDB: |