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'
DAILY =
'daily'
WEEKLY =
'weekly'
TYPES =
[HOURLY, DAILY, WEEKLY]
DURATION =
{HOURLY => 'T1H', DAILY => 'T24H', WEEKLY => '7D' }
WEEK_DAYS =
(%w(Sunday Monday Tuesday Wednesday Thursday Friday)).collect { |d| N_(d) }

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authorization::SyncPlan

#deletable?, #editable?, #readable?

Methods included from Glue

included, logger

Methods inherited from Model

#destroy!

Class Method Details

.humanize_class_name(_name = nil) ⇒ Object



113
114
115
# File 'app/models/katello/sync_plan.rb', line 113

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

Instance Method Details

#next_syncObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/katello/sync_plan.rb', line 78

def next_sync
  return nil unless self.enabled

  now = Time.now.utc
  next_sync = self.sync_date

  if self.sync_date < now
    hours = self.sync_date.hour - now.hour
    minutes = self.sync_date.min - now.min
    seconds = self.sync_date.sec - now.sec

    next_sync = nil

    case self.interval
    when HOURLY
      if self.sync_date.min < now.min
        minutes += 60
      end
      next_sync = now.advance(:minutes => minutes, :seconds => seconds)
    when DAILY
      sync_time = Time.at(self.sync_date.hour * 60 * 60 + self.sync_date.min * 60 + self.sync_date.sec)
      now_time = Time.at(now.hour * 60 * 60 + now.min * 60 + now.sec)
      if sync_time < now_time
        hours += 24
      end
      next_sync = now.advance(:hours => hours, :minutes => minutes, :seconds => seconds)
    when WEEKLY
      days = 7 + self.sync_date.wday - now.wday
      next_sync = now.change(:hour => self.sync_date.hour, :min => self.sync_date.min,
                             :sec => self.sync_date.sec).advance(:days => days)
    end
  end
  next_sync
end

#plan_date(localtime = true) ⇒ Object



46
47
48
49
# File 'app/models/katello/sync_plan.rb', line 46

def plan_date(localtime = true)
  date_obj = localtime ? self.zone_converted : self.sync_date
  date_obj.strftime('%m/%d/%Y')
end

#plan_date_time(localtime = true) ⇒ Object



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

def plan_date_time(localtime = true)
  date_obj = localtime ? self.zone_converted : self.sync_date
  date_obj.strftime('%m/%d/%Y %I:%M:%p')
end

#plan_dayObject



42
43
44
# File 'app/models/katello/sync_plan.rb', line 42

def plan_day
  WEEK_DAYS[self.sync_date.strftime('%A').to_i]
end

#plan_time(localtime = true) ⇒ Object



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

def plan_time(localtime = true)
  date_obj = localtime ? self.zone_converted : self.sync_date
  date_obj.strftime('%I:%M %p')
end

#plan_zoneObject



74
75
76
# File 'app/models/katello/sync_plan.rb', line 74

def plan_zone
  self.sync_date.strftime('%Z')
end

#schedule_formatObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/katello/sync_plan.rb', line 61

def schedule_format
  if (self.interval != DURATION[self.interval])
    format = self.sync_date.iso8601 << "/P" << DURATION[self.interval]
  else
    if self.sync_date < Time.now
      format = nil # do not schedule tasks in past
    else
      format = "R1/" << self.sync_date.iso8601 << "/P1D"
    end
  end
  return format
end

#validate_sync_dateObject



33
34
35
# File 'app/models/katello/sync_plan.rb', line 33

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

#zone_convertedObject



37
38
39
40
# File 'app/models/katello/sync_plan.rb', line 37

def zone_converted
  #convert time to local timezone
  self.sync_date.localtime.to_datetime
end