Skip to content

Commit

Permalink
Collapsing lib/resque_scheduler into lib/resque/scheduler
Browse files Browse the repository at this point in the history
plus oodles of other changes related to the move, wheeeee!
  • Loading branch information
meatballhat committed Feb 18, 2014
1 parent e0e91aa commit 8aa1e67
Show file tree
Hide file tree
Showing 44 changed files with 1,012 additions and 984 deletions.
13 changes: 7 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
.bundle/
.idea/
/.bundle/
/.idea/
/.yardoc/

doc/
pkg
/doc/
/pkg/
nbproject
Gemfile.lock
.rvmrc
/Gemfile.lock
/.rvmrc
*.swp

/coverage/
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ gem 'resque-scheduler'
Adding the resque:scheduler rake task:

```ruby
require 'resque_scheduler/tasks'
require 'resque/scheduler/tasks'
```

### Rake integration
Expand All @@ -66,12 +66,12 @@ everything `resque` needs to know.
```ruby
# Resque tasks
require 'resque/tasks'
require 'resque_scheduler/tasks'
require 'resque/scheduler/tasks'

namespace :resque do
task :setup do
require 'resque'
require 'resque_scheduler'
require 'resque-scheduler'

# you probably already have this somewhere
Resque.redis = 'localhost:6379'
Expand Down Expand Up @@ -448,7 +448,7 @@ redis instance and schedule. The scheduler processes will use redis to
elect a master process and detect failover when the master dies. Precautions are
taken to prevent jobs from potentially being queued twice during failover even
when the clocks of the scheduler machines are slightly out of sync (or load affects
scheduled job firing time). If you want the gory details, look at Resque::SchedulerLocking.
scheduled job firing time). If you want the gory details, look at Resque::Scheduler::Locking.

If the scheduler process(es) goes down for whatever reason, the delayed items
that should have fired during the outage will fire once the scheduler process
Expand Down Expand Up @@ -495,8 +495,8 @@ Now, you want to add the following:

```ruby
# This will make the tabs show up.
require 'resque_scheduler'
require 'resque_scheduler/server'
require 'resque-scheduler'
require 'resque/scheduler/server'
```

That should make the scheduler tabs show up in `resque-web`.
Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'rubocop/rake_task'
require 'yard'

task default: [:rubocop, :test]

Expand All @@ -15,3 +16,5 @@ Rake::TestTask.new do |t|
o << '--verbose ' if ENV['VERBOSE']
end
end

YARD::Rake::YardocTask.new
4 changes: 2 additions & 2 deletions bin/resque-scheduler
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
# vim:fileencoding=utf-8

require 'resque_scheduler'
ResqueScheduler::Cli.run!
require 'resque-scheduler'
Resque::Scheduler::Cli.run!
2 changes: 1 addition & 1 deletion examples/Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# vim:fileencoding=utf-8
require 'resque_scheduler/tasks'
require 'resque/scheduler/tasks'
4 changes: 2 additions & 2 deletions examples/config/initializers/resque-web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
redis_env_var = ENV['REDIS_PROVIDER'] || 'REDIS_URL'
Resque.redis = ENV[redis_env_var] || 'localhost:6379'

require 'resque_scheduler'
require 'resque_scheduler/server'
require 'resque-scheduler'
require 'resque/scheduler/server'

schedule_yml = ENV['RESQUE_SCHEDULE_YML']
if schedule_yml
Expand Down
4 changes: 2 additions & 2 deletions examples/dynamic-scheduling/lib/tasks/resque.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# vim:fileencoding=utf-8

require 'resque/tasks'
require 'resque_scheduler/tasks'
require 'resque/scheduler/tasks'
require 'yaml'

namespace :resque do
task :setup do
require 'resque'
require 'resque_scheduler'
require 'resque-scheduler'

rails_root = ENV['RAILS_ROOT'] || File.expand_path('../../../', __FILE__)
rails_env = ENV['RAILS_ENV'] || 'development'
Expand Down
4 changes: 3 additions & 1 deletion lib/resque-scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# vim:fileencoding=utf-8
require File.expand_path('../resque_scheduler', __FILE__)
require_relative 'resque/scheduler'

Resque.extend Resque::Scheduler::Extension
25 changes: 14 additions & 11 deletions lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# vim:fileencoding=utf-8
require 'rufus/scheduler'
require 'resque/scheduler_locking'
require 'resque_scheduler/logger_builder'
require_relative 'scheduler/locking'
require_relative 'scheduler/logger_builder'

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

extend Resque::Scheduler::Locking

class << self
# Allows for block-style configuration
Expand Down Expand Up @@ -83,7 +86,7 @@ def poll_sleep_amount
attr_writer :logger

def logger
@logger ||= ResqueScheduler::LoggerBuilder.new(
@logger ||= Resque::Scheduler::LoggerBuilder.new(
mute: mute,
verbose: verbose,
log_dev: logfile,
Expand Down Expand Up @@ -202,8 +205,8 @@ def optionizate_interval_value(value)
args
end

# Loads a job schedule into the Rufus::Scheduler and stores it in
# @scheduled_jobs
# Loads a job schedule into the Rufus::Scheduler and stores it
# in @scheduled_jobs
def load_schedule_job(name, config)
# If `rails_env` or `env` is set in the config, load jobs only if they
# are meant to be loaded in `Resque::Scheduler.env`. If `rails_env` or
Expand Down Expand Up @@ -306,7 +309,7 @@ def enqueue_from_config(job_config)

klass_name = job_config['class'] || job_config[:class]
begin
klass = ResqueScheduler::Util.constantize(klass_name)
klass = Resque::Scheduler::Util.constantize(klass_name)
rescue NameError
klass = klass_name
end
Expand All @@ -324,7 +327,7 @@ def enqueue_from_config(job_config)
# from the web perhaps), fall back to enqueing normally via
# Resque::Job.create.
begin
ResqueScheduler::Util.constantize(job_klass).scheduled(
Resque::Scheduler::Util.constantize(job_klass).scheduled(
queue, klass_name, *params
)
rescue NameError
Expand All @@ -337,7 +340,7 @@ def enqueue_from_config(job_config)
# for non-existent classes (for example: running scheduler in
# one app that schedules for another.
if Class === klass
ResqueScheduler::Plugin.run_before_delayed_enqueue_hooks(
Resque::Scheduler::Plugin.run_before_delayed_enqueue_hooks(
klass, *params
)

Expand Down Expand Up @@ -474,7 +477,7 @@ def build_procline(string)
end

def internal_name
"resque-scheduler-#{ResqueScheduler::VERSION}"
"resque-scheduler-#{Resque::Scheduler::VERSION}"
end
end
end
Expand Down
164 changes: 164 additions & 0 deletions lib/resque/scheduler/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# vim:fileencoding=utf-8

require 'optparse'

module Resque
module Scheduler
class Cli
BANNER = <<-EOF.gsub(/ {6}/, '')
Usage: resque-scheduler [options]
Runs a resque scheduler process directly (rather than via rake).
EOF
OPTIONS = [
{
args: ['-n', '--app-name [APP_NAME]',
'Application name for procline'],
callback: ->(options) { ->(n) { options[:app_name] = n } }
},
{
args: ['-B', '--background', 'Run in the background [BACKGROUND]'],
callback: ->(options) { ->(b) { options[:background] = b } }
},
{
args: ['-D', '--dynamic-schedule',
'Enable dynamic scheduling [DYNAMIC_SCHEDULE]'],
callback: ->(options) { ->(d) { options[:dynamic] = d } }
},
{
args: ['-E', '--environment [RAILS_ENV]', 'Environment name'],
callback: ->(options) { ->(e) { options[:env] = e } }
},
{
args: ['-I', '--initializer-path [INITIALIZER_PATH]',
'Path to optional initializer ruby file'],
callback: ->(options) { ->(i) { options[:initializer_path] = i } }
},
{
args: ['-i', '--interval [RESQUE_SCHEDULER_INTERVAL]',
'Interval for checking if a scheduled job must run'],
callback: ->(options) { ->(i) { options[:poll_sleep_amount] = i } }
},
{
args: ['-l', '--logfile [LOGFILE]', 'Log file name'],
callback: ->(options) { ->(l) { options[:logfile] = l } }
},
{
args: ['-F', '--logformat [LOGFORMAT]', 'Log output format'],
callback: ->(options) { ->(f) { options[:logformat] = f } }
},
{
args: ['-P', '--pidfile [PIDFILE]', 'PID file name'],
callback: ->(options) { ->(p) { options[:pidfile] = p } }
},
{
args: ['-q', '--quiet',
'Run with minimal output [QUIET] (or [MUTE])'],
callback: ->(options) { ->(q) { options[:mute] = q } }
},
{
args: ['-v', '--verbose', 'Run with verbose output [VERBOSE]'],
callback: ->(options) { ->(v) { options[:verbose] = v } }
}
].freeze

def self.run!(argv = ARGV, env = ENV)
new(argv, env).run!
end

def initialize(argv = ARGV, env = ENV)
@argv = argv
@env = env
end

def run!
pre_run
run_forever
end

def pre_run
parse_options
pre_setup
setup_env
end

def parse_options
OptionParser.new do |opts|
opts.banner = BANNER
OPTIONS.each do |opt|
opts.on(*opt[:args], &(opt[:callback].call(options)))
end
end.parse!(argv.dup)
end

def pre_setup
if options[:initializer_path]
load options[:initializer_path].to_s.strip
else
false
end
end

def setup_env
require 'resque'
require 'resque/scheduler'

# Need to set this here for conditional Process.daemon redirect of
# stderr/stdout to /dev/null
Resque::Scheduler.mute = !!options[:mute]

if options[:background]
unless Process.respond_to?('daemon')
abort 'background option is set, which requires ruby >= 1.9'
end

Process.daemon(true, !Resque::Scheduler.mute)
Resque.redis.client.reconnect
end

File.open(options[:pidfile], 'w') do |f|
f.puts $PROCESS_ID
end if options[:pidfile]

Resque::Scheduler.configure do |c|
# These settings are somewhat redundant given the defaults present
# in the attr reader methods. They are left here for clarity and
# to serve as an example of how to use `.configure`.

c.app_name = options[:app_name]
c.dynamic = !!options[:dynamic]
c.env = options[:env]
c.logfile = options[:logfile]
c.logformat = options[:logformat]
c.poll_sleep_amount = Float(options[:poll_sleep_amount] || '5')
c.verbose = !!options[:verbose]
end
end

def run_forever
Resque::Scheduler.run
end

private

attr_reader :argv, :env

def options
@options ||= {
app_name: env['APP_NAME'],
background: env['BACKGROUND'],
dynamic: env['DYNAMIC_SCHEDULE'],
env: env['RAILS_ENV'],
initializer_path: env['INITIALIZER_PATH'],
logfile: env['LOGFILE'],
logformat: env['LOGFORMAT'],
mute: env['MUTE'] || env['QUIET'],
pidfile: env['PIDFILE'],
poll_sleep_amount: env['RESQUE_SCHEDULER_INTERVAL'],
verbose: env['VERBOSE']
}
end
end
end
end
Loading

0 comments on commit 8aa1e67

Please sign in to comment.