Class: Zabbirc::Services::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbirc/services/base.rb

Direct Known Subclasses

Events, Ops

Constant Summary collapse

LOOP_SLEEP =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#runningObject (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

#joinObject



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

#startObject



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

#stopObject



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