Created
September 18, 2014 10:47
-
-
Save oozzal/e0227f53fe80100b65b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mutex | |
def self.busy? | |
@busy ||= false | |
end | |
def self.engage | |
@busy = true | |
end | |
def self.free | |
@busy = false | |
end | |
end | |
# some worker with concurrency 1 | |
def upload_to_s3(filename, &callback) | |
t1 = Time.now | |
# simulate sidekiq's perform_async | |
Thread.new do | |
puts "Double execution alert!!!!" if Mutex.busy? | |
Mutex.engage | |
puts "Uploading #{filename} to s3" | |
sleep rand(1..3) | |
callback.call filename, (Time.now - t1) | |
Mutex.free | |
end | |
end | |
upload_to_s3("song.mp3") do |filename, elapsed| | |
puts "Uploading #{filename} to s3 took #{elapsed} seconds" | |
end | |
upload_to_s3("transcript.txt") do |filename, elapsed| | |
puts "Uploading #{filename} to s3 took #{elapsed} seconds" | |
end | |
# worker with concurrency 1 | |
class S3Uploader | |
# include Sidekiq::Worker | |
def self.perform(filename, bucket_name, &block) | |
puts "Connecting to #{bucket_name} bucket" | |
puts "Uploading #{filename} to #{bucket_name}" | |
block.call filename, bucket_name | |
end | |
end | |
S3Uploader.perform("song1.mp3", "songs") do |filename, bucket_name| | |
puts "Done uploading #{filename} to #{bucket_name}" | |
end | |
# called from somewhere else | |
S3Uploader.perform("transcript123.srt", "transcripts") do |filename, bucket_name| | |
puts "Done uploading #{filename} to #{bucket_name}" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment