Module: RocketJob::Plugins::ProcessingWindow

Extended by:
ActiveSupport::Concern
Defined in:
lib/rocket_job/plugins/processing_window.rb

Overview

Ensure that a job will only run between certain hours of the day, regardless of when it was created/enqueued. Useful for creating a job now that should only be processed later during a specific time window. If the time window is already active the job is able to be processed immediately.

Example: Process this job on Monday’s between 8am and 10am.

Example: Run this job on the 1st of every month from midnight for the entire day.

Since the cron schedule supports time zones it is easy to setup jobs to run at UTC or any other time zone.

Example: # Only run the job between the hours of 8:30am and 8:30pm. If it is after 8:30pm schedule # it to run at 8:30am the next day. class BusinessHoursJob < RocketJob::Job

include RocketJob::Plugins::ProcessingWindow

# The start of the processing window
self.processing_schedule = "30 8 * * * America/New_York"

# How long the processing window is:
self.processing_duration = 12.hours

def perform
  # Job will only run between 8:30am and 8:30pm Eastern
end

end

Note:

If a job is created/enqueued during the processing window, but due to busy/unavailable workers
is not processed during the window, the current job will be re-queued for the beginning
of the next processing window.

Instance Method Summary collapse

Instance Method Details

#rocket_job_processing_window_active?Boolean

Returns [true|false] whether this job is currently inside its processing window

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/rocket_job/plugins/processing_window.rb', line 56

def rocket_job_processing_window_active?
  time          = Time.now.utc
  previous_time = Fugit::Cron.new(processing_schedule).previous_time(time).to_utc_time
  # Inside previous processing window?
  previous_time + processing_duration > time
end