linspector-old/lib/core/job.py

122 lines
3.6 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-10-08 00:08:47 +02:00
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-05-16 23:03:00 +02:00
"""
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, core):
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
self.core = core
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
def handle_threshold(self, jobInfo, serviceThreshold, executionSucessful):
2013-08-14 00:39:45 +02:00
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.debug("Threshold reached!")
self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold)
2013-08-14 00:39:45 +02:00
def handle_alarm(self, jobInfo, thresholdOffset):
for member in self.service.get_hostgroup().get_members():
2013-10-01 06:20:48 +02:00
#TODO: Put Tasks in a run queue and execute them in a background thread. FIFO! Reduces delay in core.
for task in member.get_tasks():
task.execute(jobInfo.get_message(), self.core)
2013-08-14 00:39:45 +02:00
2013-06-21 02:20:58 +02:00
def handle_call(self):
self.log.debug("handle call")
self.log.debug(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
self.handle_threshold(jobInfo, self.service.get_threshold(), jobInfo.was_execution_successful())
2013-07-02 23:30:31 +02:00
self.log.debug("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.debug(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