Class: D3::PuppyTime::PuppyQueue

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/d3/puppytime/puppy_queue.rb

Overview

PuppyQ - the current queue of pending puppy installs.

This class only has one real value: A hash of PendingPuppy objects, keyed by basename. But, PendingPuppies can be added and removed and the queue can be read and written to disk

Constant Summary collapse

QUEUE_FILE =

Class Constants #################

D3::SUPPORT_DIR + "d3-pending-puppies.yaml"
DFT_NOTIFICATION_FREQUENCY =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePuppyQueue

start by sett



61
62
63
# File 'lib/d3/puppytime/puppy_queue.rb', line 61

def initialize
  read_q
end

Instance Attribute Details

#qObject (readonly) Also known as: queue, pending_puppies

the queue of PendingPuppy objects A hash, keyed by basename.



56
57
58
# File 'lib/d3/puppytime/puppy_queue.rb', line 56

def q
  @q
end

Instance Method Details

#+(puppy) ⇒ Boolean

Add a puppy to the queue

Parameters:

  • puppy (D3::PendingPupppy)

    the puppy to add.

Returns:

  • (Boolean)

    True if the puppy was queued, false if already in the queue with a same or newer edition.

Raises:

  • (TypeError)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/d3/puppytime/puppy_queue.rb', line 113

def + (puppy)

  raise TypeError, "You can only add PendingPuppy ojects to the PuppyQueue" unless puppy.class == D3::PuppyTime::PendingPuppy

  D3.log "Adding to puppy queue: #{puppy.edition}", :info

  # does this basename already exist in the queue?
  # we can only have one edition per basename in the queue at a time
  if pups.include? puppy.basename
    in_q = @q[puppy.basename]

    # if the pre-queued one is older, replace it with this one.
    if in_q.id < puppy.id
      D3.log "Replacing older puppy in queue: #{in_q.edition}", :warm
      self - puppy.basename
    else
      if puppy.force
        D3.log "Puppy already in queue for '#{puppy.basename}' is the same or newer (#{in_q.edition}), but force-adding", :warn
        self - puppy.basename
      else
        D3.log "Puppy already in queue for '#{puppy.basename}' is the same or newer (#{in_q.edition}), not adding", :warn
        return false
      end # if force
    end
  end # if @pups.include? puppy.basename

  # note if we're starting with an empty queue
  started_empty = @q.empty?

  # Note when we're queueing
  puppy.queued_at = Time.now

  # add the new puppy
  @q[puppy.basename] = puppy
  D3.log "Added puppy to queue: #{puppy.edition}", :info

  # save it
  save_q

  notify_puppies

  return true
end

#-(puppy) ⇒ Boolean

Remove a puppy from the queue

Parameters:

  • puppy (String, D3::PendingPupppy)

    the basename or D3::PendingPupppy for the puppy to be removed

Returns:

  • (Boolean)

    true if it was removed, false if it wasn’t in the queue



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/d3/puppytime/puppy_queue.rb', line 164

def - (puppy)

  puppy = puppy.basename unless puppy.is_a? String

  return false unless pups.include? puppy

  # remove it
  @q.delete puppy
  D3.log "Removed basename #{puppy} from the puppy queue", :debug

  # save the queue
  save_q

  return true
end

#notify_puppies(verbose = false) ⇒ void

This method returns an undefined value.

Run the puppy notification policy, if there is one and if its been long enough since the last run.



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/d3/puppytime/puppy_queue.rb', line 241

def notify_puppies(verbose = false)

  return unless policy = should_run_notification_policy

  # put the queue editions in the ENV
  puppies = @q.values.map{|p| p.edition}.join " "
  D3::Client.set_env :puppytime_notification, puppies

  D3.run_policy policy, :puppy_notification, verbose

  update_last_notification
  D3::Client.unset_env :puppytime_notification
end

#pupsArray<String> Also known as: puppies, basenames

An array of basenames for all pending puppies.

Returns:

  • (Array<String>)

    the basenames of the pending puppies



102
103
104
# File 'lib/d3/puppytime/puppy_queue.rb', line 102

def pups
  @q.keys
end

#read_qvoid Also known as: refresh

This method returns an undefined value.

Read in the current puppy queue from disk or set it to an empty hash if not there.



72
73
74
75
76
77
78
79
80
# File 'lib/d3/puppytime/puppy_queue.rb', line 72

def read_q
  if QUEUE_FILE.exist?
    @q = YAML.load(QUEUE_FILE.read)
    D3.log "Puppy queue loaded from disk", :debug
  else
    @q = {}
    D3.log "Created new empty puppy queue", :debug
  end
end

#save_qvoid

This method returns an undefined value.

Save the current q out to disk



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/d3/puppytime/puppy_queue.rb', line 86

def save_q
  D3.log "Saving puppy queue", :debug
  if @q.empty?
    D3.log "Puppy queue is empty, deleting from disk", :debug
    QUEUE_FILE.delete if QUEUE_FILE.exist?
  else
    QUEUE_FILE.jss_save YAML.dump(@q)
    D3.log "Puppy queue saved to disk", :debug
  end

end

#should_run_notification_policyString, ...

Should we run the puppytime notification policy? returns the policy id or name, or false if we shouldn’t run.

Returns:

  • (String, Integer, false)

    the policy to run, if we should



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/d3/puppytime/puppy_queue.rb', line 186

def should_run_notification_policy
  # no puppies, no notify
  if @q.empty?
    D3.log "Not running puppytime notification policy: No puppies in queue", :debug
    return false
  end

  # no policy, no notify
  unless policy = D3::CONFIG.puppy_notification_policy
    D3.log "Not running puppytime notification policy: No policy in config", :debug
    return false
  end

  # no-notification option was given, no notify
  unless D3::Client.puppy_notification_ok_with_admin?
    D3.log "Not running puppytime notification policy: --no-puppy-notification was given", :debug
    return false
  end

  # how many days between notifications?
  frequency = D3::CONFIG.puppy_notification_frequency
  frequency ||= DFT_NOTIFICATION_FREQUENCY

  case frequency
  # zero means never notify
  when 0
    D3.log "Not running puppytime notification policy: Frequency set to Zero", :debug
    return false
  # -1 means always notify
  when -1
    D3.log "Frequency set to -1, always running puppytime notification policy", :debug
    return policy
  end

  unless last_notification = D3::CONFIG.puppy_last_notification
    # never been notified? always notify.
    D3.log "Puppytime notification policy never run, running now.", :debug
    return policy
  end # if last notiv

  # not long enough since last? no notify
  last_notification_days_ago = ((Time.now - last_notification) / 60 / 60 / 24).to_i
  unless last_notification_days_ago >= frequency
    D3.log "Not running puppytime notification policy: last notification #{last_notification_days_ago}/#{frequency} days ago", :debug
    return false
  end

  return policy
end

#update_last_notificationvoid

This method returns an undefined value.

Update the last notification date in the config to right-now.



260
261
262
263
264
# File 'lib/d3/puppytime/puppy_queue.rb', line 260

def update_last_notification
  D3.log "Updating last puppy notification time", :debug
  D3::CONFIG.puppy_last_notification = Time.now
  D3::CONFIG.save
end