Class: Crabfarm::Engines::SafeStateLoop
- Inherits:
-
Object
- Object
- Crabfarm::Engines::SafeStateLoop
- Defined in:
- lib/crabfarm/engines/safe_state_loop.rb
Instance Method Summary collapse
- #cancel ⇒ Object
- #change_state(_name, _params = {}, _wait = nil) ⇒ Object
-
#initialize ⇒ SafeStateLoop
constructor
A new instance of SafeStateLoop.
- #release ⇒ Object
- #wait_for_state(_wait = nil) ⇒ Object
Constructor Details
#initialize ⇒ SafeStateLoop
Returns a new instance of SafeStateLoop.
8 9 10 11 12 13 14 |
# File 'lib/crabfarm/engines/safe_state_loop.rb', line 8 def initialize @running = true @working = false @fatal = nil @lock = Mutex.new @thread = Thread.new { crawl_loop } end |
Instance Method Details
#cancel ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/crabfarm/engines/safe_state_loop.rb', line 46 def cancel @lock.synchronize { if @working @thread.terminate @thread.join @thread = Thread.new { crawl_loop } @working = false end } end |
#change_state(_name, _params = {}, _wait = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/crabfarm/engines/safe_state_loop.rb', line 21 def change_state(_name, _params={}, _wait=nil) @lock.synchronize { if @fatal raise CrawlerError.new @fatal elsif @working raise StillWorkingError.new unless matches_next_state? _name, _params wait_and_load_struct _wait elsif matches_current_state? _name, _params state_as_struct else @next_state_name = _name @next_state_params = _params @working = true wait_and_load_struct _wait end } end |
#release ⇒ Object
16 17 18 19 |
# File 'lib/crabfarm/engines/safe_state_loop.rb', line 16 def release @running = false @thread.join end |
#wait_for_state(_wait = nil) ⇒ Object
40 41 42 43 44 |
# File 'lib/crabfarm/engines/safe_state_loop.rb', line 40 def wait_for_state(_wait=nil) @lock.synchronize { wait_and_load_struct _wait } end |