Skip to content

Instantly share code, notes, and snippets.

@djktno
Created April 4, 2011 22:42
Show Gist options
  • Select an option

  • Save djktno/902625 to your computer and use it in GitHub Desktop.

Select an option

Save djktno/902625 to your computer and use it in GitHub Desktop.
Integrating MongoDB/Mongoid with SimpleWorker
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
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
@treeder
Copy link

treeder commented May 3, 2011

Great example, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment