Class: Katello::SyncPlan

Inherits:
Model
  • Object
show all
Includes:
ForemanTasks::Concerns::ActionSubject, Authorization::SyncPlan, Glue
Defined in:
app/models/katello/sync_plan.rb

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

Methods included from Authorization::SyncPlan

#deletable?, #editable?, #readable?, #syncable?

Methods included from Glue

logger

Methods inherited from Model

#destroy!

Class Method Details

.humanize_class_name(_name = nil) ⇒ Object



144
145
146
# File 'app/models/katello/sync_plan.rb', line 144

def self.humanize_class_name(_name = nil)
  _("Sync Plans")
end

.remove_disabled_product(repository) ⇒ Object



49
50
51
52
53
54
# File 'app/models/katello/sync_plan.rb', line 49

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/katello/sync_plan.rb', line 36

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/katello/sync_plan.rb', line 148

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
  if (interval.downcase.eql? "hourly")
    cron = min.to_s + " * * * *"
  elsif (interval.downcase.eql? "daily")
    cron = min.to_s + " " + hour.to_s + " * * *"
  elsif (interval.downcase.eql? "weekly")
    cron = min.to_s + " " + hour.to_s + " * * " + day.to_s
  elsif (interval.downcase.eql? 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_logicObject



93
94
95
# File 'app/models/katello/sync_plan.rb', line 93

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_logicObject



123
124
125
# File 'app/models/katello/sync_plan.rb', line 123

def cancel_recurring_logic
  self.foreman_tasks_recurring_logic&.cancel
end

#cron_status_mismatch?Boolean



178
179
180
# File 'app/models/katello/sync_plan.rb', line 178

def cron_status_mismatch?
  self.interval != CUSTOM_CRON && !(self.cron_expression.nil? || self.cron_expression.eql?(''))
end

#custom_cron_interval_expressionObject



62
63
64
# File 'app/models/katello/sync_plan.rb', line 62

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

#enabledObject



115
116
117
# File 'app/models/katello/sync_plan.rb', line 115

def enabled
  enabled?
end

#enabled=(value) ⇒ Object



119
120
121
# File 'app/models/katello/sync_plan.rb', line 119

def enabled=(value)
  self.foreman_tasks_recurring_logic.enabled = value
end

#enabled?Boolean



111
112
113
# File 'app/models/katello/sync_plan.rb', line 111

def enabled?
  self.foreman_tasks_recurring_logic && self.foreman_tasks_recurring_logic.state == 'active'
end

#next_syncObject



139
140
141
142
# File 'app/models/katello/sync_plan.rb', line 139

def next_sync
  return nil unless self.enabled?
  self.foreman_tasks_recurring_logic&.tasks&.order(:start_at)&.last&.start_at
end

#product_enabledObject



56
57
58
59
60
# File 'app/models/katello/sync_plan.rb', line 56

def product_enabled
  products.each do |product|
    errors.add :base, _("Can not add product %s because it is disabled.") % product.name unless product.enabled?
  end
end

#rec_logic_changed?Boolean



174
175
176
# File 'app/models/katello/sync_plan.rb', line 174

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



66
67
68
69
70
71
72
73
# File 'app/models/katello/sync_plan.rb', line 66

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_logicObject



101
102
103
104
105
106
107
108
109
# File 'app/models/katello/sync_plan.rb', line 101

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_tzObject



131
132
133
134
135
136
137
# File 'app/models/katello/sync_plan.rb', line 131

def sync_date_sans_tz
  if User.current.try(:timezone)
    return self.sync_date.strftime('%a, %d %b %Y %H:%M:%S')
  else
    sync_date
  end
end

#toggle_enabled(value = false) ⇒ Object



97
98
99
# File 'app/models/katello/sync_plan.rb', line 97

def toggle_enabled(value = false)
  self.foreman_tasks_recurring_logic.enabled = value
end

#update_attributes_with_logics!(params) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/katello/sync_plan.rb', line 75

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_sync_dateObject



127
128
129
# File 'app/models/katello/sync_plan.rb', line 127

def validate_sync_date
  errors.add :base, _("Start Date and Time can't be blank") if self.sync_date.nil?
end