-
-
Save digitalextremist/12fc824c6a4dbd94a9df to your computer and use it in GitHub Desktop.
Answer 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}" | |
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 | |
puts "002c Setting new timer." | |
after(INTERVAL) { run } | |
end | |
def on_background | |
if ONE_AT_A_TIME | |
puts "003 Running background processor in foreground." | |
else | |
puts "003 Running in background" | |
end | |
end | |
end | |
Indefinite.run! | |
puts "004 End of application." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment