diff --git a/lib/crono.rb b/lib/crono.rb index 2c4565b..0b0a05e 100644 --- a/lib/crono.rb +++ b/lib/crono.rb @@ -3,6 +3,7 @@ end require "active_support/all" require "crono/version.rb" +require "crono/logging.rb" require "crono/period.rb" require "crono/job.rb" require "crono/scheduler.rb" diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index 50580c4..db714c3 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -3,12 +3,12 @@ require 'optparse' module Crono mattr_accessor :scheduler - mattr_accessor :logger class CLI include Singleton + include Logging + attr_accessor :config - attr_accessor :logger def initialize self.config = Config.new @@ -17,11 +17,18 @@ module Crono def run parse_options(ARGV) - init_logger - daemonize if config.daemonize + + if config.daemonize + set_log_to(config.logfile) + daemonize + else + set_log_to(STDOUT) + end + write_pid load_rails print_banner + start_working_loop end @@ -33,6 +40,7 @@ module Crono File.open(config.logfile, 'ab') { |f| io.reopen(f) } io.sync = true end + $stdin.reopen("/dev/null") end @@ -41,14 +49,10 @@ module Crono File.write(pidfile, ::Process.pid) end - def init_logger - logfile = config.daemonize ? config.logfile : STDOUT - Crono.logger = self.logger = Logger.new(logfile) - end - def print_banner logger.info "Loading Crono #{Crono::VERSION}" logger.info "Running in #{RUBY_DESCRIPTION}" + logger.info "Jobs:" Crono.scheduler.jobs.each do |job| logger.info job.description diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 8d9ddb2..4d848b6 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -1,5 +1,7 @@ module Crono class Job + include Logging + attr_accessor :performer attr_accessor :period attr_accessor :last_performed_at @@ -17,11 +19,11 @@ module Crono end def perform - Crono.logger.info "Perform #{performer}" + logger.info "Perform #{performer}" self.last_performed_at = Time.now Thread.new do performer.new.perform - Crono.logger.info "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) + logger.info "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) end end end diff --git a/lib/crono/logging.rb b/lib/crono/logging.rb new file mode 100644 index 0000000..1b94f5f --- /dev/null +++ b/lib/crono/logging.rb @@ -0,0 +1,13 @@ +module Crono + mattr_accessor :logger + + module Logging + def set_log_to(logfile) + Crono.logger = Logger.new(logfile) + end + + def logger + Crono.logger + end + end +end diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index 707544d..333081b 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -7,7 +7,7 @@ module Crono def every(period, *args) job = Job.new(@performer, Period.new(period, *args)) - @scheduler.add(job) + @scheduler.add_job(job) end end diff --git a/lib/crono/scheduler.rb b/lib/crono/scheduler.rb index 3b297f1..3a71b70 100644 --- a/lib/crono/scheduler.rb +++ b/lib/crono/scheduler.rb @@ -6,7 +6,7 @@ module Crono self.jobs = [] end - def add(job) + def add_job(job) jobs << job end @@ -16,7 +16,7 @@ module Crono private def queue - jobs.sort { |a,b| a.next <=> b.next } + jobs.sort_by(&:next) end end end diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index 5d9630c..e0d0673 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -6,7 +6,7 @@ end describe Crono::PerformerProxy do it "should add job to schedule" do - expect(Crono.scheduler).to receive(:add).with(kind_of(Crono::Job)) + expect(Crono.scheduler).to receive(:add_job).with(kind_of(Crono::Job)) Crono.perform(TestJob).every(2.days, at: "15:30") end end