# HG changeset patch # User Bjoern Ricks # Date 1281447971 0 # Node ID c1d5a6cb5cb05edd40ba11af9046f5351e5c06a2 # Parent 12f3642e1756eec12b0116ebb628a1163a0f6b49 added jobdaemon diff -r 12f3642e1756 -r c1d5a6cb5cb0 bin/jobdaemon.cfg --- /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 diff -r 12f3642e1756 -r c1d5a6cb5cb0 bin/jobdaemon.py --- /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()