It's best to think of your workers as completely separate entities from Ruby code or your Rails app. In this sense, they need to be able to configure themselves to external services when they run. So if it has to connect to a database or use global attributes, you'll want to have the worker be able to access this configuration information as if it were running outside your application (which it is). SimpleWorker Access KeysSimpleWorker configuration is done in a configuration block and its simplest form looks like this: Gem v1.X
The access_key and secret_key are the only required configuration attributes, the rest is optional. (Your SimpleWorker ACCESS_KEY and SECRET_KEY can be found on each project's project page in the SimpleWorker dashboard.) Gem v2.X
Get your config token at http://www.simpleworker.com/tokens If you are using SimpleWorker in a Ruby on Rails application, you would add the config block above to your (We recommend you use different SimpleWorker projects for development and production. Each project has unique keys so make sure you assign them appropriately in the config blocks.) Configuring DatabasesYou can set db access keys as part of an init() method of your worker class or you can include it within the config block like the keys above. See our knowledge base section on configuring databases to learn how to use your database with SimpleWorker. Note that each worker/job is a standalone unit of work so if you run 20 concurrent workers, you'd create 20 connections to your database. We recommend that you look to group a number of database tasks together into a single worker to better amortize the configuration time. Setting Global AttributesAttributes can be set in your configuration block so that they will automatically be set in all of your workers that have these attributes defined. This approach is great for taking care of attributes that all of your workers will use as well as for situations where the keys might change between environments. As an example, if you were to include the following as part of your config block, then these values will be set in all your worker objects that have those attributes defined.
In your worker, you would simply have the attributes defined as follows:
Now when you use these attributes in your worker, they will be set to "admin" and "notarealsecurepassword" automatically. Changing Attributes By EnvironmentYou can also set up global config attributes to read from a file. For example:
Then in your worker, just add:
When you call queue, the global attributes will automatically be set on those attributes so you don't need to set them every time you use the worker. Avoid Rails References in your WorkersBecause workers are running separate from your application, you want to avoid using Rails environment references in your worker. If you do, you'll likely get an uninitialized constant error as the worker tries to resolve the references in the SimpleWorker cloud. (Exceptions are Rails.env and Rails.version. See below.) By way of example, do not do the following: class IncorrectWorker < SimpleWorker::Base merge "#{Rails.root}/vendor/plugins/.../somefile.rb" def run end end It may work when you uninitialized constant IncorrectWorker::Rails Accessing Rails.env and Rails.versionExceptions to the "not referencing Rails constants in your workers" rule are
|
Ruby >