Class: ContainerExpirationPolicy

Inherits:
ApplicationRecord show all
Includes:
EachBatch, Schedulable, UsageStatistics
Defined in:
app/models/container_expiration_policy.rb

Constant Summary collapse

POLICY_PARAMS =
%w[
  older_than
  keep_n
  name_regex
  name_regex_keep
].freeze

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schedulable

#schedule_next_run!

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

.cadence_optionsObject



62
63
64
65
66
67
68
69
70
# File 'app/models/container_expiration_policy.rb', line 62

def self.cadence_options
  {
    '1d': _('Every day'),
    '7d': _('Every week'),
    '14d': _('Every two weeks'),
    '1month': _('Every month'),
    '3month': _('Every three months')
  }
end

.keep_n_optionsObject



51
52
53
54
55
56
57
58
59
60
# File 'app/models/container_expiration_policy.rb', line 51

def self.keep_n_options
  {
    1 => _('%{tags} tag per image name') % { tags: 1 },
    5 => _('%{tags} tags per image name') % { tags: 5 },
    10 => _('%{tags} tags per image name') % { tags: 10 },
    25 => _('%{tags} tags per image name') % { tags: 25 },
    50 => _('%{tags} tags per image name') % { tags: 50 },
    100 => _('%{tags} tags per image name') % { tags: 100 }
  }
end

.older_than_optionsObject



72
73
74
75
76
77
78
79
80
# File 'app/models/container_expiration_policy.rb', line 72

def self.older_than_options
  {
    '7d': _('%{days} days until tags are automatically removed') % { days: 7 },
    '14d': _('%{days} days until tags are automatically removed') % { days: 14 },
    '30d': _('%{days} days until tags are automatically removed') % { days: 30 },
    '60d': _('%{days} days until tags are automatically removed') % { days: 60 },
    '90d': _('%{days} days until tags are automatically removed') % { days: 90 }
  }
end

.with_container_repositoriesObject



31
32
33
34
35
36
37
38
39
# File 'app/models/container_expiration_policy.rb', line 31

def self.with_container_repositories
  where(
    'EXISTS (?)',
    ContainerRepository.select(1)
                       .where(
                         'container_repositories.project_id = container_expiration_policies.project_id'
                       )
  )
end

.without_container_repositoriesObject



41
42
43
44
45
46
47
48
49
# File 'app/models/container_expiration_policy.rb', line 41

def self.without_container_repositories
  where.not(
    'EXISTS(?)',
    ContainerRepository.select(1)
                       .where(
                         'container_repositories.project_id = container_expiration_policies.project_id'
                       )
  )
end

Instance Method Details

#disable!Object



88
89
90
# File 'app/models/container_expiration_policy.rb', line 88

def disable!
  update_attribute(:enabled, false)
end

#policy_paramsObject



92
93
94
# File 'app/models/container_expiration_policy.rb', line 92

def policy_params
  attributes.slice(*POLICY_PARAMS)
end

#set_next_run_atObject



82
83
84
85
86
# File 'app/models/container_expiration_policy.rb', line 82

def set_next_run_at
  cadence_seconds = ChronicDuration.parse(cadence, use_complete_matcher: true).seconds

  self.next_run_at = Time.zone.now + cadence_seconds
end