Sidekiq - SeanHolden/Wiki GitHub Wiki
#What I've learned from Sidekiq
In Gemfile:
gem 'sidekiq'
gem 'sinatra', :require => false
gem 'slim'
Create new directory called 'workers' inside 'apps' directory
Inside apps/workers, create a file… e.g. reminder_worker.rb … fill it with something like the following:
# apps/workers/reminder_worker.rb
class ReminderWorker
include Sidekiq::Worker
def perform(any_args_passed_through)
# code goes here*
end
end
To perform a task in background…
# in your controller:
ReminderWorker.perform_async(any_args_to_pass_through)
To perform a task in background at a later date/time
# in your controller:
ReminderWorker.perform_at(some_date_and_time, any_args_to_pass_through)
###To run sidekiq:
start your redis server, then start sidekiq with …
$ bundle exec sidekiq
###For sidekiq web interface:
# config/routes.rb
require 'sidekiq/web'
mount Sidekiq::Web, at: '/sidekiq'
##Sidekiq API examples:
Delete all scheduled jobs to be run in the next 4 hours:
delayed = Sidekiq::ScheduledSet.new
delayed.each do |job|
job.delete if job.at < 4.hours.from_now
end
Delete all scheduled jobs for a specific user
delayed = Sidekiq::ScheduledSet.new
delayed.each do |job|
job.delete if job.args == [user_id]
end
##How a sidekiq schedule is stored in redis (example):
"{\"retry\":true,\"queue\":\"default\",\"class\":\"ReminderWorker\",\"args\":[3],\"at\":1352887200.0,\"jid\":\"6e058f2c500c71f6d16cb08d\"}"
These values can be returned by using the sidekiq API examples described above.
Examples:
job.jid => 6e058f2c500c71f6d16cb08d
job.at => 2012-11-14 10:00:00 +0000
job.queue => "default"
job.args => whatever arguments you passed through. e.g. in this case, user_id
###…You get the idea…