2020-11-12 10:09:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkImports::Tracker < ApplicationRecord
|
|
|
|
self.table_name = 'bulk_import_trackers'
|
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
alias_attribute :pipeline_name, :relation
|
|
|
|
|
2020-11-12 10:09:09 -05:00
|
|
|
belongs_to :entity,
|
|
|
|
class_name: 'BulkImports::Entity',
|
|
|
|
foreign_key: :bulk_import_entity_id,
|
|
|
|
optional: false
|
|
|
|
|
|
|
|
validates :relation,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: { scope: :bulk_import_entity_id }
|
|
|
|
|
|
|
|
validates :next_page, presence: { if: :has_next_page? }
|
2021-03-12 16:09:12 -05:00
|
|
|
|
|
|
|
validates :stage, presence: true
|
|
|
|
|
2022-06-07 23:09:01 -04:00
|
|
|
delegate :file_extraction_pipeline?, to: :pipeline_class
|
|
|
|
|
2021-03-29 05:09:15 -04:00
|
|
|
DEFAULT_PAGE_SIZE = 500
|
|
|
|
|
2021-04-13 05:11:10 -04:00
|
|
|
scope :next_pipeline_trackers_for, -> (entity_id) {
|
|
|
|
entity_scope = where(bulk_import_entity_id: entity_id)
|
|
|
|
next_stage_scope = entity_scope.with_status(:created).select('MIN(stage)')
|
|
|
|
|
|
|
|
entity_scope.where(stage: next_stage_scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.stage_running?(entity_id, stage)
|
|
|
|
where(stage: stage, bulk_import_entity_id: entity_id)
|
2021-12-13 10:12:59 -05:00
|
|
|
.with_status(:created, :enqueued, :started)
|
2021-04-13 05:11:10 -04:00
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_class
|
2021-09-13 05:11:26 -04:00
|
|
|
unless entity.pipeline_exists?(pipeline_name)
|
|
|
|
raise BulkImports::Error, "'#{pipeline_name}' is not a valid BulkImport Pipeline"
|
2021-04-13 05:11:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
pipeline_name.constantize
|
|
|
|
end
|
|
|
|
|
2021-03-12 16:09:12 -05:00
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 0
|
|
|
|
state :started, value: 1
|
|
|
|
state :finished, value: 2
|
2021-12-13 10:12:59 -05:00
|
|
|
state :enqueued, value: 3
|
2022-03-24 08:07:26 -04:00
|
|
|
state :timeout, value: 4
|
2021-03-12 16:09:12 -05:00
|
|
|
state :failed, value: -1
|
|
|
|
state :skipped, value: -2
|
|
|
|
|
|
|
|
event :start do
|
2021-12-13 10:12:59 -05:00
|
|
|
transition enqueued: :started
|
2021-09-30 02:09:27 -04:00
|
|
|
# To avoid errors when re-starting a pipeline in case of network errors
|
|
|
|
transition started: :started
|
2021-03-12 16:09:12 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 10:12:59 -05:00
|
|
|
event :retry do
|
|
|
|
transition started: :enqueued
|
|
|
|
end
|
|
|
|
|
|
|
|
event :enqueue do
|
|
|
|
transition created: :enqueued
|
|
|
|
end
|
|
|
|
|
2021-03-12 16:09:12 -05:00
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
|
|
|
transition failed: :failed
|
|
|
|
transition skipped: :skipped
|
|
|
|
end
|
|
|
|
|
|
|
|
event :skip do
|
|
|
|
transition any => :skipped
|
|
|
|
end
|
|
|
|
|
|
|
|
event :fail_op do
|
|
|
|
transition any => :failed
|
|
|
|
end
|
2022-03-24 08:07:26 -04:00
|
|
|
|
|
|
|
event :cleanup_stale do
|
|
|
|
transition [:created, :started] => :timeout
|
|
|
|
end
|
2021-03-12 16:09:12 -05:00
|
|
|
end
|
2020-11-12 10:09:09 -05:00
|
|
|
end
|