jobs - logging and error management

This commit is contained in:
bunkerity 2021-07-20 12:14:50 +02:00
parent fccf14627f
commit 2fca4cd014
No known key found for this signature in database
GPG Key ID: 3D80806F12602A7C
1 changed files with 29 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import abc, requests, redis, os
import abc, requests, redis, os, datetime, traceback
class Job(abc.ABC) :
@ -9,14 +9,29 @@ class Job(abc.ABC) :
self.__redis = None
if redis_host != None :
self.__redis = redis.Redis(host=redis_host, port=6379, db=0)
try :
self.__redis.echo("test")
except :
self.__log("can't connect to redis host " + redis_host)
self.__type = type
self.__regex = regex
def __log(self, data) :
when = datetime.datetime.today().strftime("[%Y-%m-%d %H:%M:%S]")
what = self.__name + " - " + data + "\n"
with open("/var/log/nginx/jobs.log", "a") as f :
f.write(when + " " + what)
def run(self) :
if self.__type == "line" or self.__type == "file" :
self.__external()
elif self.__type == "exec" :
self.__exec()
try :
if self.__type == "line" or self.__type == "file" :
self.__external()
elif self.__type == "exec" :
self.__exec()
except Exception as e :
self.__log("exception while running job : " + traceback.format_exc())
return False
return True
def __external(self) :
if self.__redis == None :
@ -54,10 +69,18 @@ class Job(abc.ABC) :
def __download_data(self, url) :
r = requests.get(url, stream=True)
if not r or r.status_code != 200 :
return False
raise Exception("can't download data at " + url)
if self.__type == "line" :
return r.iter_lines()
return r.iter_content(chunk_size=8192)
def __exec(self) :
proc = subprocess.run(self.__data, capture_output=True)
stdout = proc.stdout.decode("ascii")
stderr = proc.stderr.decode("err")
if len(stdout) > 1 :
self.__log("stdout = " + stdout)
if len(stderr) > 1 :
self.__log("stderr = " + stderr)
if proc.returncode != 0 :
raise Exception("error code " + str(proc.returncode))