Module: Epi::Daemon

Defined in:
lib/epi/daemon.rb,
lib/epi/daemon/sender.rb,
lib/epi/daemon/receiver.rb,
lib/epi/daemon/responder.rb,
lib/epi/daemon/responders/job.rb,
lib/epi/daemon/responders/start.rb,
lib/epi/daemon/responders/config.rb,
lib/epi/daemon/responders/status.rb,
lib/epi/daemon/responders/shutdown.rb,
lib/epi/daemon/responders/stop_all.rb

Defined Under Namespace

Modules: Responders Classes: Receiver, Responder, Sender

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.start_timeObject (readonly)

Returns the value of attribute start_time.



12
13
14
# File 'lib/epi/daemon.rb', line 12

def start_time
  @start_time
end

Class Method Details

.ensure_runningObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/epi/daemon.rb', line 18

def ensure_running
  should_run_as_root = Data.root?

  if running? && should_run_as_root && !process.root?
    logger.info "Daemon needs to run as root, but is running as #{process.user}"
    shutdown
  end

  unless running?
    if should_run_as_root && !Epi.root?
      raise Exceptions::Fatal, 'Found root data but not running as root. Either run again as root, ' +
          'or specify EPI_HOME as a directory other than /etc/epi'
    end

    logger.info 'Starting daemon'
    Epi.launch [$0, 'daemon'],
               stdout: Data.home + 'daemon.log',
               stderr: Data.home + 'daemon_errors.log'

    begin
      Timeout::timeout(5) { sleep 0.05 until socket_path.exist? }
    rescue Timeout::Error
      raise Exceptions::Fatal, 'Daemon not started after 5 seconds'
    end unless socket_path.exist?
  end
end

.loggerObject



14
15
16
# File 'lib/epi/daemon.rb', line 14

def logger
  Epi.logger
end

.processObject



94
95
96
97
98
# File 'lib/epi/daemon.rb', line 94

def process
  daemon_pid = Data.daemon_pid
  @process = nil if @process && @process.pid != daemon_pid
  @process ||= daemon_pid && RunningProcess.new(daemon_pid)
end

.runObject

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/epi/daemon.rb', line 49

def run
  raise Exceptions::Fatal, 'Daemon already running' if running?

  # Save the daemon PID
  Data.daemon_pid = Process.pid

  # Run an initial beat
  Jobs.beat!

  # Start a daemon
  EventMachine.start_unix_domain_server socket_path.to_s, Receiver
  logger.info "Listening on socket #{socket_path}"

  # Make sure other users can connect to the daemon
  socket_path.chmod 0777 #TODO: make configurable

  # Ensure the socket is destroyed when the daemon exits
  EventMachine.add_shutdown_hook { socket_path.delete }

  @start_time = Time.now
end

.running?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/epi/daemon.rb', line 90

def running?
  process && process.was_alive?
end

.send(*args, &callback) ⇒ Object



71
72
73
74
# File 'lib/epi/daemon.rb', line 71

def send(*args, &callback)
  ensure_running
  Sender.send *args, &callback
end

.shutdown(process = nil) ⇒ Object

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/epi/daemon.rb', line 76

def shutdown(process = nil)
  raise Exceptions::Fatal, 'Attempted to shut down daemon when no daemon is running' unless running?
  if (process || self.process).pid == Process.pid
    EventMachine.next_tick do
      EventMachine.stop_event_loop
      Data.daemon_pid = nil
      logger.info 'Daemon has shut down'
    end
  else
    logger.info 'Daemon will shut down'
    send :shutdown
  end
end

.socket_pathObject



45
46
47
# File 'lib/epi/daemon.rb', line 45

def socket_path
  Data.home + 'socket'
end