Skip to content

Instantly share code, notes, and snippets.

@pramodshinde
Last active March 23, 2018 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pramodshinde/f4e1256ac9cc6031fce5 to your computer and use it in GitHub Desktop.
Save pramodshinde/f4e1256ac9cc6031fce5 to your computer and use it in GitHub Desktop.
Sample template sidekiq worker with sidekiq-status
##
# Obvious steps to follow before adding this template worker to your Rails Application
# 1. Add 'gem sidekiq-status' to Gemfile
# 2. bundle install
# 3. include Sidekiq::Status::Worker in your worker if you want tarck the progrees of your worker.
# 4. Setup Sidekiq Web & sidekiq-status Web UI's
# a. Sidekiq Web UI
# a1. Add gem 'sinatra', :require => nil to Gemfile
# a2. Add follwing to routes
# require 'sidekiq/web'
# mount Sidekiq::Web => '/sidekiq'
# b. Add folliwing line to routes
# require 'sidekiq-status/web'
# 5. As this just a template, Please make neccessary chnages to fit it in your Rails Application.
## Template Sidekiq Worker with sidekiq-status
class UserWorker
include Sidekeq::Worker
include Sidekiq::Status::Worker #Important
def perform
# Setting total number of users
total User.count
store delivery_count: 0
User.find_each do |user|
# Setting 'at' for progress
status = retrieve(:at).to_i
delivery_count = retrieve(:delivery_count).to_i
at (status + 1), "User: #{user.id}, delivery_count: #{delivery_count}"
UserMailer.weekly_summary(user).deliver_now
delivery_count += 1
store delivery_count: delivery_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment