Skip to content

Instantly share code, notes, and snippets.

@digitalextremist
Created September 21, 2015 07:47
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 digitalextremist/686f42e58a58b743142b to your computer and use it in GitHub Desktop.
Save digitalextremist/686f42e58a58b743142b to your computer and use it in GitHub Desktop.
require 'celluloid/current'
class Indefinite
include Celluloid
INTERVAL = 0.5
ONE_AT_A_TIME = false
def self.run!
puts "000a Instantiating."
indefinite = new
indefinite.run
puts "000b Running forever:"
sleep
end
def initialize
puts "001a Initializing."
@mutex = Mutex.new if ONE_AT_A_TIME
@running = false
puts "001b Interval: #{INTERVAL}"
every(INTERVAL) { run }
end
def run
puts "002a Running."
unless ONE_AT_A_TIME && @running
if ONE_AT_A_TIME
@mutex.synchronize {
puts "002b Inside lock."
@running = true
on_background
@running = false
}
else
puts "002b Without lock."
on_background
end
end
end
def on_background
uuid = Celluloid::Internals::UUID.generate
if ONE_AT_A_TIME
puts "003a #{uuid}: Running background processor in foreground."
else
puts "003a #{uuid}: Running in background"
end
sleep(INTERVAL*9)
puts "003b #{uuid}: Finish running simulated 2 second job"
end
end
Indefinite.run!
puts "004 End of application."
de@extremist /code/issues/so >> master $ ruby -v
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment