Module: Qless::Pool::CLI
Instance Method Summary collapse
- #daemonize ⇒ Object
- #manage_pidfile(pidfile) ⇒ Object
- #parse_options ⇒ Object
- #process_still_running?(pidfile) ⇒ Boolean
- #redirect(opts) ⇒ Object
- #run ⇒ Object
-
#set_pool_options(opts) ⇒ Object
TODO: global variables are not the best way.
- #setup_environment(opts) ⇒ Object
- #start_pool ⇒ Object
Instance Method Details
#daemonize ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/qless/pool/cli.rb', line 53 def daemonize raise 'First fork failed' if (pid = fork) == -1 exit unless pid.nil? Process.setsid raise 'Second fork failed' if (pid = fork) == -1 exit unless pid.nil? end |
#manage_pidfile(pidfile) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/qless/pool/cli.rb', line 61 def manage_pidfile(pidfile) return unless pidfile pid = Process.pid if File.exist? pidfile if process_still_running? pidfile raise "Pidfile already exists at #{pidfile} and process is still running." else File.delete pidfile end else FileUtils.mkdir_p File.dirname(pidfile) end File.open pidfile, "w" do |f| f.write pid end at_exit do if Process.pid == pid File.delete pidfile end end end |
#parse_options ⇒ Object
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 |
# File 'lib/qless/pool/cli.rb', line 20 def opts = Trollop:: do version "qless-pool #{VERSION} (c) nicholas a. evans" <<-EOS qless-pool is the best way to manage a group (pool) of qless workers When daemonized, stdout and stderr default to qless-pool.stdxxx.log files in the log directory and pidfile defaults to qless-pool.pid in the current dir. Usage: qless-pool [options] where [options] are: EOS opt :config, "Alternate path to config file", :type => String, :short => "-c" opt :appname, "Alternate appname", :type => String, :short => "-a" opt :daemon, "Run as a background daemon", :default => false, :short => "-d" opt :stdout, "Redirect stdout to logfile", :type => String, :short => '-o' opt :stderr, "Redirect stderr to logfile", :type => String, :short => '-e' opt :nosync, "Don't sync logfiles on every write" opt :pidfile, "PID file location", :type => String, :short => "-p" opt :environment, "Set RAILS_ENV/RACK_ENV/QLESS_ENV", :type => String, :short => "-E" opt :term_graceful_wait, "On TERM signal, wait for workers to shut down gracefully" opt :term_graceful, "On TERM signal, shut down workers gracefully" opt :term_immediate, "On TERM signal, shut down workers immediately (default)" end if opts[:daemon] opts[:stdout] ||= "log/qless-pool.stdout.log" opts[:stderr] ||= "log/qless-pool.stderr.log" opts[:pidfile] ||= "tmp/pids/qless-pool.pid" end opts end |
#process_still_running?(pidfile) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/qless/pool/cli.rb', line 83 def process_still_running?(pidfile) old_pid = open(pidfile).read.strip.to_i Process.kill 0, old_pid true rescue Errno::ESRCH false rescue Errno::EPERM true rescue ::Exception => e $stderr.puts "While checking if PID #{old_pid} is running, unexpected #{e.class}: #{e}" true end |
#redirect(opts) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/qless/pool/cli.rb', line 96 def redirect(opts) $stdin.reopen '/dev/null' if opts[:daemon] # need to reopen as File, or else Qless::Pool::Logging.reopen_logs! won't work out = File.new(opts[:stdout], "a") if opts[:stdout] && !opts[:stdout].empty? err = File.new(opts[:stderr], "a") if opts[:stderr] && !opts[:stderr].empty? $stdout.reopen out if out $stderr.reopen err if err $stdout.sync = $stderr.sync = true unless opts[:nosync] end |
#run ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/qless/pool/cli.rb', line 10 def run opts = daemonize if opts[:daemon] manage_pidfile opts[:pidfile] redirect opts setup_environment opts opts start_pool end |
#set_pool_options(opts) ⇒ Object
TODO: global variables are not the best way
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/qless/pool/cli.rb', line 107 def (opts) if opts[:daemon] Qless::Pool.handle_winch = true end if opts[:term_graceful_wait] Qless::Pool.term_behavior = "graceful_worker_shutdown_and_wait" elsif opts[:term_graceful] Qless::Pool.term_behavior = "graceful_worker_shutdown" end end |
#setup_environment(opts) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/qless/pool/cli.rb', line 118 def setup_environment(opts) Qless::Pool.app_name = opts[:appname] if opts[:appname] ENV["RAILS_ENV"] ||= 'development' ENV["RACK_ENV"] = ENV["RAILS_ENV"] = ENV["QLESS_ENV"] = opts[:environment] if opts[:environment] Qless::Pool.log "Qless Pool running in #{ENV["RAILS_ENV"]} environment" ENV["QLESS_POOL_CONFIG"] = opts[:config] if opts[:config] end |
#start_pool ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/qless/pool/cli.rb', line 126 def start_pool require 'rake' require 'qless/pool/tasks' Rake.application.init Rake.application.load_rakefile Rake.application["qless:pool"].invoke end |