Sha256: 875cde0096238269778eda76224805ebe9618a94438d312b89245a05fc125ea3

Contents?: true

Size: 1.48 KB

Versions: 29

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

class ScheduledTask < ActiveRecord::Base
  module Status
    def status
      return STATUS_DISABLED unless enabled?
      return STATUS_RUNNING if running?
      return STATUS_WAITING if waiting?

      last_fail_status.presence || STATUS_FAILED
    end

    def failed?
      [STATUS_FAILED, STATUS_ABORTED, STATUS_TIMEOUT].include?(status)
    end

    def running?
      last_run_start.present?
    end

    def waiting?
      return true if ended?(last_run_successful_end, last_run_unsuccessful_end)

      status_attributes.all? { |a| send(a).blank? }
    end

    private

    def ended?(time, oposite_time)
      !running? && time.present? && (oposite_time.blank? || oposite_time < time)
    end

    def status_on_start
      update!(last_run_start: Time.zone.now)
    end

    def status_on_end(exception, last_fail_status)
      update!(
        next_run: calculate_next_run,
        (exception ? :last_run_unsuccessful_start : :last_run_successful_start) => last_run_start,
        (exception ? :last_run_unsuccessful_end : :last_run_successful_end) => Time.zone.now,
        last_run_start: nil,
        pid: nil,
        last_fail_status: exception ? last_fail_status : nil
      )
    end

    def status_attributes
      %w[start successful_start successful_end unsuccessful_start unsuccessful_end].map do |a|
        "last_run_#{a}"
      end
    end

    def status_attributes_values
      status_attributes.map { |a| "#{a}: #{send(a)}" }.join(', ')
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
tasks_scheduler-0.9.4 app/models/scheduled_task/status.rb
tasks_scheduler-0.9.3 app/models/scheduled_task/status.rb
tasks_scheduler-0.9.2 app/models/scheduled_task/status.rb
tasks_scheduler-0.9.1 app/models/scheduled_task/status.rb
tasks_scheduler-0.9.0 app/models/scheduled_task/status.rb
tasks_scheduler-0.8.0 app/models/scheduled_task/status.rb
tasks_scheduler-0.7.0 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.13 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.12 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.11 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.10 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.9 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.8 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.7 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.6 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.5 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.4 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.3 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.2 app/models/scheduled_task/status.rb
tasks_scheduler-0.6.1 app/models/scheduled_task/status.rb