Class: ForemanPatch::Plan

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ForemanTasks::Concerns::ActionSubject
Defined in:
app/models/foreman_patch/plan.rb

Defined Under Namespace

Classes: Jail

Constant Summary collapse

UNITS =
['days', 'weeks', 'months', 'years'].freeze
CORRECTIONS =
['weekday', 'last_day', 'last_weekday', 'avoid_weekend'].freeze

Instance Method Summary collapse

Instance Method Details

#end_dateObject



26
27
28
# File 'app/models/foreman_patch/plan.rb', line 26

def end_date
  next_cycle_start - 1.day
end

#frequencyObject



30
31
32
# File 'app/models/foreman_patch/plan.rb', line 30

def frequency
  interval.send(units)
end

#iterateObject



50
51
52
53
54
55
56
# File 'app/models/foreman_patch/plan.rb', line 50

def iterate
  count = tasks.where(state: 'scheduled').count

  if (count < active_count - 1) or active_count == 0
    ForemanTasks.async_task(Actions::ForemanPatch::Cycle::Create, self)
  end
end

#next_cycle_startObject



44
45
46
47
48
# File 'app/models/foreman_patch/plan.rb', line 44

def next_cycle_start
  next_date = start_date + frequency
  next_date = send(correction, next_date) unless correction.blank?
  next_date
end

#stopObject



38
39
40
41
42
# File 'app/models/foreman_patch/plan.rb', line 38

def stop
  self.active_count = 0
  save!
  tasks.active.each(&:cancel)
end

#stopped?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/foreman_patch/plan.rb', line 34

def stopped?
  active_count == 0
end

#to_paramsObject



58
59
60
61
62
63
64
# File 'app/models/foreman_patch/plan.rb', line 58

def to_params
  {
    name: ForemanPatch::CycleNameGenerator.generate(self),
    start_date: start_date,
    end_date: next_cycle_start - 1.day,
  }
end