Class: Adminix::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/adminix/watcher.rb

Constant Summary collapse

SERVICE_CHANNEL =
'ServiceChannel'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Watcher

Returns a new instance of Watcher.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/adminix/watcher.rb', line 18

def initialize(opts)
  @sync_period = config.watcher_sync_period
  @pid_full = '/tmp/adminix.pid'
  @service = Service.instance
  
  @ws_client = nil
  @ws_uri = "#{config.websocket_path}?secret_key=#{config.secret_key}"
  @ws_channel = { channel: 'ServiceChannel', service_id: @service.id }
  @ws_logs_channel = { channel: 'LogsChannel', service_id: @service.id }

  @watching_files = []

  if EM.epoll?
    EM.epoll
  elsif EM.kqueue?
    EM.kqueue
  else
    Kernel.warn('Neither epoll nor kqueue are supported.')
  end
end

Class Method Details

.run!(options) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/adminix/watcher.rb', line 6

def self.run!(options)
  watcher = Watcher.new(options)

  if options[:stop_daemon]
    watcher.stop
  else
    options[:daemonize] ? watcher.start : watcher.run!
  end
end

Instance Method Details

#get_pidObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/adminix/watcher.rb', line 94

def get_pid
  if File.exists?(@pid_full)
    file = File.new(@pid_full, "r")
    pid = file.read
    file.close

    pid
  else
    0
  end
end

#run!Object



39
40
41
42
43
44
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
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/adminix/watcher.rb', line 39

def run!
  trap_signal

  Adminix::Service.instance.count_logs_lines

  EventMachine.run do
    @ws_client = ActionCableClient.new(@ws_uri, @ws_channel)
    @ws_client.connected do |msg|
      puts 'Service connection established'
    end
    @ws_client.disconnected do
      system.log 'Service disconnected. Reconnecting...'
      sleep(2)
      @ws_client.connect!
    end
    @ws_client.received do |msg|
      message = msg['message'] || {}
      case message['type']
        when 'restart' then @service.restart!
        when 'sync' then @service.sync(@ws_client, message['data'])
      end
    end

    @ws_logs_client = ActionCableClient.new(@ws_uri, @ws_logs_channel)
    @ws_logs_client.connected do |msg|
      puts 'Logs connection established'
    end
    @ws_logs_client.disconnected do
      system.log 'Logs disconnected. Reconnecting...'
      sleep(2)
      @ws_logs_client.connect!
    end
    @ws_logs_client.received { |msg| }

    # Sync watcher
    EventMachine.add_periodic_timer(config.watcher_sync_period) do
      @ws_client.perform(:sync, @service.to_cable) if @ws_client.subscribed?
    end

    # Logs watcher
    if config.logs_enabled
      config.watch_log_files.each do |file_path|
        File.write(file_path, '') unless File.exists?(file_path)
        if File.exists?(file_path)
          EventMachine.watch_file(file_path, Adminix::LogWatchHandler)
        end
      end
    end

    EventMachine.add_periodic_timer(config.logs_sync_period) do
      Adminix::Service.instance.sync_logs(@ws_logs_client)
    end
  end
end

#startObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/adminix/watcher.rb', line 106

def start
  pid = get_pid
  if pid != 0
    warn "Daemon is already running"
    exit -1
  end

  pid = fork {
    run!
  }
  begin
    file = File.new(@pid_full, "w")
    file.write(pid)
    file.close
    Process.detach(pid)
  rescue => exc
    Process.kill('TERM', pid)
    warn "Cannot start daemon: #{exc.message}"
  end
end

#stopObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/adminix/watcher.rb', line 127

def stop
  pid = get_pid
  begin
    EM.stop
  rescue
  end

  if pid != 0
    Process.kill('HUP', pid.to_i)
    File.delete(@pid_full)
    system.log "Stopped"
  else
    warn "Daemon is not running"
    exit -1
  end
end