Skip to content

Commit d3301bb

Browse files
committed
* added auto_load option to allow jobs to declare theirs schedule
1 parent e17ad8c commit d3301bb

17 files changed

+256
-19
lines changed

.rubocop.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ Style/DoubleNegation:
1414
Enabled: false
1515
Metrics/PerceivedComplexity:
1616
Enabled: false
17-
Metrics/ClassLength:
18-
Max: 110

.rubocop_todo.yml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2016-06-26 12:36:38 -0400 using RuboCop version 0.40.0.
3+
# on 2021-07-10 20:06:41 +0000 using RuboCop version 0.40.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: AllowSafeAssignment.
11-
Lint/AssignmentInCondition:
12-
Exclude:
13-
- 'lib/resque/scheduler/env.rb'
14-
159
# Offense count: 2
1610
Lint/UselessAccessModifier:
1711
Exclude:
1812
- 'lib/resque/scheduler.rb'
1913

20-
# Offense count: 17
14+
# Offense count: 18
2115
Metrics/AbcSize:
22-
Max: 36
16+
Max: 41
2317

24-
# Offense count: 3
18+
# Offense count: 1
19+
# Configuration parameters: CountComments.
20+
Metrics/ClassLength:
21+
Max: 112
22+
23+
# Offense count: 5
2524
Metrics/CyclomaticComplexity:
2625
Max: 12
2726

28-
# Offense count: 6
27+
# Offense count: 11
2928
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
3029
# URISchemes: http, https
3130
Metrics/LineLength:
3231
Max: 96
3332

34-
# Offense count: 20
33+
# Offense count: 23
3534
# Configuration parameters: CountComments.
3635
Metrics/MethodLength:
3736
Max: 34
3837

3938
# Offense count: 2
4039
# Configuration parameters: CountComments.
4140
Metrics/ModuleLength:
42-
Max: 331
41+
Max: 350
4342

4443
# Offense count: 1
4544
Style/CaseEquality:

AUTHORS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Resque Scheduler authors
44
- Aaron Suggs
55
- Alexander Simonov
66
- Andrea Campolonghi
7+
- Andrea Lorenzetti
78
- Ben VandenBos
89
- Bernerd Schaefer
910
- Bogdan Gusiev
@@ -85,4 +86,5 @@ Resque Scheduler authors
8586
- malomalo
8687
- sawanoboly
8788
- serek
88-
- iloveitaly
89+
- iloveitaly
90+
- Andrea Lorenzetti

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ Resque.schedule = YAML.load_file('your_resque_schedule.yml')
271271

272272
If a static schedule is not set `resque-scheduler` will issue a "Schedule empty!" warning on
273273
startup, but despite that warning setting a static schedule is totally optional. It is possible
274-
to use only dynamic schedules (see below).
274+
to use only dynamic schedules or auto load (see below).
275275

276276
The schedule file is a list of Resque job classes with arguments and a
277277
schedule frequency (in crontab syntax). The schedule is just a hash, but
@@ -403,6 +403,50 @@ config[:every] = '1d'
403403
Resque.set_schedule(name, config)
404404
```
405405

406+
#### Auto load
407+
408+
With auto load you specify a path from which jobs will be loaded and scheduled without the needs of static scheduling or dynamic scheduling.
409+
410+
Auto load are not enabled by default. To be able to auto load set schedules, you must pass the following to resque-scheduler initialization (see Installation above for a more complete example):
411+
412+
```ruby
413+
Resque::Scheduler.auto_load = 'path/to/*_job.rb'
414+
```
415+
416+
Auto load enables a job to declare it's scheduling. In order to do that file must follow `snake_case` convention for filename and `CamelCase` for class name. It also must include `Resque::Scheduler::Job` and declares it's schedule:
417+
418+
```ruby
419+
cron '*/2 * * * *'
420+
queue 'default'
421+
```
422+
423+
All options available:
424+
425+
```ruby
426+
cron '* */3 * * *' # use cron or every option, don't use both
427+
every '3d' # use every or cron option, don't use both
428+
queue 'default'
429+
args 'custom arg'
430+
description 'Nice description'
431+
```
432+
433+
Job's example:
434+
435+
```ruby
436+
# my_great_job.rb
437+
require 'resque/scheduler/job'
438+
439+
class MyGreatJob
440+
include Resque::Scheduler::Job
441+
442+
cron '*/2 * * * *'
443+
queue 'default'
444+
args 'args'
445+
description 'description'
446+
end
447+
448+
```
449+
406450
#### Time zones
407451

408452
Note that if you use the cron syntax, this will be interpreted as in the server time zone

lib/resque/scheduler.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ def load_schedule!
107107
Resque.schedule.each do |name, config|
108108
load_schedule_job(name, config)
109109
end
110+
111+
Dir[auto_load.to_s].each do |file|
112+
require File.absolute_path(file)
113+
name = File.basename(file, '.rb')
114+
begin
115+
klass = Resque::Scheduler::Util.constantize(name)
116+
rescue NameError
117+
log! "Can't load file #{file}"
118+
end
119+
load_schedule_job(
120+
name,
121+
'class' => klass.name,
122+
'cron' => klass.respond_to?(:cron) ? klass.cron : nil,
123+
'every' => klass.respond_to?(:every) ? klass.every : nil,
124+
'queue' => klass.respond_to?(:queue) ? klass.queue : nil,
125+
'args' => klass.respond_to?(:args) ? klass.args : nil,
126+
'description' => klass.respond_to?(:description) ? klass.description : nil
127+
) if klass
128+
end
129+
110130
Resque.redis.del(:schedules_changed) if am_master && dynamic
111131
procline 'Schedules Loaded'
112132
end

lib/resque/scheduler/cli.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Scheduler
88
app_name: 'APP_NAME',
99
background: 'BACKGROUND',
1010
dynamic: 'DYNAMIC_SCHEDULE',
11+
auto_load: 'AUTO_LOAD',
1112
env: 'RAILS_ENV',
1213
initializer_path: 'INITIALIZER_PATH',
1314
logfile: 'LOGFILE',
@@ -31,6 +32,10 @@ class Cli
3132
'Application name for procline'],
3233
callback: ->(options) { ->(n) { options[:app_name] = n } }
3334
},
35+
{
36+
args: ['-A', '--auto-load [AUTO_LOAD]', 'Enable jobs auto load'],
37+
callback: ->(options) { ->(a) { options[:auto_load] = a } }
38+
},
3439
{
3540
args: ['-B', '--background', 'Run in the background [BACKGROUND]'],
3641
callback: ->(options) { ->(b) { options[:background] = b } }

lib/resque/scheduler/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def dynamic
5353
@dynamic ||= !!ENV['DYNAMIC_SCHEDULE']
5454
end
5555

56+
# If set, will try to automatically load those jobs
57+
attr_writer :auto_load
58+
59+
def auto_load
60+
@auto_load ||= !!ENV['AUTO_LOAD']
61+
end
62+
5663
# If set, will append the app name to procline
5764
attr_writer :app_name
5865

lib/resque/scheduler/env.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def setup_scheduler_configuration
6464

6565
c.dynamic = !!options[:dynamic] if options.key?(:dynamic)
6666

67+
c.auto_load = options[:auto_load] if options.key?(:auto_load)
68+
6769
c.env = options[:env] if options.key?(:env)
6870

6971
c.logfile = options[:logfile] if options.key?(:logfile)

lib/resque/scheduler/job.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# vim:fileencoding=utf-8
2+
3+
module Resque
4+
module Scheduler
5+
module Job
6+
class << self
7+
def included(base)
8+
base.extend ClassMethods
9+
end
10+
end
11+
12+
module ClassMethods
13+
def cron(value = nil)
14+
return @cron ||= nil if value.nil?
15+
@cron = value
16+
end
17+
18+
def every(value = nil)
19+
return @every ||= nil if value.nil?
20+
@every = value
21+
end
22+
23+
def queue(value = nil)
24+
return @queue ||= nil if value.nil?
25+
@queue = value
26+
end
27+
28+
def args(value = nil)
29+
return @args ||= nil if value.nil?
30+
@args = value
31+
end
32+
33+
def description(value = nil)
34+
return @description ||= nil if value.nil?
35+
@description = value
36+
end
37+
end
38+
end
39+
end
40+
end

lib/resque/scheduler/util.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class Util
88
# Scheduler. refer to:
99
# https://github.com/resque/resque-scheduler/pull/273
1010

11+
CLASSIFY_DELIMETERS = %w(- _).freeze
12+
1113
def self.constantize(camel_cased_word)
1214
camel_cased_word = camel_cased_word.to_s
1315

14-
if camel_cased_word.include?('-')
16+
unless (camel_cased_word.chars & CLASSIFY_DELIMETERS).empty?
1517
camel_cased_word = classify(camel_cased_word)
1618
end
1719

@@ -32,7 +34,12 @@ def self.constantize(camel_cased_word)
3234
end
3335

3436
def self.classify(dashed_word)
35-
dashed_word.split('-').map(&:capitalize).join
37+
CLASSIFY_DELIMETERS.each do |delimiter|
38+
dashed_word = dashed_word.split(delimiter)
39+
.map { |w| w[0].capitalize + w[1..-1] }
40+
.join
41+
end
42+
dashed_word
3643
end
3744
end
3845
end

0 commit comments

Comments
 (0)