Mercurial > treepkg
changeset 449:c1d5a6cb5cb0
added jobdaemon
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Tue, 10 Aug 2010 13:46:11 +0000 |
parents | 12f3642e1756 |
children | 5c06e0a0d329 |
files | bin/jobdaemon.cfg bin/jobdaemon.py |
diffstat | 2 files changed, 116 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/jobdaemon.cfg Tue Aug 10 13:46:11 2010 +0000 @@ -0,0 +1,6 @@ +# jobdaemon.cfg examlpe +# back tasks of treepkg +/path/to/treepkg1/bin/publishdebianpackages.py +/path/to/treepkg2/bin/publishdebianpackages.py +# tasks to run synchronously +BLOCKER: update-apt-files.sh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/jobdaemon.py Tue Aug 10 13:46:11 2010 +0000 @@ -0,0 +1,110 @@ +#!/usr/bin/env python + +import os.path +import sys +import shlex +import traceback +import time + +from optparse import OptionParser + +CONFIG_FILE = os.environ.get("JOBDAEMON_CFG", "jobdaemon.cfg") + +SLEEP = int(os.environ.get("JOBDAEMON_SLEEP", "60")) + +def fork(job): + pid = os.fork() + if pid: return pid + os.execv(job[0], job) + return -1 # never reached + +class Jobs(object): + + def __init__(self, filename): + self.load_config(filename) + + def load_config(self, filename): + print >> sys.stderr, "loading config file '%s'" % filename + plan, jobs = [], [] + f = open(filename, "r") + try: + while True: + line = f.readline() + if not line: break + line = line.strip() + if not line or line.startswith("#"): + continue + if line.startswith("BLOCKER:"): + line = line[len("BLOCKER:"):].strip() + plan.append((jobs, line)) + jobs = [] + continue + jobs.append(shlex.split(line)) + finally: + f.close() + + if jobs: plan.append((jobs, None)) + + self.plan = plan + + def run_jobs(self, jobs): + pids = [] + for job in jobs: + try: + pids.append(fork(job)) + except: + traceback.print_exc() + + for pid in pids: + try: + os.waitpid(pid, 0) + except: + traceback.print_exc() + + def run(self): + for jobs, blocker in self.plan: + self.run_jobs(jobs) + if blocker: + try: + os.system(blocker) + except: + traceback.print_exc() + +def main(): + + global config_changed + + parser = OptionParser() + + parser.add_option( + "-c", "--config", dest="config", + help="load configuration from file", + metavar="FILE", default=CONFIG_FILE) + + parser.add_option( + "-s", "--sleep", dest="sleep_time", + help="sleep time between runs", + type="int", + metavar="TIME", default=SLEEP) + + (options, _) = parser.parse_args() + + if not os.path.isfile(options.config): + print >> sys.stderr, "config file '%s' does not exists." + sys.exit(1) + + modtime = os.stat(options.config).st_mtime + + jobs = Jobs(options.config) + + while True: + if os.path.isfile(options.config): + nmodtime = os.stat(options.config).st_mtime + if nmodtime > modtime: + modtime = nmodtime + jobs = Jobs(options.config) + jobs.run() + time.sleep(options.sleep_time) + +if __name__ == '__main__': + main()