Sha256: a389eca2d3cdf6bbcf35751b62abe2b58e46e835cea6746fd615fc61e88a8ca4

Contents?: true

Size: 1.33 KB

Versions: 24

Compression:

Stored size: 1.33 KB

Contents

require "active_job"

module CanvasSync
  # Inherit from this class to build a Job that will log to the canvas_sync_job_logs table
  class Job < ActiveJob::Base
    before_enqueue do |job|
      create_job_log(job)
    end

    around_perform do |job, block|
      @job_log = CanvasSync::JobLog.find_by(job_id: job.job_id) || create_job_log(job)
      @job_log.status = JobLog::RUNNING_STATUS
      @job_log.started_at = Time.now
      @job_log.save

      begin
        block.call
        @job_log.status = JobLog::SUCCESS_STATUS
      rescue => e # rubocop:disable Style/RescueStandardError
        @job_log.exception = "#{e.class}: #{e.message}"
        @job_log.backtrace = e.backtrace
        @job_log.status = JobLog::ERROR_STATUS
        raise e
      ensure
        if CanvasSync.config.classes_to_only_log_errors_on.include?(@job_log.job_class) && @job_log.status != JobLog::ERROR_STATUS
          @job_log.destroy
        else
          @job_log.completed_at = Time.now
          @job_log.save!
        end
      end
    end

    def report_checker_wait_time
      Rails.env.development? ? 1.second : 30.seconds
    end

    def create_job_log(job)
      CanvasSync::JobLog.create(
        job_class: self.class.name,
        job_arguments: job.arguments,
        job_id: job.job_id,
        status: JobLog::ENQUEUED_STATUS,
      )
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
canvas_sync-0.10.5 lib/canvas_sync/job.rb
canvas_sync-0.10.4 lib/canvas_sync/job.rb
canvas_sync-0.10.3 lib/canvas_sync/job.rb
canvas_sync-0.10.2 lib/canvas_sync/job.rb
canvas_sync-0.10.0 lib/canvas_sync/job.rb
canvas_sync-0.9.4 lib/canvas_sync/job.rb
canvas_sync-0.9.3 lib/canvas_sync/job.rb
canvas_sync-0.9.2 lib/canvas_sync/job.rb
canvas_sync-0.9.1 lib/canvas_sync/job.rb
canvas_sync-0.9.0 lib/canvas_sync/job.rb
canvas_sync-0.8.4 lib/canvas_sync/job.rb
canvas_sync-0.8.3 lib/canvas_sync/job.rb
canvas_sync-0.8.2 lib/canvas_sync/job.rb
canvas_sync-0.8.1 lib/canvas_sync/job.rb
canvas_sync-0.8.0 lib/canvas_sync/job.rb
canvas_sync-0.7.3 lib/canvas_sync/job.rb
canvas_sync-0.7.2 lib/canvas_sync/job.rb
canvas_sync-0.7.1 lib/canvas_sync/job.rb
canvas_sync-0.7.0 lib/canvas_sync/job.rb
canvas_sync-0.6.4 lib/canvas_sync/job.rb