Class: Exekutor::Internal::CLI::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/exekutor/internal/cli/daemon.rb

Overview

Manages daemonization of the current process.

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pidfile:) ⇒ Daemon

Returns a new instance of Daemon.

Parameters:

  • pidfile (String)

    Pidfile path



15
16
17
# File 'lib/exekutor/internal/cli/daemon.rb', line 15

def initialize(pidfile:)
  @pidfile = pidfile
end

Instance Attribute Details

#pidfileString (readonly)

The path of the generated pidfile.

Returns:

  • (String)


12
13
14
# File 'lib/exekutor/internal/cli/daemon.rb', line 12

def pidfile
  @pidfile
end

Instance Method Details

#daemonizevoid

This method returns an undefined value.

Daemonizes the current process and writes out a pidfile.



21
22
23
24
25
# File 'lib/exekutor/internal/cli/daemon.rb', line 21

def daemonize
  validate!
  ::Process.daemon true
  write_pid
end

#pidInteger?

The process ID for this daemon, if known

Returns:

  • (Integer, nil)

    The process ID

Raises:

  • (Error)

    if the pid-file is corrupt



30
31
32
33
34
35
36
37
# File 'lib/exekutor/internal/cli/daemon.rb', line 30

def pid
  return nil unless ::File.exist? pidfile

  pid = ::File.read(pidfile)
  raise Error, "Corrupt PID-file. Check #{pidfile}" unless pid.to_i.positive?

  pid.to_i
end

#status:running, ...

The process status for this daemon. Possible states are:

  • :running when the daemon is running;

  • :not_running when the daemon is not running;

  • :dead when the daemon is dead. (Ie. the PID is known, but the process is gone);

  • :not_owned when the daemon cannot be accessed.

Returns:

  • (:running, :not_running, :dead, :not_owned)

    the status



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/exekutor/internal/cli/daemon.rb', line 45

def status
  pid = self.pid
  return :not_running if pid.nil?

  # If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for
  # the existence of a process ID or process group ID.
  ::Process.kill(0, pid)
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end

#status?(*statuses) ⇒ Boolean

Checks whether #status matches any of the given statuses.

Parameters:

  • statuses (Symbol...)

    The statuses to check for.

Returns:

  • (Boolean)

    whether the status matches

See Also:



63
64
65
# File 'lib/exekutor/internal/cli/daemon.rb', line 63

def status?(*statuses)
  statuses.include? status
end

#validate!void

This method returns an undefined value.

Raises an Error if a daemon is already running. Deletes the pidfile is the process is dead.

Raises:

  • (Error)

    when the daemon is running



70
71
72
73
74
75
76
77
78
# File 'lib/exekutor/internal/cli/daemon.rb', line 70

def validate!
  case status
  when :running, :not_owned
    raise Error, "A worker is already running. Check #{pidfile}"
  else
    delete_pid
  end
  nil
end