mirror of
https://github.com/plashchynski/crono.git
synced 2026-03-21 08:59:36 +01:00
add start|stop|restart|run to crono executable
this is done using daemonize gem, but it is not breaking the old interface. But sets a deprected comment to the old one daemonize process.
This commit is contained in:
@@ -7,6 +7,8 @@ module Crono
|
||||
include Singleton
|
||||
include Logging
|
||||
|
||||
COMMANDS = %w(start stop restart run zap reload status)
|
||||
|
||||
attr_accessor :config
|
||||
|
||||
def initialize
|
||||
@@ -16,16 +18,21 @@ module Crono
|
||||
|
||||
def run
|
||||
parse_options(ARGV)
|
||||
parse_command(ARGV)
|
||||
|
||||
setup_log
|
||||
setup_log
|
||||
|
||||
write_pid
|
||||
write_pid unless config.daemonize
|
||||
load_rails
|
||||
Cronotab.process(File.expand_path(config.cronotab))
|
||||
print_banner
|
||||
|
||||
check_jobs
|
||||
start_working_loop
|
||||
if config.daemonize
|
||||
start_working_loop_in_daemon
|
||||
else
|
||||
start_working_loop
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
@@ -33,13 +40,15 @@ module Crono
|
||||
def setup_log
|
||||
if config.daemonize
|
||||
self.logfile = config.logfile
|
||||
daemonize
|
||||
elsif config.deprecated_daemonize
|
||||
self.logfile = config.logfile
|
||||
deprecated_daemonize
|
||||
else
|
||||
self.logfile = STDOUT
|
||||
end
|
||||
end
|
||||
|
||||
def daemonize
|
||||
def deprecated_daemonize
|
||||
::Process.daemon(true, true)
|
||||
|
||||
[$stdout, $stderr].each do |io|
|
||||
@@ -71,7 +80,7 @@ module Crono
|
||||
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = config.environment
|
||||
require 'rails'
|
||||
require File.expand_path('config/environment.rb')
|
||||
::Rails.application.eager_load!
|
||||
::Rails.application.eager_load! if config.daemonize
|
||||
end
|
||||
|
||||
def check_jobs
|
||||
@@ -79,6 +88,30 @@ module Crono
|
||||
logger.error "You have no jobs in you cronotab file #{config.cronotab}"
|
||||
end
|
||||
|
||||
def start_working_loop_in_daemon
|
||||
unless ENV['RAILS_ENV'] == 'test'
|
||||
begin
|
||||
require 'daemons'
|
||||
rescue LoadError
|
||||
raise "You need to add gem 'daemons' to your Gemfile if you wish to use it."
|
||||
end
|
||||
end
|
||||
Daemons.run_proc(config.process_name, dir: config.piddir, dir_mode: :normal, monitor: config.monitor, ARGV: @argv) do |*_argv|
|
||||
Dir.chdir(root)
|
||||
Crono.logger = Logger.new(config.logfile)
|
||||
|
||||
start_working_loop
|
||||
end
|
||||
end
|
||||
|
||||
def root
|
||||
@root ||= rails_root_defined? ? ::Rails.root : DIR_PWD
|
||||
end
|
||||
|
||||
def rails_root_defined?
|
||||
defined?(::Rails.root)
|
||||
end
|
||||
|
||||
def start_working_loop
|
||||
loop do
|
||||
next_time, jobs = Crono.scheduler.next_jobs
|
||||
@@ -88,8 +121,8 @@ module Crono
|
||||
end
|
||||
|
||||
def parse_options(argv)
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = "Usage: crono [options]"
|
||||
@argv = OptionParser.new do |opts|
|
||||
opts.banner = "Usage: crono [options] start|stop|restart|run"
|
||||
|
||||
opts.on("-C", "--cronotab PATH", "Path to cronotab file (Default: #{config.cronotab})") do |cronotab|
|
||||
config.cronotab = cronotab
|
||||
@@ -103,8 +136,20 @@ module Crono
|
||||
config.pidfile = pidfile
|
||||
end
|
||||
|
||||
opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize|
|
||||
config.daemonize = daemonize
|
||||
opts.on("--piddir PATH", "Path to piddir (Default: #{config.piddir})") do |piddir|
|
||||
config.piddir = piddir
|
||||
end
|
||||
|
||||
opts.on("-N", "--process_name name", "Name of the process (Default: #{config.process_name})") do |process_name|
|
||||
config.process_name = process_name
|
||||
end
|
||||
|
||||
opts.on("-d", "--[no-]daemonize", "Deprecated! Instead use crono [start|stop|restart] without this option; Daemonize process (Default: #{config.daemonize})") do |daemonize|
|
||||
config.deprecated_daemonize = daemonize
|
||||
end
|
||||
|
||||
opts.on("-m", "--monitor", "Start monitor process for a deamon (Default #{config.monitor})") do
|
||||
config.monitor = true
|
||||
end
|
||||
|
||||
opts.on '-e', '--environment ENV', "Application environment (Default: #{config.environment})" do |env|
|
||||
@@ -112,5 +157,12 @@ module Crono
|
||||
end
|
||||
end.parse!(argv)
|
||||
end
|
||||
|
||||
def parse_command(argv)
|
||||
if COMMANDS.include? argv[0]
|
||||
config.daemonize = true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,18 +4,31 @@ module Crono
|
||||
CRONOTAB = 'config/cronotab.rb'
|
||||
LOGFILE = 'log/crono.log'
|
||||
PIDFILE = 'tmp/pids/crono.pid'
|
||||
PIDDIR = 'tmp/pids'
|
||||
PROCESS_NAME = 'crono'
|
||||
|
||||
attr_accessor :cronotab, :logfile, :pidfile, :daemonize, :environment
|
||||
attr_accessor :cronotab, :logfile, :pidfile, :piddir, :process_name,
|
||||
:monitor, :daemonize, :deprecated_daemonize, :environment
|
||||
|
||||
def initialize
|
||||
self.cronotab = CRONOTAB
|
||||
self.logfile = LOGFILE
|
||||
self.piddir = PIDDIR
|
||||
self.process_name = PROCESS_NAME
|
||||
self.daemonize = false
|
||||
self.deprecated_daemonize = false
|
||||
self.monitor = false
|
||||
self.environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
||||
end
|
||||
|
||||
def pidfile=(pidfile)
|
||||
@pidfile = pidfile
|
||||
self.process_name = Pathname.new(pidfile).basename(".*").to_s
|
||||
self.piddir = Pathname.new(pidfile).dirname.to_s
|
||||
end
|
||||
|
||||
def pidfile
|
||||
@pidfile || (daemonize ? PIDFILE : nil)
|
||||
@pidfile || (deprecated_daemonize ? PIDFILE : nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user