lib/rocket_job/plugins/cron.rb in rocketjob-3.3.3 vs lib/rocket_job/plugins/cron.rb in rocketjob-3.3.4
- old
+ new
@@ -30,10 +30,12 @@
#
# Note:
# - The job will not be restarted if:
# - A validation fails after cloning this job.
# - The job has expired.
+ # - Any time the `cron_schedule` is changed, the `run_at` is automatically set before saving the changes.
+ # - However, if the `run_at` is explicitly set then it will not be overriden.
#
# Example:
#
# class MyCronJob < RocketJob::Job
# include RocketJob::Plugins::Cron
@@ -100,11 +102,11 @@
included do
include Restart
field :cron_schedule, type: String, class_attribute: true, user_editable: true, copy_on_restart: true
- before_create :rocket_job_set_run_at
+ before_save :rocket_job_set_run_at
validates_presence_of :cron_schedule
validates_each :cron_schedule do |record, attr, value|
begin
RocketJob::Plugins::Rufus::CronLine.new(value)
@@ -112,13 +114,23 @@
record.errors.add(attr, exc.message)
end
end
end
+ # Returns [Time] the next time this job will be scheduled to run at.
+ #
+ # 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)
+ end
+
private
def rocket_job_set_run_at
- self.run_at = RocketJob::Plugins::Rufus::CronLine.new(cron_schedule).next_time
+ self.run_at = rocket_job_cron_next_time if cron_schedule_changed? && !run_at_changed?
end
end
end
end