Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module SidekiqThreadSafetyDemo
class Threadsafe
include Sidekiq::Worker
include Sidetiq::Schedulable
sidekiq_options :queue => :default
recurrence{ secondly }
SAFETY = Mutex.new
@@count = 0
def perform
SAFETY.synchronize do
logger.warn "threadsafe pre-count: #{@@count}"
logger.notice User.god.first_name
@@count += 1
logger.warn "threadsafe post-count: #{@@count}"
end
end
end
class NonThreadsafe
include Sidekiq::Worker
include Sidetiq::Schedulable
sidekiq_options :queue => :default
recurrence{ secondly }
@@count = 0
def perform
logger.warn "nonthreadsafe pre-count: #{@@count}"
logger.notice User.god.first_name
@@count += 1
logger.warn "nonthreadsafe post-count: #{@@count}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.