Class: Projects::BuildArtifactsSizeRefresh

Inherits:
ApplicationRecord show all
Includes:
BulkInsertSafe
Defined in:
app/models/projects/build_artifacts_size_refresh.rb

Constant Summary collapse

STALE_WINDOW =
2.hours
STATES =
{
  created: 1,
  running: 2,
  pending: 3
}.freeze

Constants included from BulkInsertSafe

BulkInsertSafe::ALLOWED_CALLBACKS, BulkInsertSafe::DEFAULT_BATCH_SIZE, BulkInsertSafe::MethodNotAllowedError, BulkInsertSafe::PrimaryKeySetError

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.enqueue_refresh(projects) ⇒ Object


53
54
55
56
57
58
59
60
61
# File 'app/models/projects/build_artifacts_size_refresh.rb', line 53

def self.enqueue_refresh(projects)
  now = Time.zone.now

  records = Array(projects).map do |project|
    new(project: project, state: STATES[:created], created_at: now, updated_at: now)
  end

  bulk_insert!(records, skip_duplicates: true)
end

.process_next_refresh!Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/projects/build_artifacts_size_refresh.rb', line 63

def self.process_next_refresh!
  next_refresh = nil

  transaction do
    next_refresh = remaining
      .order(:state, :updated_at)
      .lock('FOR UPDATE SKIP LOCKED')
      .take

    next_refresh&.process!
  end

  next_refresh
end

Instance Method Details

#next_batch(limit:) ⇒ Object


84
85
86
87
88
89
# File 'app/models/projects/build_artifacts_size_refresh.rb', line 84

def next_batch(limit:)
  project.job_artifacts.select(:id, :size)
    .where('created_at <= ? AND id > ?', refresh_started_at, last_job_artifact_id.to_i)
    .order(:created_at)
    .limit(limit)
end

#reset_project_statistics!Object


78
79
80
81
82
# File 'app/models/projects/build_artifacts_size_refresh.rb', line 78

def reset_project_statistics!
  statistics = project.statistics
  statistics.update!(build_artifacts_size: 0)
  statistics.clear_counter!(:build_artifacts_size)
end