lib/resque/scheduler.rb in resque-scheduler-2.0.1 vs lib/resque/scheduler.rb in resque-scheduler-2.1.0
- old
+ new
@@ -1,46 +1,60 @@
require 'rufus/scheduler'
-require 'thwait'
require 'resque/scheduler_locking'
+require 'resque_scheduler/logger_builder'
module Resque
class Scheduler
- extend Resque::Helpers
extend Resque::SchedulerLocking
class << self
# If true, logs more stuff...
attr_accessor :verbose
# If set, produces no output
attr_accessor :mute
+ # If set, will write messages to the file
+ attr_accessor :logfile
+
# If set, will try to update the schedule in the loop
attr_accessor :dynamic
# Amount of time in seconds to sleep between polls of the delayed
# queue. Defaults to 5
attr_writer :poll_sleep_amount
+ attr_writer :logger
+
# the Rufus::Scheduler jobs that are scheduled
def scheduled_jobs
@@scheduled_jobs
end
def poll_sleep_amount
@poll_sleep_amount ||= 5 # seconds
end
+ def logger
+ @logger ||= ResqueScheduler::LoggerBuilder.new(:mute => mute, :verbose => verbose, :log_dev => logfile).build
+ end
+
# Schedule all jobs and continually look for delayed jobs (never returns)
def run
$0 = "resque-scheduler: Starting"
# trap signals
register_signal_handlers
+ # Quote from the resque/worker.
+ # Fix buffering so we can `rake resque:scheduler > scheduler.log` and
+ # get output from the child in there.
+ $stdout.sync = true
+ $stderr.sync = true
+
# Load the schedule into rufus
# If dynamic is set, load that schedule otherwise use normal load
if dynamic
reload_schedule!
else
@@ -60,12 +74,12 @@
poll_sleep
end
# never gets here.
end
-
+
# For all signals, set the shutdown flag and wait for current
# poll/enqueing to finish (should be almost istant). In the
# case of sleeping, exit immediately.
def register_signal_handlers
trap("TERM") { shutdown }
@@ -203,21 +217,21 @@
# Enqueues a job based on a config hash
def enqueue_from_config(job_config)
args = job_config['args'] || job_config[:args]
klass_name = job_config['class'] || job_config[:class]
- klass = constantize(klass_name) rescue klass_name
+ klass = Resque.constantize(klass_name) rescue klass_name
params = args.is_a?(Hash) ? [args] : Array(args)
queue = job_config['queue'] || job_config[:queue] || Resque.queue_from_class(klass)
# Support custom job classes like those that inherit from Resque::JobWithStatus (resque-status)
if (job_klass = job_config['custom_job_class']) && (job_klass != 'Resque::Job')
# The custom job class API must offer a static "scheduled" method. If the custom
# job class can not be constantized (via a requeue call from the web perhaps), fall
# back to enqueing normally via Resque::Job.create.
begin
- constantize(job_klass).scheduled(queue, klass_name, *params)
+ Resque.constantize(job_klass).scheduled(queue, klass_name, *params)
rescue NameError
# Note that the custom job class (job_config['custom_job_class']) is the one enqueued
Resque::Job.create(queue, job_klass, *params)
end
else
@@ -295,21 +309,20 @@
# Sets the shutdown flag, exits if sleeping
def shutdown
@shutdown = true
if @sleeping
- release_master_lock!
+ Thread.new { release_master_lock! }
exit
end
end
def log!(msg)
- puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} #{msg}" unless mute
+ logger.info msg
end
def log(msg)
- # add "verbose" logic later
- log!(msg) if verbose
+ logger.debug msg
end
def procline(string)
log! string
$0 = "resque-scheduler-#{ResqueScheduler::VERSION}: #{string}"