aboutsummaryrefslogtreecommitdiff
path: root/jobstate.py
blob: ba5e2052c00d0e72ca5f0113dd82e8ee95137897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python

import time
import logging
import datetime

class JobState:
	def __init__(self, name):
		self.name = name
		self.CurrentStateSuccess = True
		self.FirstFailureTime = 0
		self.LastNotifyTime = 0

	def markFailedAndNotify(self):
		if self.CurrentStateSuccess:
			self.CurrentStateSuccess = False
			self.FirstFailureTime = time.time()
			self.LastNotifyTime = self.FirstFailureTime
		else:
			self.LastNotifyTime = time.time()

	def markFailedNoNotify(self):
		if self.CurrentStateSuccess:
			logging.warn("Somehow we called markFailedNoNotify, on a success condition, without notifying the user")
			self.CurrentStateSuccess = False
			self.FirstFailureTime = time.time()
			self.LastNotifyTime = 0
		else:
			pass

	def markSuccessful(self):
		if self.CurrentStateSuccess:
			pass
		else:
			self.CurrentStateSuccess = True
			self.FirstFailureTime = 0
			self.LastNotifyTime = 0

	def serialize(self):
		ret  = self.name + "|" 
		ret += "Succeeding" if self.CurrentStateSuccess else "Failing"
		ret += "|" + str(self.FirstFailureTime)
		ret += "|" + str(self.LastNotifyTime) + "\n"
		return ret

	@staticmethod
	def Parse(line):
		s = JobState("")

		line = line.strip()
		parts = line.split("|")

		s.name = parts[0]
		s.CurrentStateSuccess = True if parts[1] == "Succeeding" else False
		s.FirstFailureTime = float(parts[2])
		s.LastNotifyTime = float(parts[3])

		return s

	@staticmethod
	def Empty(name):
		s = JobState(name)
		return s