Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Add daemonization options to Lita::CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Jul 26, 2013
1 parent 3dcf208 commit 4ff2932
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion lib/lita/cli.rb
@@ -1,3 +1,5 @@
require "fileutils"

require "thor"

module Lita
Expand All @@ -14,18 +16,67 @@ def self.source_root
class_option :config,
aliases: "-c",
banner: "PATH",
default: "lita_config.rb",
default: File.expand_path("lita_config.rb", Dir.pwd),
desc: "Path to the configuration file to use"

class_option :daemonize,
aliases: "-d",
default: false,
desc: "Run Lita as a daemon",
type: :boolean

class_option :log_file,
aliases: "-l",
banner: "PATH",
default: Process.euid == 0 ?
"/var/log/lita.log" : File.expand_path("lita.log", ENV["HOME"]),
desc: "Path where the log file should be written when daemonized"

class_option :pid_file,
aliases: "-p",
banner: "PATH",
default: Process.euid == 0 ?
"/var/run/lita.pid" : File.expand_path("lita.pid", ENV["HOME"]),
desc: "Path where the PID file should be written when daemonized"

desc "start", "Starts Lita"
def start
Bundler.require
daemonize if options[:daemonize]
Lita.run(options[:config])
end

desc "new NAME", "Generates a new Lita project (default name: lita)"
def new(name = "lita")
directory "skeleton", name
end

private

def daemonize
ensure_not_running
Process.daemon(true)
File.open(options[:pid_file], "w") { |f| f.write(Process.pid) }
set_up_logs
at_exit do
FileUtils.rm(options[:pid_file]) if File.exist?(options[:pid_file])
end
end

def ensure_not_running
if File.exist?(options[:pid_file])
abort <<-FATAL.chomp
PID file exists at #{options[:pid_file]}. Lita may already be running. \
Kill the existing process or remove the PID file and then start Lita.
FATAL
end
end

def set_up_logs
log_file = File.new(options[:log_file], "a")
$stdout.reopen(log_file)
$stderr.reopen(log_file)
$stderr.sync = $stdout.sync = true
end
end
end

0 comments on commit 4ff2932

Please sign in to comment.