Skip to content

Instantly share code, notes, and snippets.

@Davidslv
Last active August 29, 2015 14:13
Show Gist options
  • Save Davidslv/604fd4dd4ddc1200089d to your computer and use it in GitHub Desktop.
Save Davidslv/604fd4dd4ddc1200089d to your computer and use it in GitHub Desktop.
how to speed up your workers?
class SuperServiceObject
attr_reader :user, :options
def new(user_id, options = {})
@user = User.find(user_id)
@options = options
end
def call
# move the code here
sleep 4
end
end
class SuperWorker
include Sidekiq::Worker
def perform(user_id, options = {})
# imagine this as a bunch of code here that takes
# a while to process, this is bad because it will require
# the worker to wait for this bunch of code to finish in order
# to pick up another job
sleep 4
## my test took 4 seconds to run
# Finished in 4.3 seconds
# 1 example, 0 failures
# move the entire code into a Service Object
# - this means your worker is done, it's now up to the service object
# to finish whatever it needs to do.
SuperServiceObject.new(user_id, options).call
## my test now takes 0.3 seconds
# Finished in 0.29994 seconds
# 1 example, 0 failures
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment