diff options
Diffstat (limited to 'app/models/project_export_job.rb')
-rw-r--r-- | app/models/project_export_job.rb | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/app/models/project_export_job.rb b/app/models/project_export_job.rb index decc71ee193..d26ce5465cd 100644 --- a/app/models/project_export_job.rb +++ b/app/models/project_export_job.rb @@ -1,11 +1,24 @@ # 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 @@ -19,9 +32,17 @@ class ProjectExportJob < ApplicationRecord transition [:queued, :started] => :failed end - state :queued, value: 0 - state :started, value: 1 - state :finished, value: 2 - state :failed, value: 3 + 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 |