File: export_batch.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (40 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module BulkImports
  class ExportBatch < ApplicationRecord
    self.table_name = 'bulk_import_export_batches'

    BATCH_SIZE = 1000
    TIMEOUT_AFTER_START = 1.hour
    IN_PROGRESS_STATES = %i[created started].freeze

    scope :started_and_not_timed_out, -> { with_status(:started).where(updated_at: TIMEOUT_AFTER_START.ago...) }

    belongs_to :export, class_name: 'BulkImports::Export'
    has_one :upload, class_name: 'BulkImports::ExportUpload', foreign_key: :batch_id, inverse_of: :batch

    validates :batch_number, presence: true, uniqueness: { scope: :export_id }

    scope :in_progress, -> { with_status(IN_PROGRESS_STATES) }

    state_machine :status, initial: :created do
      state :created, value: 0
      state :started, value: 2
      state :finished, value: 1
      state :failed, value: -1

      event :start do
        transition any => :started
      end

      event :finish do
        transition started: :finished
        transition failed: :failed
      end

      event :fail_op do
        transition any => :failed
      end
    end
  end
end