Class: ColdStorage::Run

Inherits:
ArchiveRecord show all
Defined in:
lib/cold_storage/run.rb

Overview

One archiving run of one model. Lives in the archive database, and is what makes the every: option work across processes and deploys.

Constant Summary collapse

STATUSES =
%w[running succeeded failed].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ArchiveRecord

connect!, database_config, reset_connection!, #restore!, source_model, #source_model, with_archive_connection

Class Method Details

.last_success_at(model) ⇒ Time?

Returns when the model was last archived successfully.

Returns:

  • (Time, nil)

    when the model was last archived successfully



16
17
18
19
# File 'lib/cold_storage/run.rb', line 16

def last_success_at(model)
  InternalSchema.ensure!
  for_model(model).succeeded.maximum(:finished_at)
end

.start!(model) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cold_storage/run.rb', line 21

def start!(model)
  InternalSchema.ensure!
  create!(
    archived_model: model.to_s,
    archived_table: (model.table_name if model.respond_to?(:table_name)),
    status: 'running',
    started_at: Time.current
  )
end

Instance Method Details

#fail!(error, archived_count: 0, deleted_count: 0) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/cold_storage/run.rb', line 41

def fail!(error, archived_count: 0, deleted_count: 0)
  update!(
    status: 'failed',
    archived_count: archived_count,
    deleted_count: deleted_count,
    finished_at: Time.current,
    error_message: "#{error.class}: #{error.message}"
  )
end

#succeed!(archived_count:, deleted_count:) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/cold_storage/run.rb', line 32

def succeed!(archived_count:, deleted_count:)
  update!(
    status: 'succeeded',
    archived_count: archived_count,
    deleted_count: deleted_count,
    finished_at: Time.current
  )
end