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

Adds support for cron syntax #30

Open
wants to merge 6 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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ matrix:
- rvm: "2.5.3"
gemfile: "gemfiles/delayed_job_3.gemfile"
before_install:
# Travis bundler versions are quite out of date and can cause install errors
# see: https://github.com/rubygems/rubygems/issues/1419
- "gem install bundler"
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
- gem install bundler -v '< 2'
1 change: 1 addition & 0 deletions delayed_job_recurring.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'delayed_job', '>= 3.0'
s.add_runtime_dependency 'delayed_job_active_record'
s.add_runtime_dependency 'fugit', '~> 1.2.1'
end
19 changes: 18 additions & 1 deletion lib/delayed/recurring_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module Delayed
module RecurringJob
def self.included(base)
require 'fugit' unless defined?(Fugit)
base.extend(ClassMethods)
base.class_eval do
@@logger = Delayed::Worker.logger
Expand All @@ -30,6 +31,7 @@ def schedule! options = {}
end

@schedule_options = options.reverse_merge(@schedule_options || {}).reverse_merge(
cron: self.class.cron,
run_at: self.class.run_at,
timezone: self.class.timezone,
run_interval: serialize_duration(self.class.run_every),
Expand All @@ -51,6 +53,8 @@ def schedule! options = {}
end

def next_run_time
return @schedule_options[:cron].next_time.to_s if @schedule_options[:cron].respond_to?(:next_time)

times = @schedule_options[:run_at]
times = [times] unless times.is_a? Array
times = times.map{|time| parse_time(time, @schedule_options[:timezone])}
Expand Down Expand Up @@ -114,6 +118,19 @@ def next_future_time(times)
end

module ClassMethods

def cron(cronline = false)
return @cron if defined?(@cron) && cronline == false
return (@cron = nil) if cronline.nil?

if cronline
@cron = Fugit.parse(cronline)
raise ArgumentError, 'Only cron and "natural language" syntax supported' unless @cron.is_a?(Fugit::Cron)
end

@cron
end

def run_at(*times)
if times.length == 0
@run_at || run_every.from_now
Expand Down Expand Up @@ -198,7 +215,7 @@ def scheduled?(options = {})
end

def inherited(subclass)
[:@run_at, :@run_interval, :@tz, :@priority].each do |var|
[:@run_at, :@run_interval, :@tz, :@priority, :@cron].each do |var|
next unless instance_variable_defined? var
subclass.instance_variable_set var, self.instance_variable_get(var)
subclass.instance_variable_set "#{var}_inherited", true
Expand Down