linspector-old/lib/core/job.py

105 lines
2.7 KiB
Python
Raw Normal View History

2013-05-16 23:03:00 +02:00
"""
2013-05-27 20:27:57 +02:00
This is what job_function needs as parameter for each job to successfully
2013-05-16 23:03:00 +02:00
execute.
"""
2013-08-14 20:36:35 +02:00
2013-07-02 23:30:31 +02:00
from datetime import datetime
2013-05-30 04:20:35 +02:00
2013-08-13 22:48:51 +02:00
2013-05-29 22:52:54 +02:00
def generateId():
2013-05-30 04:20:35 +02:00
i = 0
2013-05-29 22:52:54 +02:00
while True:
2013-05-30 04:20:35 +02:00
yield i
i += 1
2013-08-14 20:36:35 +02:00
2013-07-02 23:30:31 +02:00
class Job:
def __init__(self, service, host, members, processors):
2013-06-21 02:20:58 +02:00
self.service = service
2013-08-14 00:39:45 +02:00
self.host = host
self.members = members
self.processors = processors
2013-07-02 23:30:31 +02:00
self.jobInfos = []
2013-08-14 00:39:45 +02:00
self.jobThreshold = 0
2013-07-02 23:30:31 +02:00
2013-05-27 01:44:28 +02:00
def __str__(self):
2013-07-02 23:30:31 +02:00
return str(self.__dict__)
2013-06-21 02:20:58 +02:00
def set_logger(self, log):
self.log = log
2013-06-21 02:20:58 +02:00
def set_job(self, job):
self.job = job
2013-08-14 00:39:45 +02:00
def handle_threshold(self, serviceThreshold, executionSucessful):
if executionSucessful:
2013-08-15 01:50:26 +02:00
if self.jobThreshold > 0:
self.jobThreshold -= 1
2013-08-14 00:39:45 +02:00
else:
self.jobThreshold += 1
if self.jobThreshold >= serviceThreshold:
self.log.d("Threshold reached!")
self.handle_alarm(self.jobThreshold - serviceThreshold)
2013-08-14 00:39:45 +02:00
def handle_alarm(self, thresholdOffset):
for member in self.service.get_hostgroup().get_members():
for task in member.get_tasks():
print(task)
task.execute("Task executed for host: " + self.host)
2013-08-14 00:39:45 +02:00
pass
2013-06-21 02:20:58 +02:00
def handle_call(self):
2013-05-29 22:52:54 +02:00
self.log.d("handle call")
2013-06-21 02:20:58 +02:00
self.log.d(self.service)
2013-08-14 00:39:45 +02:00
try:
jobInfo = JobInfo(self.host, self.service)
2013-08-15 00:35:16 +02:00
self.service._execute(jobInfo)
2013-08-14 00:39:45 +02:00
jobInfo.set_execution_end()
2013-07-02 00:19:20 +02:00
2013-08-15 00:35:16 +02:00
self.handle_threshold(self.service.get_threshold(), jobInfo.was_execution_successful())
2013-07-02 23:30:31 +02:00
self.log.d("Code: " + str(jobInfo.get_errorcode()) + ", Message: " + str(jobInfo.get_message()))
2013-08-15 00:47:40 +02:00
2013-08-14 00:39:45 +02:00
self.jobInfos.append(jobInfo)
except Exception, e:
self.log.d(e)
2013-07-02 23:30:31 +02:00
2013-08-14 20:36:35 +02:00
2013-08-15 00:35:16 +02:00
class JobInfo(object):
2013-07-02 23:30:31 +02:00
def __init__(self, host, service):
self.id = generateId()
self.host = host
self.service = service
self.executionBegin = datetime.now()
2013-08-15 00:35:16 +02:00
self._errorcode = -1
self._message = None
self._executionSuccess = False
def get_host(self):
return self.host
2013-07-02 23:30:31 +02:00
def set_result(self, result):
self.result = result
def set_execution_end(self):
self.executionEnd = datetime.now()
def set_execution_successful(self, successful):
2013-08-15 00:35:16 +02:00
self._executionSuccess = successful
def was_execution_successful(self):
return self._executionSuccess
def set_message(self, msg):
self._message = msg
def get_message(self):
return self._message
def set_errorcode(self, errcode):
self._errorcode = errcode
def get_errorcode(self):
return self._errorcode