Skip to content

Commit

Permalink
Merge fa948d4 into 410ccde
Browse files Browse the repository at this point in the history
  • Loading branch information
hlascelles committed Mar 26, 2018
2 parents 410ccde + fa948d4 commit 628bad6
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 11 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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source 'https://rubygems.org'

gemspec

gem 'fugit', git: 'https://github.com/floraison/fugit.git',
ref: 'ea7f47a257bd1b0d285c473023e6b721cd1e241b'
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ SendOrders:
# 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
4 changes: 2 additions & 2 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 @@ -20,7 +20,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'
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
37 changes: 36 additions & 1 deletion spec/que/scheduler/defined_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,34 @@
end

def expect_time(from, to, exp)
puts "###################################"
puts ENV['TZ']
expected_time = exp ? Time.zone.parse(exp) : nil
expect(job.next_run_time(Time.zone.parse(from), Time.zone.parse(to))).to eq(expected_time)
from = Time.zone.parse(from)
to = Time.zone.parse(to)
puts from
puts to
puts job.next_run_time(from, to)
puts expected_time

cron = Fugit::Cron.parse('14 17 * * *')
from = ::EtOrbi.make_time(from)
t = Fugit::Cron::TimeCursor.new(from.translate(@timezone))
puts "etorbi #{from}"
puts "t #{t}"
puts t
puts t.time
puts t.time.translate(from.zone)

puts cron.to_cron_s
next_time = cron.next_time(from)
puts next_time
puts next_time.to_local_time
puts next_time.to_local_time.in_time_zone(next_time.zone)

puts "Done ''''''''''''''''''"

#expect(job.next_run_time(from, to)).to eq(expected_time)
end

it 'calculates the next run time over a day' do
Expand Down Expand Up @@ -52,6 +78,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
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
# 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.
puts "Setting timezone to Copenhagen #{ENV['ZONEBIE_TZ']}"
puts "Time.zone: #{Time.zone}"
ENV['ZONEBIE_TZ'] ="Copenhagen"
puts "Timezone now #{ENV['ZONEBIE_TZ']}"
puts "Time.zone: #{Time.zone}"
require 'zonebie/rspec'
puts "And timezone now #{ENV['ZONEBIE_TZ']}"
puts "Time.zone: #{Time.zone}"

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 628bad6

Please sign in to comment.