Sha256: a88a2f461eb50f134bdcbed58a9b041f8c68116f0e7fac0e1a6bd303aea8018c

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

module Workhorse::Jobs
  class DetectStaleJobsJob
    # Instantiates a new stale detection job.
    #
    # @param locked_to_started_threshold [Integer] The maximum number of seconds
    #   a job is allowed to stay 'locked' before this job throws an exception.
    #   Set this to 0 to skip this check.
    # @param run_time_threshold [Integer] The maximum number of seconds
    #   a job is allowed to run before this job throws an exception. Set this to
    #   0 to skip this check.
    def initialize(locked_to_started_threshold: 3 * 60, run_time_threshold: 12 * 60)
      @locked_to_started_threshold = locked_to_started_threshold
      @run_time_threshold = run_time_threshold
    end

    def perform
      messages = []

      # Detect jobs that are locked for too long #
      if @locked_to_started_threshold != 0
        rel = Workhorse::DbJob.locked
        rel = rel.where('locked_at < ?', @locked_to_started_threshold.seconds.ago)
        ids = rel.pluck(:id)

        unless ids.empty?
          messages << "Detected #{ids.size} jobs that were locked more than "\
                      "#{@locked_to_started_threshold}s ago and might be stale: #{ids.inspect}."
        end
      end

      # Detect jobs that are running for too long #
      if @run_time_threshold != 0
        rel = Workhorse::DbJob.started
        rel = rel.where('started_at < ?', @run_time_threshold.seconds.ago)
        ids = rel.pluck(:id)

        unless ids.empty?
          messages << "Detected #{ids.size} jobs that are running for longer than "\
                      "#{@run_time_threshold}s ago and might be stale: #{ids.inspect}."
        end
      end

      if messages.any?
        fail messages.join(' ')
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
workhorse-1.2.4 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.2.3 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.2.2 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.2.1 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.2.0 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.1.1 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.1.0 lib/workhorse/jobs/detect_stale_jobs_job.rb
workhorse-1.0.1 lib/workhorse/jobs/detect_stale_jobs_job.rb