summaryrefslogtreecommitdiff
path: root/app/models/project_export_job.rb
blob: d26ce5465cdee43c2fb61c63112e38d2206ed140 (plain)
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
41
42
43
44
45
46
47
48
# frozen_string_literal: true

class ProjectExportJob < ApplicationRecord
  include EachBatch

  EXPIRES_IN = 7.days

  belongs_to :project
  has_many :relation_exports, class_name: 'Projects::ImportExport::RelationExport'

  validates :project, :jid, :status, presence: true

  STATUS = {
    queued: 0,
    started: 1,
    finished: 2,
    failed: 3
  }.freeze

  scope :prunable, -> { where("updated_at < ?", EXPIRES_IN.ago) }

  state_machine :status, initial: :queued do
    event :start do
      transition [:queued] => :started
    end

    event :finish do
      transition [:started] => :finished
    end

    event :fail_op do
      transition [:queued, :started] => :failed
    end

    state :queued, value: STATUS[:queued]
    state :started, value: STATUS[:started]
    state :finished, value: STATUS[:finished]
    state :failed, value: STATUS[:failed]
  end

  class << self
    def prune_expired_jobs
      prunable.each_batch do |relation| # rubocop:disable Style/SymbolProc
        relation.delete_all
      end
    end
  end
end