Getting Started =============== [Sign up for a SimpleWorker account][1], it's free to try! [1]: http://www.simpleworker.com/ Configure SimpleWorker ---------------------- You really just need your access keys. SimpleWorker.configure do |config| config.access_key = ACCESS_KEY config.secret_key = SECRET_KEY end Write a Worker -------------- Here's an example worker that sends an email: require 'simple_worker' class EmailWorker < SimpleWorker::Base attr_accessor :to, :subject, :body # This is the method that will be run def run send_email(:to=>to, :subject=>subject, :body=>body) end def send_email # Put sending code here end end Test It Locally --------------- Let's say someone does something in your app and you want to send an email about it. worker = EmailWorker.new worker.to = current_user.email worker.subject = "Here is your mail!" worker.body = "This is the body" **worker.run** Queue up your Worker -------------------- Let's say someone does something in your app and you want to send an email about it. worker = EmailWorker.new worker.to = current_user.email worker.subject = "Here is your mail!" worker.body = "This is the body" **worker.queue** Schedule your Worker -------------------- There are two scenarios here, one is the scenario where you want something to happen due to a user action in your application. This is almost the same as queuing your worker. worker = EmailWorker.new worker.to = current_user.email worker.subject = "Here is your mail!" worker.body = "This is the body" **worker.schedule(:start_at=>1.hours.since)** Check Status ------------ If you still have access to the worker object, just call: worker.status If you only have the job ID, call: SimpleWorker.status(job_id) This will return a hash like: {"task_id"=>"ece460ce-12d8-11e0-8e15-12313b0440c6", "status"=>"running", "msg"=>nil, "start_time"=>"2010-12-28T23:19:36+00:00", "end_time"=>nil, "duration"=>nil, "progress"=>{"percent"=>25}} TODO: How to access log. Logging ------- log "Starting to do something..." Setting Progress ---------------- set_progress(:percent => progress, :message => "Server running. Trying to connect...") Schedule a Recurring Job - CRON ------------------------------ The alternative is when you want to user it like Cron. In this case you'll probably want to write a script that will schedule, you don't want to schedule it everytime your app starts or anything so best to keep it external. Create a file called 'schedule_email_worker.rb' and add this: require 'simple_worker' require_relative 'email_worker' worker = EmailWorker.new worker.to = current_user.email worker.subject = "Here is your mail!" worker.body = "This is the body" worker.schedule(:start_at=>1.hours.since, :run_every=>3600) Now run it and your worker will be scheduled to run every hour. SimpleWorker on Rails --------------------- Rails 2.X: config.gem 'simple_worker' Rails 3.X: gem 'simple_worker' Now you can use your workers like they're part of your app! We recommend putting your worker classes in /app/workers path. Configuring a Database Connection --------------------------------- Although you could easily do this in your worker, this makes it a bit more convenient and more importantly it will create the connection for you. If you are using ActiveRecord, you would add the following to your SimpleWorker config: config.database = { :adapter => "mysql2", :host => "localhost", :database => "appdb", :username => "appuser", :password => "secret" } Then before you job is run, SimpleWorker will establish the ActiveRecord connection. Including/Merging other Ruby Classes ------------------------------------ If you are using the Rails setup above, you can probably skip this as your models will automatically be merged. class AvgWorker < SimpleWorker::Base attr_accessor :aws_access_key, :aws_secret_key, :s3_suffix merge File.join(File.dirname(__FILE__), "..", "app", "models", "user.rb") merge File.join(File.dirname(__FILE__), "..", "app", "models", "account") Or simpler yet, try using relative paths: merge "../app/models/user" merge "../app/models/account.rb" Bringing in other Workers --------------------- merge_worker TODO Configuration Options --------------------- ### Global Attributes These are attributes that can be set as part of your config block then will be set on all your worker objects automatically. This is particularly good for things like database connection info or things that you would need to use across the board. Eg: config.global_attributes[:db_user] = "sa" config.global_attributes[:db_pass] = "pass" Then in your worker, you would have the attributes defined: attr_accessor :db_user, :db_pass