Sha256: 900ad4f22af0243a98deda5541f42c192f0f325aa8516534990c9b21c0b8858b

Contents?: true

Size: 1.26 KB

Versions: 12

Compression:

Stored size: 1.26 KB

Contents

class Marty::CronJob < ActiveJob::Base
  around_perform do |_job, block|
    begin
      block.call
      log_success
    rescue StandardError => e
      log_failure(e)
      raise e
    end
  end

  def log_failure(exception)
    error = {
      message: exception.message,
      backtrace: exception.backtrace
    }

    ::Marty::BackgroundJob::Log.create!(
      job_class: self.class.name,
      status: :failure,
      error: error
    )
  end

  def log_success
    ::Marty::BackgroundJob::Log.create!(
      job_class: self.class.name,
      status: :success
    )
  end

  class << self
    def schedule
      return reschedule if scheduled?

      cron = cron_expression

      return if cron.blank?

      set(cron: cron).perform_later
    end

    def reschedule
      dj = delayed_job
      return dj.update(cron: cron_expression) if dj.locked_by?

      remove
      schedule
    end

    def remove
      delayed_job.destroy if scheduled?
    end

    alias remove_schedule remove

    def scheduled?
      delayed_job.present?
    end

    def delayed_job
      Delayed::Job.
        where('handler LIKE ?', "%job_class: #{name}\n%").
        first
    end

    def cron_expression
      ::Marty::BackgroundJob::Schedule.on.find_by(job_class: name)&.cron
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
marty-9.5.1 app/jobs/marty/cron_job.rb
marty-9.5.0 app/jobs/marty/cron_job.rb
marty-9.3.3 app/jobs/marty/cron_job.rb
marty-9.3.2 app/jobs/marty/cron_job.rb
marty-9.3.0 app/jobs/marty/cron_job.rb
marty-8.5.0 app/jobs/marty/cron_job.rb
marty-8.4.1 app/jobs/marty/cron_job.rb
marty-8.3.1 app/jobs/marty/cron_job.rb
marty-8.2.0 app/jobs/marty/cron_job.rb
marty-8.0.0 app/jobs/marty/cron_job.rb
marty-6.1.0 app/jobs/marty/cron_job.rb
marty-5.2.0 app/jobs/marty/cron_job.rb