Class: Zabbirc::Services::Base
- Inherits:
-
Object
- Object
- Zabbirc::Services::Base
- Defined in:
- lib/zabbirc/services/base.rb
Constant Summary collapse
- LOOP_SLEEP =
10
Instance Attribute Summary collapse
-
#running ⇒ Object
readonly
Returns the value of attribute running.
Instance Method Summary collapse
-
#initialize(service, cinch_bot) ⇒ Base
constructor
A new instance of Base.
- #join ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #synchronize(&block) ⇒ Object
Constructor Details
#initialize(service, cinch_bot) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 |
# File 'lib/zabbirc/services/base.rb', line 7 def initialize service, cinch_bot @service = service @cinch_bot = cinch_bot @running = false @mutex = Mutex.new end |
Instance Attribute Details
#running ⇒ Object (readonly)
Returns the value of attribute running.
5 6 7 |
# File 'lib/zabbirc/services/base.rb', line 5 def running @running end |
Instance Method Details
#join ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/zabbirc/services/base.rb', line 18 def join slept = 0 while slept < (LOOP_SLEEP * 2) return true if @running == false sleep 1 slept += 1 end false end |
#start ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/zabbirc/services/base.rb', line 28 def start main_thread = Thread.current @thread = Thread.new do begin loop do Thread.handle_interrupt(StopError => :never) do @running = true iterate end Thread.handle_interrupt(StopError => :immediate) do sleep LOOP_SLEEP end end rescue StopError => e # nothing, ensure block sets the variables rescue => e main_thread.raise e ensure @running = false end end end |
#stop ⇒ Object
51 52 53 |
# File 'lib/zabbirc/services/base.rb', line 51 def stop @thread.raise StopError end |
#synchronize(&block) ⇒ Object
14 15 16 |
# File 'lib/zabbirc/services/base.rb', line 14 def synchronize &block @mutex.synchronize &block end |