Sha256: feaa82371de88fb03eac3a143038881f9b3e797ea1a07deae276a05246600bda

Contents?: true

Size: 1.39 KB

Versions: 33

Compression:

Stored size: 1.39 KB

Contents

#
# Run in cron
#
module Cron
  #
  # Cycle through all members and tell them to sync with with switchboard
  #
  class RestartOrphanedDelayedJobs < Job
    cron_tab_entry :hourly

    #
    # Only run when we have background jobs and we have the time keeper plugin installed
    #
    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

    #
    # 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
    #
    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
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
web47core-3.2.20 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.19 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.18 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.17 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.16 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.15 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.14 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.13 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.12 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.9 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.8 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.7 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.6 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.5 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.4 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-2.2.20 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-2.2.19 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.3 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-3.2.2 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb
web47core-2.2.15 lib/app/jobs/cron/restart_orphaned_delayed_jobs.rb