class Que::Scheduler::DefinedJob

Constants

DEFINED_JOB_TYPES

Public Class Methods

create(options) click to toggle source
# File lib/que/scheduler/defined_job.rb, line 24
def create(options)
  defined_job = new(options.compact)
  defined_job.freeze.tap { |dj| dj.validate(options) }
end

Public Instance Methods

calculate_missed_runs(last_run_time, as_time) click to toggle source

Given the last scheduler run time, and this run time, return all the instances that should be enqueued for the job class.

# File lib/que/scheduler/defined_job.rb, line 40
def calculate_missed_runs(last_run_time, as_time)
  missed_times = []
  last_time = last_run_time
  while (next_run = next_run_time(last_time, as_time))
    missed_times << next_run
    last_time = next_run
  end

  generate_to_enqueue_list(missed_times)
end
next_run_time(from, to) click to toggle source

Given a “last time”, return the next Time the event will occur, or nil if it is after “to”.

# File lib/que/scheduler/defined_job.rb, line 32
def next_run_time(from, to)
  next_time = cron.next_time(from)
  next_run = next_time.to_local_time.in_time_zone(next_time.zone)
  next_run <= to ? next_run : nil
end
validate(options) click to toggle source
# File lib/que/scheduler/defined_job.rb, line 51
def validate(options)
  validate_fields_presence(options)
  validate_fields_types(options)
  validate_job_class_related(options)
end

Private Instance Methods

err_field(field, options, reason = "") click to toggle source
# File lib/que/scheduler/defined_job.rb, line 107
def err_field(field, options, reason = "")
  schedule = Que::Scheduler.configuration.schedule_location
  value = options[field]
  raise "Invalid #{field} '#{value}' for '#{name}' in que-scheduler schedule #{schedule}.\n" \
        "#{reason}"
end
generate_to_enqueue_list(missed_times) click to toggle source
# File lib/que/scheduler/defined_job.rb, line 114
def generate_to_enqueue_list(missed_times)
  return [] if missed_times.empty?

  options = to_h.slice(:args, :queue, :priority, :job_class).compact

  if schedule_type == DefinedJob::DEFINED_JOB_TYPE_EVERY_EVENT
    missed_times.map do |time_missed|
      ToEnqueue.create(options.merge(args: [time_missed] + args_array))
    end
  else
    [ToEnqueue.create(options.merge(args: args_array))]
  end
end
validate_fields_presence(options) click to toggle source

rubocop:enable Style/GuardClause

# File lib/que/scheduler/defined_job.rb, line 73
def validate_fields_presence(options)
  err_field(:name, options, "name must be present") if name.nil?
  err_field(:job_class, options, "job_class must be present") if job_class.nil?
  # An invalid cron is nil
  err_field(:cron, options, "cron must be present") if cron.nil?
end
validate_fields_types(options) click to toggle source

rubocop:disable Style/GuardClause This reads better as a conditional

# File lib/que/scheduler/defined_job.rb, line 60
def validate_fields_types(options)
  unless queue.nil? || queue.is_a?(String)
    err_field(:queue, options, "queue must be a string")
  end
  unless priority.nil? || priority.is_a?(Integer)
    err_field(:priority, options, "priority must be an integer")
  end
  unless DEFINED_JOB_TYPES.include?(schedule_type)
    err_field(:schedule_type, options, "Not in #{DEFINED_JOB_TYPES}")
  end
end