Class: Crabfarm::Engines::AsyncStateManager
- Inherits:
-
Object
- Object
- Crabfarm::Engines::AsyncStateManager
show all
- Defined in:
- lib/crabfarm/engines/async_state_manager.rb
Defined Under Namespace
Classes: LoopAbortedException
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AsyncStateManager.
10
11
12
13
14
15
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 10
def initialize(_context)
@context = _context
@working = false
@fatal = nil
@lock = Mutex.new
end
|
Instance Method Details
#navigate(_name, _params = {}, _wait = nil) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 41
def navigate(_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
|
#restart ⇒ Object
36
37
38
39
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 36
def restart
stop
start
end
|
#start ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 17
def start
@lock.synchronize {
if @thread.nil?
@fatal = nil
@thread = Thread.new { crawl_loop }
end
}
end
|
#stop ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 26
def stop
@lock.synchronize {
unless @thread.nil?
@thread.raise LoopAbortedException
@thread.join
@thread = nil
end
}
end
|
#wait_for_state(_wait = nil) ⇒ Object
60
61
62
63
64
|
# File 'lib/crabfarm/engines/async_state_manager.rb', line 60
def wait_for_state(_wait=nil)
@lock.synchronize {
wait_and_load_struct _wait
}
end
|