Skip to content

Commit

Permalink
Extracting some of Resque::Scheduler's functionality into modules
Browse files Browse the repository at this point in the history
although this is admittedly not an awesome solution.  Needs more work.
  • Loading branch information
meatballhat committed Feb 18, 2014
1 parent 8aa1e67 commit 3525f40
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 125 deletions.
23 changes: 12 additions & 11 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2014-02-09 12:18:33 -0500 using RuboCop version 0.18.1.
# on 2014-02-12 01:55:22 -0500 using RuboCop version 0.18.1.
# The point is for the user to remove these configuration records
# one by one as the offences are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -9,30 +9,31 @@ AllCops:
Includes:
- Gemfile
- Rakefile
- '*.gemspec'
- 'bin/*'
- resque-scheduler.gemspec
- bin/resque-scheduler

# Offence count: 1
CaseEquality:
Enabled: false

# Offence count: 1
# Offence count: 2
# Configuration parameters: CountComments.
ClassLength:
Max: 341
Max: 130

# Offence count: 4
# Offence count: 3
CyclomaticComplexity:
Max: 24
Max: 21

# Offence count: 29
Documentation:
Enabled: false

# Offence count: 17
# Configuration parameters: CountComments.
MethodLength:
Max: 150
Max: 145

# Offence count: 1
RescueException:
Enabled: false

Documentation:
Enabled: false
125 changes: 20 additions & 105 deletions lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,99 +1,25 @@
# vim:fileencoding=utf-8

require 'rufus/scheduler'
require_relative 'scheduler/configuration'
require_relative 'scheduler/locking'
require_relative 'scheduler/logger_builder'
require_relative 'scheduler/signal_handling'

module Resque
module Scheduler
autoload :Cli, 'resque/scheduler/cli'
autoload :Extension, 'resque/scheduler/extension'

extend Resque::Scheduler::Locking

class << self
# Allows for block-style configuration
def configure
yield self
end

attr_writer :signal_queue

def signal_queue
@signal_queue ||= []
end

# Used in `#load_schedule_job`
attr_writer :env

def env
return @env if @env
@env ||= Rails.env if defined?(Rails)
@env ||= ENV['RAILS_ENV']
@env
end

# If true, logs more stuff...
attr_writer :verbose

def verbose
@verbose ||= !!ENV['VERBOSE']
end

# If set, produces no output
attr_writer :mute

def mute
@mute ||= !!ENV['MUTE']
end

# If set, will write messages to the file
attr_writer :logfile

def logfile
@logfile ||= ENV['LOGFILE']
end

# Sets whether to log in 'text' or 'json'
attr_writer :logformat

def logformat
@logformat ||= ENV['LOGFORMAT']
end

# If set, will try to update the schedule in the loop
attr_writer :dynamic

def dynamic
@dynamic ||= !!ENV['DYNAMIC_SCHEDULE']
end

# If set, will append the app name to procline
attr_writer :app_name

def app_name
@app_name ||= ENV['APP_NAME']
end

# Amount of time in seconds to sleep between polls of the delayed
# queue. Defaults to 5
attr_writer :poll_sleep_amount

def poll_sleep_amount
@poll_sleep_amount ||=
Float(ENV.fetch('RESQUE_SCHEDULER_INTERVAL', '5'))
end
private

attr_writer :logger
extend Resque::Scheduler::Locking
extend Resque::Scheduler::Configuration
extend Resque::Scheduler::SignalHandling

def logger
@logger ||= Resque::Scheduler::LoggerBuilder.new(
mute: mute,
verbose: verbose,
log_dev: logfile,
format: logformat
).build
end
public

class << self
# the Rufus::Scheduler jobs that are scheduled
attr_reader :scheduled_jobs

Expand Down Expand Up @@ -139,28 +65,6 @@ def run
end
end

# For all signals, set the shutdown flag and wait for current
# poll/enqueing to finish (should be almost instant). In the
# case of sleeping, exit immediately.
def register_signal_handlers
%w(INT TERM USR1 USR2 QUIT).each do |sig|
trap(sig) { signal_queue << sig }
end
end

def handle_signals
loop do
sig = signal_queue.shift
break unless sig
log! "Got #{sig} signal"
case sig
when 'INT', 'TERM', 'QUIT' then shutdown
when 'USR1' then print_schedule
when 'USR2' then reload_schedule!
end
end
end

def print_schedule
if rufus_scheduler
log! "Scheduling Info\tLast Run"
Expand Down Expand Up @@ -464,6 +368,17 @@ def procline(string)

private

attr_writer :logger

def logger
@logger ||= Resque::Scheduler::LoggerBuilder.new(
mute: mute,
verbose: verbose,
log_dev: logfile,
format: logformat
).build
end

def app_str
app_name ? "[#{app_name}]" : ''
end
Expand Down
73 changes: 73 additions & 0 deletions lib/resque/scheduler/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# vim:fileencoding=utf-8

module Resque
module Scheduler
module Configuration
# Allows for block-style configuration
def configure
yield self
end

# Used in `#load_schedule_job`
attr_writer :env

def env
return @env if @env
@env ||= Rails.env if defined?(Rails)
@env ||= ENV['RAILS_ENV']
@env
end

# If true, logs more stuff...
attr_writer :verbose

def verbose
@verbose ||= !!ENV['VERBOSE']
end

# If set, produces no output
attr_writer :mute

def mute
@mute ||= !!ENV['MUTE']
end

# If set, will write messages to the file
attr_writer :logfile

def logfile
@logfile ||= ENV['LOGFILE']
end

# Sets whether to log in 'text' or 'json'
attr_writer :logformat

def logformat
@logformat ||= ENV['LOGFORMAT']
end

# If set, will try to update the schedule in the loop
attr_writer :dynamic

def dynamic
@dynamic ||= !!ENV['DYNAMIC_SCHEDULE']
end

# If set, will append the app name to procline
attr_writer :app_name

def app_name
@app_name ||= ENV['APP_NAME']
end

# Amount of time in seconds to sleep between polls of the delayed
# queue. Defaults to 5
attr_writer :poll_sleep_amount

def poll_sleep_amount
@poll_sleep_amount ||=
Float(ENV.fetch('RESQUE_SCHEDULER_INTERVAL', '5'))
end
end
end
end
35 changes: 35 additions & 0 deletions lib/resque/scheduler/signal_handling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# vim:fileencoding=utf-8

module Resque
module Scheduler
module SignalHandling
attr_writer :signal_queue

def signal_queue
@signal_queue ||= []
end

# For all signals, set the shutdown flag and wait for current
# poll/enqueing to finish (should be almost instant). In the
# case of sleeping, exit immediately.
def register_signal_handlers
%w(INT TERM USR1 USR2 QUIT).each do |sig|
trap(sig) { signal_queue << sig }
end
end

def handle_signals
loop do
sig = signal_queue.shift
break unless sig
log! "Got #{sig} signal"
case sig
when 'INT', 'TERM', 'QUIT' then shutdown
when 'USR1' then print_schedule
when 'USR2' then reload_schedule!
end
end
end
end
end
end
18 changes: 10 additions & 8 deletions test/scheduler_setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

test 'set custom logger' do
custom_logger = Logger.new('/dev/null')
Resque::Scheduler.logger = custom_logger
assert_equal(custom_logger, Resque::Scheduler.logger)
Resque::Scheduler.send(:logger=, custom_logger)
assert_equal(custom_logger, Resque::Scheduler.send(:logger))
end

test 'configure block' do
Expand Down Expand Up @@ -58,16 +58,17 @@ def wipe

test 'uses STDOUT' do
assert_equal(
Resque::Scheduler.logger.instance_variable_get(:@logdev).dev, $stdout
Resque::Scheduler.send(:logger)
.instance_variable_get(:@logdev).dev, $stdout
)
end

test 'not verbose' do
assert Resque::Scheduler.logger.level > Logger::DEBUG
assert Resque::Scheduler.send(:logger).level > Logger::DEBUG
end

test 'not muted' do
assert Resque::Scheduler.logger.level < Logger::FATAL
assert Resque::Scheduler.send(:logger).level < Logger::FATAL
end
end

Expand All @@ -78,19 +79,20 @@ def wipe
test 'uses logfile' do
Resque::Scheduler.logfile = '/dev/null'
assert_equal(
Resque::Scheduler.logger.instance_variable_get(:@logdev).filename,
Resque::Scheduler.send(:logger)
.instance_variable_get(:@logdev).filename,
'/dev/null'
)
end

test 'set verbosity' do
Resque::Scheduler.verbose = true
assert Resque::Scheduler.logger.level == Logger::DEBUG
assert Resque::Scheduler.send(:logger).level == Logger::DEBUG
end

test 'mute logger' do
Resque::Scheduler.mute = true
assert Resque::Scheduler.logger.level == Logger::FATAL
assert Resque::Scheduler.send(:logger).level == Logger::FATAL
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def nullify_logger
c.mute = nil
c.verbose = nil
c.logfile = nil
c.logger = nil
c.send(:logger=, nil)
end

ENV['LOGFILE'] = nil
Expand Down

0 comments on commit 3525f40

Please sign in to comment.