Module: Epi

Defined in:
lib/epi.rb,
lib/epi/cli.rb,
lib/epi/job.rb,
lib/epi/data.rb,
lib/epi/jobs.rb,
lib/epi/daemon.rb,
lib/epi/launch.rb,
lib/epi/logging.rb,
lib/epi/trigger.rb,
lib/epi/version.rb,
lib/epi/connection.rb,
lib/epi/exceptions.rb,
lib/epi/cli/command.rb,
lib/epi/daemon/sender.rb,
lib/epi/process_status.rb,
lib/epi/triggers/touch.rb,
lib/epi/daemon/receiver.rb,
lib/epi/exceptions/base.rb,
lib/epi/job_description.rb,
lib/epi/running_process.rb,
lib/epi/triggers/memory.rb,
lib/epi/triggers/uptime.rb,
lib/epi/cli/commands/job.rb,
lib/epi/daemon/responder.rb,
lib/epi/exceptions/fatal.rb,
lib/epi/cli/commands/help.rb,
lib/epi/cli/commands/stop.rb,
lib/epi/cli/commands/start.rb,
lib/epi/configuration_file.rb,
lib/epi/cli/commands/config.rb,
lib/epi/cli/commands/daemon.rb,
lib/epi/cli/commands/status.rb,
lib/epi/exceptions/shutdown.rb,
lib/epi/cli/commands/restart.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,
lib/epi/cli/commands/concerns/daemon.rb,
lib/epi/triggers/concerns/comparison.rb,
lib/epi/exceptions/invalid_configuration_file.rb

Defined Under Namespace

Modules: Cli, Daemon, Exceptions, Jobs, Triggers Classes: ConfigurationFile, Connection, Data, Job, JobDescription, ProcessStatus, RunningProcess, Trigger

Constant Summary collapse

ROOT =
Pathname File.expand_path('../..', __FILE__)
DEFAULT_LEVEL =
'info'
VERSION =
'0.1.5'

Class Method Summary collapse

Class Method Details

.launch(command, env: {}, user: nil, cwd: nil, stdout: true, stderr: true) ⇒ Fixnum

Run a system command in the background, and allow it to keep running after Ruby has exited.

Parameters:

  • command (String|Array)

    The command to run, either as a pre-escaped string, or an array of non-escaped strings (a command and zero or more arguments).

  • env (Hash) (defaults to: {})

    Environment variables to be passed to the command, in addition to those inherited from the current environment

  • user (String|NilClass) (defaults to: nil)

    If supplied, command will be run through su as this user.

  • cwd (String|NilClass) (defaults to: nil)

    If supplied, command will be run from this directory.

  • stdout (String|TrueClass|FalseClass|NilClass) (defaults to: true)

    Where to redirect standard output; either a file path, or true for no redirection, or false/nil to redirect to /dev/null

  • stderr (String|TrueClass|FalseClass|NilClass) (defaults to: true)

    Where to redirect standard error; either a file path, or true for no redirection, or false/nil to redirect to /dev/null

Returns:

  • (Fixnum)

    The PID of the started process



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/epi/launch.rb', line 19

def self.launch(command, env: {}, user: nil, cwd: nil, stdout: true, stderr: true)

  # Prevent hang-up
  cmd = 'nohup '

  # The main command and its arguments
  if String === command

    # Pre-escaped string
    cmd << command
  else

    # Command and arguments that need to be escaped
    command.each { |part| cmd << ' ' << escape(part) }
  end

  # STDOUT and STDERR redirection
  {:>> => stdout, :'2>>' => stderr}.each do |arrow, dest|
    cmd << " #{arrow} #{dest || '/dev/null'}" unless TrueClass === dest
  end

  # Run in background, and return PID of backgrounded process
  cmd << ' & echo $!'

  # Include `su` command if a user is given
  cmd = "su #{user} -c #{escape cmd}" if user

  # Include the working directory
  cmd = "cd #{escape cwd} && (#{cmd})" if cwd

  # Convert environment variables to strings, and merge them with the current environment
  env = ENV.to_h.merge(env).map { |k, v| [k.to_s, v.to_s] }.to_h

  logger.debug "Starting `#{cmd}`"

  # Run the command and read the resulting PID from its STDOUT
  IO.popen(env, cmd) { |p| p.read }.to_i.tap do |pid|
    logger.info "Process #{pid} started: #{`ps -p #{pid} -o command=`.chomp}"
  end
end

.loggerObject



7
8
9
# File 'lib/epi/logging.rb', line 7

def logger
  @logger ||= make_logger
end

.logger=(value) ⇒ Object



11
12
13
# File 'lib/epi/logging.rb', line 11

def logger=(value)
  @logger = Logger === value ? value : make_logger(value)
end

.root?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/epi.rb', line 27

def root?
  @is_root = `whoami`.chomp == 'root' if @is_root.nil?
  @is_root
end