Class: Cron::RestartOrphanedDelayedJobs

Inherits:
Job show all
Defined in:
lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb

Overview

Cycle through all members and tell them to sync with with switchboard

Instance Attribute Summary

Attributes inherited from ApplicationJob

#payload, #started_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Job

cron_tab_entry, #notify_job_failure, #send_support_email

Methods inherited from ApplicationJob

#duration, #parse_payload, #perform, valid_environments

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Class Method Details

.valid_environment?Boolean

Only run when we have background jobs and we have the time keeper plugin installed

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb', line 14

def self.valid_environment?
  Delayed::Worker.plugins.include?(Delayed::Plugins::TimeKeeper) &&
    SystemConfiguration.delayed_job_restart_orphaned? &&
    Delayed::Backend::Mongoid::Job.count.positive?
rescue StandardError
  false
end

Instance Method Details

#executeObject

Cycle through delayed jobs, looking for running jobs skip if we don’t have any records or low sample set skip if the allowed time is less than allowed by the job look to see if have a worker associated with the delayed job If we dont have a worker or the worker is dead, restart the job



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb', line 29

def execute
  Delayed::Backend::Mongoid::Job.each do |delayed_job|
    next unless delayed_job.running?

    metric = Delayed::Jobs::Metric.where(name: delayed_job.display_name).first
    next if metric.blank? || metric.count < 30 # not enough data to make a call

    run_time = Time.now.utc - delayed_job.locked_at
    next if run_time < metric.max_allowed_seconds # still within parameters

    worker = delayed_job.worker
    delayed_job.resubmit if worker.blank? || worker.dead?
  end
end