2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
module HasStatus
|
2016-06-02 11:05:42 +05:30
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
DEFAULT_STATUS = 'created'.freeze
|
2018-12-05 23:21:45 +05:30
|
|
|
BLOCKED_STATUS = %w[manual scheduled].freeze
|
|
|
|
AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped manual scheduled].freeze
|
|
|
|
STARTED_STATUSES = %w[running success failed skipped manual scheduled].freeze
|
2017-08-17 22:00:37 +05:30
|
|
|
ACTIVE_STATUSES = %w[pending running].freeze
|
|
|
|
COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
|
2018-12-05 23:21:45 +05:30
|
|
|
ORDERED_STATUSES = %w[failed pending running manual scheduled canceled success skipped created].freeze
|
2018-03-17 18:26:18 +05:30
|
|
|
STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3,
|
2018-12-05 23:21:45 +05:30
|
|
|
failed: 4, canceled: 5, skipped: 6, manual: 7,
|
|
|
|
scheduled: 8 }.freeze
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
UnknownStatusError = Class.new(StandardError)
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
class_methods do
|
|
|
|
def status_sql
|
2017-09-10 17:25:29 +05:30
|
|
|
scope_relevant = respond_to?(:exclude_ignored) ? exclude_ignored : all
|
|
|
|
scope_warnings = respond_to?(:failed_but_allowed) ? failed_but_allowed : none
|
|
|
|
|
|
|
|
builds = scope_relevant.select('count(*)').to_sql
|
|
|
|
created = scope_relevant.created.select('count(*)').to_sql
|
|
|
|
success = scope_relevant.success.select('count(*)').to_sql
|
|
|
|
manual = scope_relevant.manual.select('count(*)').to_sql
|
2018-12-05 23:21:45 +05:30
|
|
|
scheduled = scope_relevant.scheduled.select('count(*)').to_sql
|
2017-09-10 17:25:29 +05:30
|
|
|
pending = scope_relevant.pending.select('count(*)').to_sql
|
|
|
|
running = scope_relevant.running.select('count(*)').to_sql
|
|
|
|
skipped = scope_relevant.skipped.select('count(*)').to_sql
|
|
|
|
canceled = scope_relevant.canceled.select('count(*)').to_sql
|
|
|
|
warnings = scope_warnings.select('count(*) > 0').to_sql.presence || 'false'
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
"(CASE
|
2017-09-10 17:25:29 +05:30
|
|
|
WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success'
|
2017-08-17 22:00:37 +05:30
|
|
|
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
|
2016-11-03 12:29:30 +05:30
|
|
|
WHEN (#{builds})=(#{success}) THEN 'success'
|
2016-09-29 09:46:39 +05:30
|
|
|
WHEN (#{builds})=(#{created}) THEN 'created'
|
2017-08-17 22:00:37 +05:30
|
|
|
WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
|
2016-11-03 12:29:30 +05:30
|
|
|
WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
|
|
|
|
WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
|
2017-08-17 22:00:37 +05:30
|
|
|
WHEN (#{running})+(#{pending})>0 THEN 'running'
|
|
|
|
WHEN (#{manual})>0 THEN 'manual'
|
2018-12-05 23:21:45 +05:30
|
|
|
WHEN (#{scheduled})>0 THEN 'scheduled'
|
2017-08-17 22:00:37 +05:30
|
|
|
WHEN (#{created})>0 THEN 'running'
|
2016-06-02 11:05:42 +05:30
|
|
|
ELSE 'failed'
|
|
|
|
END)"
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
2016-11-03 12:29:30 +05:30
|
|
|
all.pluck(status_sql).first
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def started_at
|
|
|
|
all.minimum(:started_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished_at
|
|
|
|
all.maximum(:finished_at)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
def all_state_names
|
|
|
|
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
|
|
|
validates :status, inclusion: { in: AVAILABLE_STATUSES }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 'created'
|
2016-06-02 11:05:42 +05:30
|
|
|
state :pending, value: 'pending'
|
|
|
|
state :running, value: 'running'
|
|
|
|
state :failed, value: 'failed'
|
|
|
|
state :success, value: 'success'
|
|
|
|
state :canceled, value: 'canceled'
|
|
|
|
state :skipped, value: 'skipped'
|
2017-08-17 22:00:37 +05:30
|
|
|
state :manual, value: 'manual'
|
2018-12-05 23:21:45 +05:30
|
|
|
state :scheduled, value: 'scheduled'
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
scope :created, -> { where(status: 'created') }
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :relevant, -> { where(status: AVAILABLE_STATUSES - ['created']) }
|
2016-06-02 11:05:42 +05:30
|
|
|
scope :running, -> { where(status: 'running') }
|
|
|
|
scope :pending, -> { where(status: 'pending') }
|
|
|
|
scope :success, -> { where(status: 'success') }
|
2019-03-02 22:35:43 +05:30
|
|
|
scope :failed, -> { where(status: 'failed') }
|
|
|
|
scope :canceled, -> { where(status: 'canceled') }
|
|
|
|
scope :skipped, -> { where(status: 'skipped') }
|
|
|
|
scope :manual, -> { where(status: 'manual') }
|
|
|
|
scope :scheduled, -> { where(status: 'scheduled') }
|
2018-03-17 18:26:18 +05:30
|
|
|
scope :alive, -> { where(status: [:created, :pending, :running]) }
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :created_or_pending, -> { where(status: [:created, :pending]) }
|
2016-06-02 11:05:42 +05:30
|
|
|
scope :running_or_pending, -> { where(status: [:running, :pending]) }
|
|
|
|
scope :finished, -> { where(status: [:success, :failed, :canceled]) }
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :failed_or_canceled, -> { where(status: [:failed, :canceled]) }
|
|
|
|
|
|
|
|
scope :cancelable, -> do
|
2018-12-05 23:21:45 +05:30
|
|
|
where(status: [:running, :pending, :created, :scheduled])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2016-09-13 17:45:13 +05:30
|
|
|
STARTED_STATUSES.include?(status) && started_at
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
2016-09-13 17:45:13 +05:30
|
|
|
ACTIVE_STATUSES.include?(status)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def complete?
|
2016-09-13 17:45:13 +05:30
|
|
|
COMPLETED_STATUSES.include?(status)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def blocked?
|
2018-12-05 23:21:45 +05:30
|
|
|
BLOCKED_STATUS.include?(status)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_duration
|
|
|
|
if started_at && finished_at
|
|
|
|
finished_at - started_at
|
|
|
|
elsif started_at
|
|
|
|
Time.now - started_at
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|