jobs - logging and error management
This commit is contained in:
parent
fccf14627f
commit
2fca4cd014
35
jobs/Job.py
35
jobs/Job.py
@ -1,4 +1,4 @@
|
|||||||
import abc, requests, redis, os
|
import abc, requests, redis, os, datetime, traceback
|
||||||
|
|
||||||
class Job(abc.ABC) :
|
class Job(abc.ABC) :
|
||||||
|
|
||||||
@ -9,14 +9,29 @@ class Job(abc.ABC) :
|
|||||||
self.__redis = None
|
self.__redis = None
|
||||||
if redis_host != None :
|
if redis_host != None :
|
||||||
self.__redis = redis.Redis(host=redis_host, port=6379, db=0)
|
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.__type = type
|
||||||
self.__regex = regex
|
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) :
|
def run(self) :
|
||||||
if self.__type == "line" or self.__type == "file" :
|
try :
|
||||||
self.__external()
|
if self.__type == "line" or self.__type == "file" :
|
||||||
elif self.__type == "exec" :
|
self.__external()
|
||||||
self.__exec()
|
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) :
|
def __external(self) :
|
||||||
if self.__redis == None :
|
if self.__redis == None :
|
||||||
@ -54,10 +69,18 @@ class Job(abc.ABC) :
|
|||||||
def __download_data(self, url) :
|
def __download_data(self, url) :
|
||||||
r = requests.get(url, stream=True)
|
r = requests.get(url, stream=True)
|
||||||
if not r or r.status_code != 200 :
|
if not r or r.status_code != 200 :
|
||||||
return False
|
raise Exception("can't download data at " + url)
|
||||||
if self.__type == "line" :
|
if self.__type == "line" :
|
||||||
return r.iter_lines()
|
return r.iter_lines()
|
||||||
return r.iter_content(chunk_size=8192)
|
return r.iter_content(chunk_size=8192)
|
||||||
|
|
||||||
def __exec(self) :
|
def __exec(self) :
|
||||||
proc = subprocess.run(self.__data, capture_output=True)
|
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))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user