Class: Zabbirc::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zabbirc/service.rb', line 9

def initialize
  @semaphores_mutex = Mutex.new
  @semaphores       = Hash.new { |h, k| h[k] = Mutex.new }
  @ops = OpList.new
  @running = false

  initialize_bot

  @ops_service = Services::Ops.new self, @cinch_bot
  @events_service = Services::Events.new self, @cinch_bot
end

Instance Attribute Details

#cinch_botObject (readonly)

Returns the value of attribute cinch_bot.



5
6
7
# File 'lib/zabbirc/service.rb', line 5

def cinch_bot
  @cinch_bot
end

#opsObject (readonly)

Returns the value of attribute ops.



6
7
8
# File 'lib/zabbirc/service.rb', line 6

def ops
  @ops
end

#ops_serviceObject (readonly)

Returns the value of attribute ops_service.



6
7
8
# File 'lib/zabbirc/service.rb', line 6

def ops_service
  @ops_service
end

#runningObject (readonly)

Returns the value of attribute running.



7
8
9
# File 'lib/zabbirc/service.rb', line 7

def running
  @running
end

Instance Method Details

#initialize_botObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zabbirc/service.rb', line 21

def initialize_bot
  @cinch_bot = Cinch::Bot.new do
    configure do |c|
      c.server = Zabbirc.config.irc_server
      c.channels = Zabbirc.config.irc_channels
      c.nick = "zabbirc"
      c.plugins.plugins = [Irc::Plugin]
    end
  end

  # Stores reference to this Zabbirc::Service to be available in plugins
  @cinch_bot.instance_variable_set :@zabbirc_service, self
  @cinch_bot.class_eval do
    attr_reader :zabbirc_service
  end
end

#joinObject



99
100
101
# File 'lib/zabbirc/service.rb', line 99

def join
  @cinch_bot_thread.join
end

#pre_start_scriptObject



88
89
90
91
92
93
# File 'lib/zabbirc/service.rb', line 88

def pre_start_script
  @ops_service.sync_ops
  @ops.load_settings
rescue => e
  Zabbirc.logger.error "Exception `#{e}` while running pre_start_script"
end

#pre_stop_scriptObject



82
83
84
85
86
# File 'lib/zabbirc/service.rb', line 82

def pre_stop_script
  @ops.dump_settings
rescue => e
  Zabbirc.logger.error "Exception `#{e}` while running pre_stop_script"
end

#start(join = true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zabbirc/service.rb', line 45

def start join=true
  return if @running
  @running = true

  pre_start_script

  @cinch_bot_thread = Thread.new do
    begin
      @cinch_bot.start
    rescue => e
      Zabbirc.logger.fatal "Cinch bot start error: #{e}"
    end
  end

  @controll_thread = Thread.new do
    begin
      sleep
    rescue StopError
      pre_stop_script
      @ops_service.stop
      @events_service.stop

      Zabbirc.logger.info "Stopping ops service: #{@ops_service.join}"
      Zabbirc.logger.info "Stopping events service: #{@events_service.join}"

      @cinch_bot.quit
    ensure
      @running = false
    end
  end

  @ops_service.start
  @events_service.start

  @cinch_bot_thread.join if join
end

#stopObject



95
96
97
# File 'lib/zabbirc/service.rb', line 95

def stop
  @controll_thread.raise StopError
end

#synchronize(name, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/zabbirc/service.rb', line 38

def synchronize(name, &block)
  # Must run the default block +/ fetch in a thread safe way in order to
  # ensure we always get the same mutex for a given name.
  semaphore = @semaphores_mutex.synchronize { @semaphores[name] }
  semaphore.synchronize(&block)
end