Skip to content

Commit

Permalink
Use fugit with timezone support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlascelles committed Mar 29, 2018
1 parent 410ccde commit d741671
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 15 deletions.
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
sudo: false
language: ruby
rvm:
- 2.1
- 2.2
- 2.3
- 2.4
- 2.1
- 2.2
- 2.3
- 2.4
env:
global:
- TZ=Europe/London
- TZ=Europe/London
before_install:
- export TZ=Europe/London
script:
- bundle exec rspec -fd -b -P ./spec/**/*_spec.rb
- bundle exec rspec -fd -b -P ./spec/**/*_spec.rb
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0 (2018-03-29)

* Upgraded [fugit](https://github.com/floraison/fugit/issues/2) to allow timezones in cron lines

## 1.1.0 (2018-03-24)

* Switched to use DB time to find "now" so as to match que queries
Expand All @@ -22,7 +26,7 @@

## 0.10.1 (2017-12-03)

* Added `schedule_type` config key.
* Added `schedule_type` config key

## 0.9.1 (2017-12-03)

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ For example:
CancelAbandonedOrders:
cron: "*/5 * * * *"

# Specify the job_class, using any name for the key.
# Specify the job_class, using any name for the key
queue_documents_for_indexing:
cron: "0 0 * * *"
class: QueueDocuments
Expand All @@ -67,12 +67,16 @@ SendOrders:
cron: "0 0 * * *"
args: ['open']

# Use simpler cron syntax.
# Use simpler cron syntax
SendBilling:
cron: "@daily"

# Use timezone cron syntax
SendCoupons:
cron: "0 7 * * * America/Los_Angeles"

# Altogether now
all_args_job:
all_options_job:
cron: "0 0 * * *"
class: QueueDocuments
queue: reporting
Expand Down
2 changes: 1 addition & 1 deletion lib/que/scheduler/defined_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.err_field(f, v)
job_class = Object.const_get(v)
job_class < Que::Job ? job_class : err_field(:job_class, v)
}
property :cron, transform_with: ->(v) { Fugit::Cron.new(v) || err_field(:cron, v) }
property :cron, transform_with: ->(v) { Fugit::Cron.parse(v) || err_field(:cron, v) }
property :queue, transform_with: ->(v) { v.is_a?(String) ? v : err_field(:queue, v) }
property :priority, transform_with: ->(v) { v.is_a?(Integer) ? v : err_field(:priority, v) }
property :args
Expand Down
2 changes: 1 addition & 1 deletion lib/que/scheduler/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Que
module Scheduler
VERSION = '1.1.0'.freeze
VERSION = '1.2.0'.freeze
end
end
5 changes: 2 additions & 3 deletions que-scheduler.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'que/scheduler/version'

Expand All @@ -19,8 +19,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'activesupport', '>= 3.0'
spec.add_dependency 'backports', '~> 3.10'
spec.add_dependency 'et-orbi', '> 1.0.5' # need the `#to_local_time` method
spec.add_dependency 'fugit', '~> 1'
spec.add_dependency 'fugit', '~> 1.1'
spec.add_dependency 'hashie', '~> 3'
spec.add_dependency 'que', '~> 0.10'

Expand Down
4 changes: 4 additions & 0 deletions spec/config/que_schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ TwiceDailyTestJob:
priority: 35
queue: backlog
schedule_type: every_event

TimezoneTestJob:
cron: "5 7 1 * * America/Los_Angeles"
schedule_type: every_event
9 changes: 9 additions & 0 deletions spec/que/scheduler/defined_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def expect_time(from, to, exp)
expect(job.cron.to_cron_s).to eq('0 0 * * 0')
end

it 'allows crons with fugit compatible timezones' do
job = described_class.new(
name: 'testing_job_definitions',
job_class: 'HalfHourlyTestJob',
cron: '0 7 * * * America/Los_Angeles'
)
expect(job.cron.zone).to eq('America/Los_Angeles')
end

it 'checks the queue is a string' do
expect do
described_class.new(
Expand Down
24 changes: 24 additions & 0 deletions spec/que/scheduler/enqueueing_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
daily_test_job_specifying_class
DailyTestJob
TwiceDailyTestJob
TimezoneTestJob
]
end

Expand All @@ -31,6 +32,25 @@
run_test('2017-10-08T16:59:59', 1.seconds, HalfHourlyTestJob => [{}])
end

it 'should enqueue if a job has been defined in a different timezone' do
# Last scheduler run time is 18:04:58 in Finland (+3).
# This run time is 10:04:59 in New York (-5)
last_run = Time.zone.parse('2017-12-01T18:04:58+03')
this_run = Time.zone.parse('2017-12-01T10:04:59-05')
# Prove this is just one second different
expect(this_run - last_run).to eq(1.second)

# The cron was defined for 7:05 in Los Angeles (-8) so should not yet run (1 sec too early).
run_test_with_times(last_run, this_run, {})

# Now tip it over the required time by a 1 second so it should run.
# Since it is an `every_event` job it should receive the schedule time as an arg.
expected_arg_time = Time.zone.parse('2017-12-01T07:05:00-08')
run_test_with_times(
last_run, this_run + 1.second, TimezoneTestJob => [{ args: [expected_arg_time] }]
)
end

# This is testing that the fugit cron "next_time" doesn't return the current time if it matches.
# It truly is the "next" time.
it 'should not enqueue if the previous run time was exactly the cron time' do
Expand Down Expand Up @@ -87,6 +107,10 @@
def run_test(last_run_time, delay_since_last_scheduler, expect_scheduled)
last_time = Time.zone.parse(last_run_time)
as_time = last_time + delay_since_last_scheduler
run_test_with_times(last_time, as_time, expect_scheduled)
end

def run_test_with_times(last_time, as_time, expect_scheduled)
scheduler_job_args = Que::Scheduler::SchedulerJobArgs.new(
last_run_time: last_time,
job_dictionary: all_keys,
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# zonebie. If you want to force one particular timezone, you can use the following:
# ENV['ZONEBIE_TZ'] = 'International Date Line West'
# Require zonebie before any other gem to ensure it sets the correct test timezone.
# Setting zonebie to London for travis tests so it matches the .travis.yml ENV.
ENV['ZONEBIE_TZ'] = 'Europe/London' if ENV['TRAVIS'] == 'true'
require 'zonebie/rspec'

Bundler.require :default, :development
Expand Down
4 changes: 4 additions & 0 deletions spec/support/test_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class TwiceDailyTestJob < ::Que::Job
def run; end
end

class TimezoneTestJob < ::Que::Job
def run; end
end

class NotAQueJob
def run; end
end

0 comments on commit d741671

Please sign in to comment.