Created
August 19, 2019 17:39
-
-
Save ranaroussi/4f988cdf60602d47177f177102ddead4 to your computer and use it in GitHub Desktop.
Python Job Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
import time | |
import sys | |
import logging | |
class Job: | |
""" | |
monitors that a job is running and executing it when it's not | |
Usage | |
----- | |
import runner | |
def constraint(): | |
# run checkes | |
return False | |
if __name__ == "__main__": | |
job = runner.Job("python file2run.py") | |
job.run_until(constraint, verbose=True) | |
""" | |
@staticmethod | |
def createLogger(name, level=logging.WARNING): | |
logger = logging.getLogger(name) | |
logger.setLevel(level) | |
handler = logging.StreamHandler() | |
handler.setFormatter(logging.Formatter( | |
'%(asctime)s [%(levelname)s] %(name)s: %(message)s')) | |
logger.addHandler(handler) | |
logger.propagate = False | |
return logger | |
def __init__(self, job, loglevel="INFO"): | |
self.job = job[0] if isinstance(job, list) else job | |
loglevel = "WARNING" if loglevel is None else loglevel | |
self.logger = self.createLogger( | |
'Job', getattr(logging, loglevel.upper())) | |
self.logger.info('Initializing: `%s`' % self.job) | |
def run_until(self, constraint=None, verbose=False): | |
if constraint is None: | |
constraint = True | |
try: | |
while True: | |
running = self._grep() | |
if not running: | |
if not constraint: | |
sys.exit() | |
pid = self._exec() | |
if verbose: | |
self.logger.warning("Not Running. Restarting...") | |
self.logger.info("Executing (pid #%.f)" % pid) | |
time.sleep(1) | |
except (KeyboardInterrupt, SystemExit): | |
print("\n\n>>> Interrupted with Ctrl-c...") | |
sys.exit(1) | |
def run_forever(self, verbose=False): | |
try: | |
while True: | |
running = self._grep() | |
if not running: | |
pid = self._exec() | |
if verbose: | |
self.logger.warning("Not Running. Restarting...") | |
self.logger.info("Executing (pid #%.f)" % pid) | |
time.sleep(1) | |
except (KeyboardInterrupt, SystemExit): | |
print("\n\n>>> Interrupted with Ctrl-c...") | |
sys.exit(1) | |
def _exec(self): | |
return subprocess.Popen( | |
[self.job], shell=True, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE).pid | |
def _grep(self): | |
process = subprocess.Popen( | |
['ps -ef | grep "%s"' % self.job], shell=True, | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
my_pid, err = process.communicate() | |
return len(my_pid.splitlines()) > 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment