Revision e52e0ddc lib/watcher/state.py

b/lib/watcher/state.py
38 38
# counter, so 8 hours (16*1/2h) seems like a reasonable reset time
39 39
RETRY_EXPIRATION = 8 * 3600
40 40

  
41
KEY_CLEANUP_COUNT = "cleanup_count"
42
KEY_CLEANUP_WHEN = "cleanup_when"
41 43
KEY_RESTART_COUNT = "restart_count"
42 44
KEY_RESTART_WHEN = "restart_when"
43 45
KEY_BOOT_ID = "bootid"
......
162 164

  
163 165
    return 0
164 166

  
167
  def NumberOfCleanupAttempts(self, instance_name):
168
    """Returns number of previous cleanup attempts.
169

  
170
    @type instance_name: string
171
    @param instance_name: the name of the instance to look up
172

  
173
    """
174
    idata = self._data["instance"]
175

  
176
    if instance_name in idata:
177
      return idata[instance_name][KEY_CLEANUP_COUNT]
178

  
179
    return 0
180

  
165 181
  def MaintainInstanceList(self, instances):
166 182
    """Perform maintenance on the recorded instances.
167 183

  
......
185 201
      logging.debug("Expiring record for instance %s", inst)
186 202
      idict.pop(inst, None)
187 203

  
204
  @staticmethod
205
  def _RecordAttempt(instances, instance_name, key_when, key_count):
206
    """Record an event.
207

  
208
    @type instances: dict
209
    @param instances: contains instance data indexed by instance_name
210

  
211
    @type instance_name: string
212
    @param instance_name: name of the instance involved in the event
213

  
214
    @type key_when:
215
    @param key_when: dict key for the information for when the event occurred
216

  
217
    @type key_count: int
218
    @param key_count: dict key for the information for how many times
219
                      the event occurred
220

  
221
    """
222
    instance = instances.setdefault(instance_name, {})
223
    instance[key_when] = time.time()
224
    instance[key_count] = instance.get(key_count, 0) + 1
225

  
188 226
  def RecordRestartAttempt(self, instance_name):
189 227
    """Record a restart attempt.
190 228

  
......
192 230
    @param instance_name: the name of the instance being restarted
193 231

  
194 232
    """
195
    idata = self._data["instance"]
233
    self._RecordAttempt(self._data["instance"], instance_name,
234
                        KEY_RESTART_WHEN, KEY_RESTART_COUNT)
235

  
236
  def RecordCleanupAttempt(self, instance_name):
237
    """Record a cleanup attempt.
196 238

  
197
    inst = idata.setdefault(instance_name, {})
198
    inst[KEY_RESTART_WHEN] = time.time()
199
    inst[KEY_RESTART_COUNT] = inst.get(KEY_RESTART_COUNT, 0) + 1
239
    @type instance_name: string
240
    @param instance_name: the name of the instance being cleaned up
241

  
242
    """
243
    self._RecordAttempt(self._data["instance"], instance_name,
244
                        KEY_CLEANUP_WHEN, KEY_CLEANUP_COUNT)
200 245

  
201 246
  def RemoveInstance(self, instance_name):
202 247
    """Update state to reflect that a machine is running.

Also available in: Unified diff