Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created May 21, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jared-Prime/2f4345427891a61bdf8a to your computer and use it in GitHub Desktop.
Save Jared-Prime/2f4345427891a61bdf8a to your computer and use it in GitHub Desktop.
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