From e1c9334a236b83e4bbd5af6bfa24176bd1ee6183 Mon Sep 17 00:00:00 2001 From: Andrea Lorenzetti Date: Mon, 1 Nov 2021 17:17:55 +0100 Subject: [PATCH] * updated documentation * removed auto_load fields in favour of resque_schedule method --- AUTHORS.md | 1 - README.md | 21 ++++++++++---------- lib/resque/scheduler.rb | 15 --------------- lib/resque/scheduler/job.rb | 33 ++++++++++---------------------- test/fixtures/valid_cron_job.rb | 7 +++---- test/fixtures/valid_every_job.rb | 7 +++---- test/job_test.rb | 33 -------------------------------- test/scheduler_test.rb | 6 +++--- 8 files changed, 29 insertions(+), 94 deletions(-) delete mode 100644 test/job_test.rb diff --git a/AUTHORS.md b/AUTHORS.md index bdc04403..c2a0ab9b 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -87,4 +87,3 @@ Resque Scheduler authors - sawanoboly - serek - iloveitaly -- Andrea Lorenzetti diff --git a/README.md b/README.md index 636131bf..60305541 100644 --- a/README.md +++ b/README.md @@ -416,18 +416,18 @@ Resque::Scheduler.auto_load = 'path/to/*_job.rb' 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: ```ruby - cron '*/2 * * * *' - queue 'default' +resque_schedule cron: '*/2 * * * *' ``` All options available: ```ruby -cron '* */3 * * *' # use cron or every option, don't use both -every '3d' # use every or cron option, don't use both -queue 'default' -args 'custom arg' -description 'Nice description' +resque_schedule( + cron: '* */3 * * *', # use cron or every option, don't use both + every: '3d', # use every or cron option, don't use both + args: 'Custom arg', + description: 'Nice description' +) ``` Job's example: @@ -439,10 +439,9 @@ require 'resque/scheduler/job' class MyGreatJob include Resque::Scheduler::Job - cron '*/2 * * * *' - queue 'default' - args 'args' - description 'description' + @queue = :default + + resque_schedule cron: '*/2 * * * *', args: 'args', description: 'description' end ``` diff --git a/lib/resque/scheduler.rb b/lib/resque/scheduler.rb index c9b842d3..fbe4f2c8 100644 --- a/lib/resque/scheduler.rb +++ b/lib/resque/scheduler.rb @@ -110,21 +110,6 @@ def load_schedule! Dir[auto_load.to_s].each do |file| require File.absolute_path(file) - name = File.basename(file, '.rb') - begin - klass = Resque::Scheduler::Util.constantize(name) - rescue NameError - log! "Can't load file #{file}" - end - load_schedule_job( - name, - 'class' => klass.name, - 'cron' => klass.respond_to?(:cron) ? klass.cron : nil, - 'every' => klass.respond_to?(:every) ? klass.every : nil, - 'queue' => klass.respond_to?(:queue) ? klass.queue : nil, - 'args' => klass.respond_to?(:args) ? klass.args : nil, - 'description' => klass.respond_to?(:description) ? klass.description : nil - ) if klass end Resque.redis.del(:schedules_changed) if am_master && dynamic diff --git a/lib/resque/scheduler/job.rb b/lib/resque/scheduler/job.rb index 15f90c49..61892278 100644 --- a/lib/resque/scheduler/job.rb +++ b/lib/resque/scheduler/job.rb @@ -10,29 +10,16 @@ def included(base) end module ClassMethods - def cron(value = nil) - return @cron ||= nil if value.nil? - @cron = value - end - - def every(value = nil) - return @every ||= nil if value.nil? - @every = value - end - - def queue(value = nil) - return @queue ||= nil if value.nil? - @queue = value - end - - def args(value = nil) - return @args ||= nil if value.nil? - @args = value - end - - def description(value = nil) - return @description ||= nil if value.nil? - @description = value + def resque_schedule(cron: nil, every: nil, args: nil, description: nil) + Resque::Scheduler.load_schedule_job( + name, + 'class' => name, + 'cron' => cron, + 'every' => every, + 'queue' => @queue, + 'args' => args, + 'description' => description + ) end end end diff --git a/test/fixtures/valid_cron_job.rb b/test/fixtures/valid_cron_job.rb index 60e90784..39993f0c 100644 --- a/test/fixtures/valid_cron_job.rb +++ b/test/fixtures/valid_cron_job.rb @@ -5,8 +5,7 @@ class ValidCronJob include Resque::Scheduler::Job - cron '*/2 * * * *' - queue 'default' - args 'args' - description 'description' + @queue = :default + + resque_schedule cron: '*/2 * * * *', args: 'args', description: 'description' end diff --git a/test/fixtures/valid_every_job.rb b/test/fixtures/valid_every_job.rb index 74489a64..a99011dd 100644 --- a/test/fixtures/valid_every_job.rb +++ b/test/fixtures/valid_every_job.rb @@ -5,8 +5,7 @@ class ValidEveryJob include Resque::Scheduler::Job - every '1d' - queue 'default' - args 'args' - description 'description' + @queue = :default + + resque_schedule every: '1d', args: 'args', description: 'description' end diff --git a/test/job_test.rb b/test/job_test.rb deleted file mode 100644 index a2835544..00000000 --- a/test/job_test.rb +++ /dev/null @@ -1,33 +0,0 @@ -# vim:fileencoding=utf-8 - -require 'resque/scheduler/job' - -context 'Job' do - test 'has nil default parameters' do - class EmptyJob - include Resque::Scheduler::Job - end - - %i(cron every queue args description).each do |p| - assert_nil EmptyJob.send(p) - end - end - - test 'saves values' do - class JobWithValues - include Resque::Scheduler::Job - - cron '* */3 * * *' - every '3d' - queue 'default' - args 'some arg' - description 'nice description' - end - - assert_equal '* */3 * * *', JobWithValues.cron - assert_equal '3d', JobWithValues.every - assert_equal 'default', JobWithValues.queue - assert_equal 'some arg', JobWithValues.args - assert_equal 'nice description', JobWithValues.description - end -end diff --git a/test/scheduler_test.rb b/test/scheduler_test.rb index 223cbfcf..b0e00a69 100644 --- a/test/scheduler_test.rb +++ b/test/scheduler_test.rb @@ -113,14 +113,14 @@ assert_equal(2, Resque::Scheduler.rufus_scheduler.jobs.size) assert_equal(2, Resque::Scheduler.scheduled_jobs.size) - %w(valid_cron_job valid_every_job).each do |job_name| + %w(ValidCronJob ValidEveryJob).each do |job_name| assert Resque::Scheduler.scheduled_jobs.keys.include?(job_name) end - cron_job = Resque::Scheduler.scheduled_jobs['valid_cron_job'] + cron_job = Resque::Scheduler.scheduled_jobs['ValidCronJob'] assert_equal('*/2 * * * *', cron_job.original) - every_job = Resque::Scheduler.scheduled_jobs['valid_every_job'] + every_job = Resque::Scheduler.scheduled_jobs['ValidEveryJob'] assert_equal('1d', every_job.original) end