lib/rocket_job/plugins/cron.rb in rocketjob-5.3.0 vs lib/rocket_job/plugins/cron.rb in rocketjob-5.3.1
- old
+ new
@@ -1,6 +1,7 @@
require "active_support/concern"
+require "fugit"
module RocketJob
module Plugins
# Allow jobs to run on a predefined schedule, much like a crontab.
#
@@ -15,11 +16,13 @@
included do
include Restart
field :cron_schedule, type: String, class_attribute: true, user_editable: true, copy_on_restart: true
- validate :rocket_job_cron_valid
+ validates_each :cron_schedule do |record, attr, value|
+ record.errors.add(attr, "Invalid cron_schedule: #{value.inspect}") if value && !Fugit::Cron.new(value)
+ end
before_save :rocket_job_cron_set_run_at
private
# Prevent auto restart if this job does not have a cron schedule.
@@ -47,26 +50,16 @@
# Parameters
# time: [Time]
# The next time as of this time.
# Default: Time.now
def rocket_job_cron_next_time(time = Time.now)
- RocketJob::Plugins::Rufus::CronLine.new(cron_schedule).next_time(time)
+ Fugit::Cron.new(cron_schedule).next_time.to_utc_time
end
- private
-
def rocket_job_cron_set_run_at
return unless cron_schedule
self.run_at = rocket_job_cron_next_time if cron_schedule_changed? && !run_at_changed?
- end
-
- def rocket_job_cron_valid
- return unless cron_schedule
-
- RocketJob::Plugins::Rufus::CronLine.new(cron_schedule)
- rescue ArgumentError => e
- errors.add(:cron_schedule, e.message)
end
end
end
end