Sha256: 4bdef9ef5e52bf5ab5d24117019b281571b1f5b158e390b02a78564e57aec66b

Contents?: true

Size: 1.69 KB

Versions: 24

Compression:

Stored size: 1.69 KB

Contents

require "fileutils"

module Lita
  # Converts Lita to a daemon process.
  # @deprecated Will be removed in Lita 5.0. Use your operating system's process manager instead.
  class Daemon
    # @param pid_path [String] The path to the PID file.
    # @param log_path [String] The path to the log file.
    # @param kill_existing [Boolean] Whether or not to kill existing processes.
    def initialize(pid_path, log_path, kill_existing)
      @pid_path = pid_path
      @log_path = log_path
      @kill_existing = kill_existing
    end

    # Converts Lita to a daemon process.
    # @return [void]
    def daemonize
      handle_existing_process
      Process.daemon(true)
      File.open(@pid_path, "w") { |f| f.write(Process.pid) }
      set_up_logs
      at_exit { FileUtils.rm(@pid_path) if File.exist?(@pid_path) }
    end

    private

    # Abort if Lita is already running.
    def ensure_not_running
      abort I18n.t("lita.daemon.pid_exists", path: @pid_path) if File.exist?(@pid_path)
    end

    # Call the appropriate method depending on kill mode.
    def handle_existing_process
      if @kill_existing && File.exist?(@pid_path)
        kill_existing_process
      else
        ensure_not_running
      end
    end

    # Try to kill an existing process.
    def kill_existing_process
      pid = File.read(@pid_path).to_s.strip.to_i
      Process.kill("TERM", pid)
    rescue Errno::ESRCH, RangeError, Errno::EPERM
      abort I18n.t("lita.daemon.kill_failure", pid: pid)
    end

    # Redirect the standard streams to a log file.
    def set_up_logs
      log_file = File.new(@log_path, "a")
      STDOUT.reopen(log_file)
      STDERR.reopen(log_file)
      STDERR.sync = STDOUT.sync = true
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
lita-4.8.0 lib/lita/daemon.rb
lita-4.8.0.beta1 lib/lita/daemon.rb
lita-4.7.1 lib/lita/daemon.rb
lita-4.7.0 lib/lita/daemon.rb
lita-4.6.1 lib/lita/daemon.rb
lita-4.6.0 lib/lita/daemon.rb
lita-4.5.0 lib/lita/daemon.rb
lita-4.4.3 lib/lita/daemon.rb
lita-4.4.2 lib/lita/daemon.rb
lita-4.4.1 lib/lita/daemon.rb
lita-4.4.0 lib/lita/daemon.rb
lita-4.3.2 lib/lita/daemon.rb
lita-4.3.1 lib/lita/daemon.rb
lita-4.3.0 lib/lita/daemon.rb
lita-4.2.1 lib/lita/daemon.rb
lita-4.2.0 lib/lita/daemon.rb
lita-4.1.0 lib/lita/daemon.rb
lita-4.0.4 lib/lita/daemon.rb
lita-4.0.3 lib/lita/daemon.rb
lita-4.0.2 lib/lita/daemon.rb