-
-
Save digitalextremist/686f42e58a58b743142b to your computer and use it in GitHub Desktop.
Answer #2 to: http://stackoverflow.com/questions/32492560/celluloid-async-inside-ruby-blocks-does-not-work
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
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