Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow duration to be used for scheduling every #607

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## [Unreleased]
### Changed
- Remove support for Ruby < 2.3
- Allow use of duration (e.g. `30.minutes`) for interval type scheduling

## [4.5.0] - 2021-09-25
### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def load_schedule_job(name, config)
interval_defined = false
interval_types = %w(cron every)
interval_types.each do |interval_type|
next unless !config[interval_type].nil? && !config[interval_type].empty?
next unless !config[interval_type].nil? && !config[interval_type].to_s.empty?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this fixing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iloveitaly I believe this is since the interval type can now be Numeric and empty? is a String only.

args = optionizate_interval_value(config[interval_type])
args = [args, nil, job: true] if args.is_a?(::String)

Expand Down
4 changes: 2 additions & 2 deletions lib/resque/scheduler/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ def schedule_interval(config)

def schedule_interval_every(every)
every = [*every]
s = 'every: ' << every.first
s = 'every: ' << "#{every.first}#{'s' if every.first.is_a?(Numeric)}"

return s unless every.length > 1

s << ' ('
meta = every.last.map do |key, value|
"#{key.to_s.tr('_', ' ')} #{value}"
"#{key.to_s.tr('_', ' ')} #{value}#{'s' if value.is_a?(Numeric)}"
end
s << meta.join(', ') << ')'
end
Expand Down
16 changes: 16 additions & 0 deletions test/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@
assert job.opts.keys.include?(:first_in)
end

test 'load_schedule_job with every fixnum' do
Resque::Scheduler.load_schedule_job(
'some_ivar_job',
'every' => 30,
'class' => 'SomeIvarJob',
'args' => '/tmp'
)

assert_equal(1, Resque::Scheduler.rufus_scheduler.jobs.size)
assert_equal(1, Resque::Scheduler.scheduled_jobs.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not test the new job is well scheduled every 30s, and not 30m or 30ms ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, we need to test the actual schedule that's configured in the scheduler here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephpage @iloveitaly definitely, updated!

assert Resque::Scheduler.scheduled_jobs.keys.include?('some_ivar_job')
job = Resque::Scheduler.rufus_scheduler.jobs.first
next_times = job.next_times(2)
assert_equal(30, next_times[1] - next_times[0])
end

test 'load_schedule_job with cron with options' do
Resque::Scheduler.load_schedule_job(
'some_ivar_job',
Expand Down