Included Modules

Chef::Expander::Daemonizable

Public Instance Methods

configure_process() click to toggle source

Daemonizes the process if configured to do so, and ensures that only one copy of the process is running with a given config by obtaining an exclusive lock on the pidfile. Also sets process user and group if so configured.

Raises

  • AlreadyRunning:

    when another process has the exclusive lock on the pidfile

  • NoSuchUser:

    when a user is configured that doesn’t exist

  • NoSuchGroup:

    when a group is configured that doesn’t exist

  • SystemCallError:

    if there is an error creating the pidfile

# File lib/chef/expander/daemonizable.rb, line 46
def configure_process
  Expander.config.daemonize? ? daemonize : ensure_exclusive
  set_user_and_group
end
daemonize() click to toggle source
# File lib/chef/expander/daemonizable.rb, line 51
def daemonize
  acquire_locks
  exit if fork
  Process.setsid
  exit if fork
  write_pid
  Dir.chdir('/')
  STDIN.reopen("/dev/null")
  STDOUT.reopen("/dev/null", "a")
  STDERR.reopen("/dev/null", "a")
end
ensure_exclusive() click to toggle source

When not forking into the background, this ensures only one chef-expander is running with a given config and writes the process id to the pidfile.

# File lib/chef/expander/daemonizable.rb, line 65
def ensure_exclusive
  acquire_locks
  write_pid
end
release_locks() click to toggle source

Deletes the pidfile, releasing the exclusive lock on it in the process.

# File lib/chef/expander/daemonizable.rb, line 85
def release_locks
  File.unlink(@pidfile.path) if File.exist?(@pidfile.path)
  @pidfile.close unless @pidfile.closed?
end
set_user_and_group() click to toggle source
# File lib/chef/expander/daemonizable.rb, line 70
def set_user_and_group
  return nil if Expander.config.user.nil?

  if Expander.config.group.nil?
    log.info {"Changing user to #{Expander.config.user}"}
  else
    log.info {"Changing user to #{Expander.config.user} and group to #{Expander.config.group}"}
  end

  unless (set_group && set_user)
    log.error {"Unable to change user to #{Expander.config.user} - Are you root?"}
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.