[Treepkg-commits] r409 - trunk/bin

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Aug 10 15:46:11 CEST 2010


Author: bricks
Date: 2010-08-10 15:46:11 +0200 (Tue, 10 Aug 2010)
New Revision: 409

Added:
   trunk/bin/jobdaemon.cfg
   trunk/bin/jobdaemon.py
Log:
added jobdaemon


Added: trunk/bin/jobdaemon.cfg
===================================================================
--- trunk/bin/jobdaemon.cfg	2010-08-10 10:31:40 UTC (rev 408)
+++ trunk/bin/jobdaemon.cfg	2010-08-10 13:46:11 UTC (rev 409)
@@ -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

Added: trunk/bin/jobdaemon.py
===================================================================
--- trunk/bin/jobdaemon.py	2010-08-10 10:31:40 UTC (rev 408)
+++ trunk/bin/jobdaemon.py	2010-08-10 13:46:11 UTC (rev 409)
@@ -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()


Property changes on: trunk/bin/jobdaemon.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the Treepkg-commits mailing list