aboutsummaryrefslogtreecommitdiff
path: root/jobs/PeerChecker.py
diff options
context:
space:
mode:
authorTom Ritter <tom@ritter.vg>2016-01-31 13:22:08 -0600
committerTom Ritter <tom@ritter.vg>2016-01-31 13:22:08 -0600
commit3bea3bae59e7404b286b5bf97a6270270bfadd6c (patch)
treec9d1d7cf76681420587198d09abda761912c47c4 /jobs/PeerChecker.py
parent1a8b46d940d3a4bc06700d15307191bb10008ea6 (diff)
Refactor lots of things to allow you to be notified every so often, instead of every single time.
Diffstat (limited to 'jobs/PeerChecker.py')
-rwxr-xr-xjobs/PeerChecker.py61
1 files changed, 36 insertions, 25 deletions
diff --git a/jobs/PeerChecker.py b/jobs/PeerChecker.py
index f17da53..8211472 100755
--- a/jobs/PeerChecker.py
+++ b/jobs/PeerChecker.py
@@ -9,46 +9,57 @@ import requests
import JobBase
-class PeerChecker(JobBase.JobBase):
- def executeEvery(self):
- return JobBase.JobFrequency.HOUR
- def execute(self):
- testSuccess = True
- peers = self.config.items('peers')
- for p in peers:
- peer = p[1].split(',')
+class PeerChecker(JobSpawner.JobSpawner):
+ class IndividualPeerChecker(JobBase.JobBase):
+ def __init__(self, config, checkurl, notificationAddress):
+ self.checkurl = checkurl
+ self.notificationAddress = notificationAddress
+
+ def executeEvery(self):
+ return JobBase.JobFrequency.HOUR
+ def notifyOnFailureEvery(self):
+ return JobBase.JobFailureNotificationFrequency.EVERYTIME
+ def execute(self):
peerOK = False
- subject = ""
- body = ""
+ self.subject = ""
+ self.body = ""
try:
- response = requests.get(peer[0])
+ response = requests.get(self.checkurl)
if response.status_code != 200:
peerOK = False
- subject = peer[0] + " returned a non-standard status code."
- body = str(response.status_code) + "\n" + response.content
+ self.subject = self.checkurl + " returned a non-standard status code."
+ self.body = str(response.status_code) + "\n" + response.content
else:
if "True" in response.content:
peerOK = True
elif "MailProblem" in response.content:
peerOK = False
- subject = peer[0] + " reports it cannot send email."
- body = str(response.status_code) + "\n" + response.content
+ self.subject = self.checkurl + " reports it cannot send email."
+ self.body = str(response.status_code) + "\n" + response.content
elif "JobProblem" in response.content:
peerOK = False
- subject = peer[0] + " reports its jobs are not running."
- body = str(response.status_code) + "\n" + response.content
+ self.subject = self.checkurl + " reports its jobs are not running."
+ self.body = str(response.status_code) + "\n" + response.content
else:
peerOK = False
- subject = peer[0] + " had an unexpected response."
- body = str(response.status_code) + "\n" + response.content
+ self.subject = self.checkurl + " had an unexpected response."
+ self.body = str(response.status_code) + "\n" + response.content
except Exception as e:
peerOK = False
- subject = peer[0] + " is not responding."
- body = str(e)
+ self.subject = self.checkurl + " is not responding."
+ self.body = str(e)
+ return peerOK
- if not peerOK:
- if not self.sendEmail(subject, body, peer[1]):
- testSuccess = False
- return testSuccess
+ return peerOK:
+ def onFailure(self):
+ return self.sendEmail(self.subject, self.body, self.notificationAddress)
+ def onStateChangeSuccess(self):
+ return self.sendEmail("Successfully hit " + self.checkurl, "", self.notificationAddress)
+
+ def get_sub_jobs(self, config):
+ peers = config.items('peers')
+ for p in peers:
+ (address, email) = p[1].split(',')
+ yield self.IndividualPeerChecker(config, address, email)