Created
April 4, 2011 22:42
-
-
Save djktno/902625 to your computer and use it in GitHub Desktop.
Integrating MongoDB/Mongoid with SimpleWorker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'simple_worker' | |
| require 'mongoid' | |
| class ExampleWorker < SimpleWorker::Base | |
| #Not sure if this is necessary (according to the docs), but | |
| #it stopped complaining when I did it. | |
| merge File.join(File.dirname("__FILE__"), "..", "models", "user.rb") | |
| attr_accessor :user_id, :mongodb_settings | |
| def run | |
| init_mongodb | |
| #create a user object (in this space) via lookup. | |
| user = User.find(user_id) | |
| #Do something cool with the user | |
| user.save | |
| log "Saved user." | |
| end | |
| private | |
| def init_mongodb | |
| Mongoid.configure do |config| | |
| config.from_hash(@mongodb_settings) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| filename = File.join(File.dirname(__FILE__), "..", "mongoid.yml") | |
| DATABASE = YAML.load(ERB.new(File.new(filename).read).result) | |
| SimpleWorker.configure do |config| | |
| config.access_key = '' | |
| config.secret_key = '' | |
| config.global_attributes[:mongodb_settings] = DATABASE[Rails.env] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example, thanks!