Class: Katello::SyncPlan
Constant Summary
collapse
- HOURLY =
'hourly'.freeze
- DAILY =
'daily'.freeze
- WEEKLY =
'weekly'.freeze
- CUSTOM_CRON =
'custom cron'.freeze
- TYPES =
[HOURLY, DAILY, WEEKLY, CUSTOM_CRON].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
#deletable?, #editable?, #readable?, #syncable?
Methods included from Glue
logger
Methods inherited from Model
#destroy!
Class Method Details
.humanize_class_name(_name = nil) ⇒ Object
141
142
143
|
# File 'app/models/katello/sync_plan.rb', line 141
def self.humanize_class_name(_name = nil)
_("Sync Plans")
end
|
.remove_disabled_product(repository) ⇒ Object
50
51
52
53
54
55
|
# File 'app/models/katello/sync_plan.rb', line 50
def self.remove_disabled_product(repository)
if (product = repository.product) && product&.redhat? && (sync_plan = product&.sync_plan) && product&.repositories&.count == 1
sync_plan.product_ids = (sync_plan.product_ids - [product.id])
sync_plan.save!
end
end
|
.search_enabled(_key, operator, value) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'app/models/katello/sync_plan.rb', line 37
def self.search_enabled(_key, operator, value)
value = ::Foreman::Cast.to_bool(value)
value = !value if operator == '<>'
active_logics = ForemanTasks::RecurringLogic.where(:state => "active")
active_logic_ids = active_logics.pluck(:id)
if active_logic_ids.empty?
{:conditions => "1=0"}
else
operator = value ? 'IN' : 'NOT IN'
{:conditions => "#{Katello::SyncPlan.table_name}.foreman_tasks_recurring_logic_id #{operator} (#{active_logic_ids.join(',')})"}
end
end
|
Instance Method Details
#add_recurring_logic_to_sync_plan(sync_date, interval, cron_expression) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'app/models/katello/sync_plan.rb', line 145
def add_recurring_logic_to_sync_plan(sync_date, interval, cron_expression)
sync_date_local_zone = sync_date
min, hour, day = sync_date_local_zone.min, sync_date_local_zone.hour, sync_date_local_zone.wday
if interval.nil?
fail _("Interval cannot be nil")
end
case interval.downcase
when "hourly"
cron = min.to_s + " * * * *"
when "daily"
cron = min.to_s + " " + hour.to_s + " * * *"
when "weekly"
cron = min.to_s + " " + hour.to_s + " * * " + day.to_s
when CUSTOM_CRON
cron = cron_expression
else
fail _("Interval not set correctly")
end
recurring_logic = ForemanTasks::RecurringLogic.new_from_cronline(cron)
unless recurring_logic.valid_cronline?
fail _("Cron expression is not valid!")
end
recurring_logic.save!
return recurring_logic
end
|
#associate_recurring_logic ⇒ Object
94
95
96
|
# File 'app/models/katello/sync_plan.rb', line 94
def associate_recurring_logic
self.foreman_tasks_recurring_logic = add_recurring_logic_to_sync_plan(self.sync_date, self.interval, self.cron_expression)
end
|
#cancel_recurring_logic ⇒ Object
124
125
126
|
# File 'app/models/katello/sync_plan.rb', line 124
def cancel_recurring_logic
self.foreman_tasks_recurring_logic&.cancel
end
|
#cron_status_mismatch? ⇒ Boolean
176
177
178
|
# File 'app/models/katello/sync_plan.rb', line 176
def cron_status_mismatch?
self.interval != CUSTOM_CRON && !(self.cron_expression.nil? || self.cron_expression.eql?(''))
end
|
#custom_cron_interval_expression ⇒ Object
63
64
65
|
# File 'app/models/katello/sync_plan.rb', line 63
def custom_cron_interval_expression
errors.add :base, _("Custom cron expression only needs to be set for interval value of custom cron") if cron_status_mismatch?
end
|
#enabled ⇒ Object
116
117
118
|
# File 'app/models/katello/sync_plan.rb', line 116
def enabled
enabled?
end
|
#enabled=(value) ⇒ Object
120
121
122
|
# File 'app/models/katello/sync_plan.rb', line 120
def enabled=(value)
self.foreman_tasks_recurring_logic.enabled = value
end
|
#enabled? ⇒ Boolean
112
113
114
|
# File 'app/models/katello/sync_plan.rb', line 112
def enabled?
self.foreman_tasks_recurring_logic && self.foreman_tasks_recurring_logic.state == 'active'
end
|
#next_sync ⇒ Object
136
137
138
139
|
# File 'app/models/katello/sync_plan.rb', line 136
def next_sync
return nil unless self.enabled?
self.foreman_tasks_recurring_logic&.tasks&.order(:start_at)&.last&.start_at
end
|
#product_enabled ⇒ Object
57
58
59
60
61
|
# File 'app/models/katello/sync_plan.rb', line 57
def product_enabled
products.each do |product|
errors.add :base, _("Cannot add product %s because it is disabled.") % product.name if (product.redhat? && !product.enabled?)
end
end
|
#rec_logic_changed? ⇒ Boolean
172
173
174
|
# File 'app/models/katello/sync_plan.rb', line 172
def rec_logic_changed?
saved_change_to_attribute?(:sync_date) || saved_change_to_attribute?(:interval) || saved_change_to_attribute?(:cron_expression)
end
|
#save_with_logic!(enabled = true) ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'app/models/katello/sync_plan.rb', line 67
def save_with_logic!(enabled = true)
self.task_group ||= SyncPlanTaskGroup.create!
self.cron_expression = '' if (self.cron_expression && !(self.interval.eql? CUSTOM_CRON))
associate_recurring_logic
self.save!
start_recurring_logic
self.foreman_tasks_recurring_logic.enabled = enabled unless (enabled || enabled.nil?)
end
|
#start_recurring_logic ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'app/models/katello/sync_plan.rb', line 102
def start_recurring_logic
User.as_anonymous_admin do
if self.sync_date.to_time < Time.now
self.foreman_tasks_recurring_logic.start(::Actions::Katello::SyncPlan::Run, self)
else
self.foreman_tasks_recurring_logic.start_after(::Actions::Katello::SyncPlan::Run, self.sync_date, self)
end
end
end
|
#sync_date_sans_tz ⇒ Object
132
133
134
|
# File 'app/models/katello/sync_plan.rb', line 132
def sync_date_sans_tz
self.sync_date.strftime('%Y-%m-%d %H:%M:%S %z')
end
|
#toggle_enabled(value = false) ⇒ Object
98
99
100
|
# File 'app/models/katello/sync_plan.rb', line 98
def toggle_enabled(value = false)
self.foreman_tasks_recurring_logic.enabled = value
end
|
#update_attributes_with_logics!(params) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'app/models/katello/sync_plan.rb', line 76
def update_attributes_with_logics!(params)
transaction do
fail _("No recurring logic tied to the sync plan.") if self.foreman_tasks_recurring_logic.nil?
params["cron_expression"] = '' if (params.key?("interval") && !params["interval"].eql?(CUSTOM_CRON) && self.interval.eql?(CUSTOM_CRON))
self.update!(params.except(:enabled))
if (rec_logic_changed? || (params["enabled"] && !self.enabled? && self.foreman_tasks_recurring_logic.cancelled?))
old_rec_logic = self.foreman_tasks_recurring_logic
associate_recurring_logic
::Katello::Util::Support.active_record_retry do
self.save!
end
old_rec_logic.reload.cancel
start_recurring_logic
end
toggle_enabled(params[:enabled]) if (params.key?(:enabled) && params[:enabled] != self.enabled?)
end
end
|
#validate_and_update_products(force_update: false) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'app/models/katello/sync_plan.rb', line 180
def validate_and_update_products(force_update: false)
sync_plan_products = Product.where(id: self.product_ids).select { |p| p.enabled? }
return if sync_plan_products.length == self.product_ids.length
sync_plan_product_ids = sync_plan_products.pluck(:id)
if sync_plan_product_ids.length < self.product_ids.length
missing_ids = self.product_ids - sync_plan_product_ids
Rails.logger.warn "Sync plan products with following ids are either disabled or don't exist: #{missing_ids}"
if force_update
Rails.logger.info "Updating sync plan with valid and enabled product ids: #{sync_plan_product_ids}"
self.product_ids = sync_plan_product_ids
self.save!
else
Rails.logger.warn "Some sync plan products are invalid/disabled. Please run validate_and_update_products(force_update: true) on the sync_plan from `foreman-rake console`"
end
end
end
|
#validate_sync_date ⇒ Object
128
129
130
|
# File 'app/models/katello/sync_plan.rb', line 128
def validate_sync_date
errors.add :base, _("Start Date and Time can't be blank") if self.sync_date.nil?
end
|