2021-01-29 00:20:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkImports::Tracker < ApplicationRecord
|
|
|
|
self.table_name = 'bulk_import_trackers'
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
alias_attribute :pipeline_name, :relation
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
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-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
validates :stage, presence: true
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
delegate :file_extraction_pipeline?, to: :pipeline_class
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
DEFAULT_PAGE_SIZE = 500
|
|
|
|
|
|
|
|
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)
|
2022-01-26 12:08:38 +05:30
|
|
|
.with_status(:created, :enqueued, :started)
|
2021-04-29 21:17:54 +05:30
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_class
|
2021-11-11 11:23:49 +05:30
|
|
|
unless entity.pipeline_exists?(pipeline_name)
|
|
|
|
raise BulkImports::Error, "'#{pipeline_name}' is not a valid BulkImport Pipeline"
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
pipeline_name.constantize
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 0
|
|
|
|
state :started, value: 1
|
|
|
|
state :finished, value: 2
|
2022-01-26 12:08:38 +05:30
|
|
|
state :enqueued, value: 3
|
2022-06-21 17:19:12 +05:30
|
|
|
state :timeout, value: 4
|
2021-04-17 20:07:23 +05:30
|
|
|
state :failed, value: -1
|
|
|
|
state :skipped, value: -2
|
|
|
|
|
|
|
|
event :start do
|
2022-01-26 12:08:38 +05:30
|
|
|
transition enqueued: :started
|
2021-11-18 22:05:49 +05:30
|
|
|
# To avoid errors when re-starting a pipeline in case of network errors
|
|
|
|
transition started: :started
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
event :retry do
|
|
|
|
transition started: :enqueued
|
2022-11-25 23:54:43 +05:30
|
|
|
# To avoid errors when retrying a pipeline in case of network errors
|
|
|
|
transition enqueued: :enqueued
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
event :enqueue do
|
|
|
|
transition created: :enqueued
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
event :cleanup_stale do
|
|
|
|
transition [:created, :started] => :timeout
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|