Mercurial > treepkg
view bin/jobdaemon.py @ 502:35759274aeb4
Expose package time and a short revision to the version templates.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 25 Oct 2010 08:18:21 +0000 |
parents | c1d5a6cb5cb0 |
children |
line wrap: on
line source
#!/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()