annotate bin/jobdaemon.py @ 460:10d4cbffcc07

corrected link back to overview in detail view of sawmill
author Sascha Teichmann <teichmann@intevation.de>
date Fri, 27 Aug 2010 09:50:04 +0000
parents c1d5a6cb5cb0
children
rev   line source
449
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
1 #!/usr/bin/env python
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
2
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
3 import os.path
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
4 import sys
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
5 import shlex
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
6 import traceback
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
7 import time
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
8
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
9 from optparse import OptionParser
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
10
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
11 CONFIG_FILE = os.environ.get("JOBDAEMON_CFG", "jobdaemon.cfg")
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
12
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
13 SLEEP = int(os.environ.get("JOBDAEMON_SLEEP", "60"))
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
14
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
15 def fork(job):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
16 pid = os.fork()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
17 if pid: return pid
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
18 os.execv(job[0], job)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
19 return -1 # never reached
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
20
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
21 class Jobs(object):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
22
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
23 def __init__(self, filename):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
24 self.load_config(filename)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
25
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
26 def load_config(self, filename):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
27 print >> sys.stderr, "loading config file '%s'" % filename
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
28 plan, jobs = [], []
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
29 f = open(filename, "r")
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
30 try:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
31 while True:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
32 line = f.readline()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
33 if not line: break
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
34 line = line.strip()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
35 if not line or line.startswith("#"):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
36 continue
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
37 if line.startswith("BLOCKER:"):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
38 line = line[len("BLOCKER:"):].strip()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
39 plan.append((jobs, line))
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
40 jobs = []
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
41 continue
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
42 jobs.append(shlex.split(line))
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
43 finally:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
44 f.close()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
45
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
46 if jobs: plan.append((jobs, None))
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
47
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
48 self.plan = plan
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
49
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
50 def run_jobs(self, jobs):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
51 pids = []
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
52 for job in jobs:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
53 try:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
54 pids.append(fork(job))
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
55 except:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
56 traceback.print_exc()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
57
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
58 for pid in pids:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
59 try:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
60 os.waitpid(pid, 0)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
61 except:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
62 traceback.print_exc()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
63
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
64 def run(self):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
65 for jobs, blocker in self.plan:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
66 self.run_jobs(jobs)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
67 if blocker:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
68 try:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
69 os.system(blocker)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
70 except:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
71 traceback.print_exc()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
72
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
73 def main():
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
74
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
75 global config_changed
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
76
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
77 parser = OptionParser()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
78
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
79 parser.add_option(
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
80 "-c", "--config", dest="config",
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
81 help="load configuration from file",
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
82 metavar="FILE", default=CONFIG_FILE)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
83
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
84 parser.add_option(
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
85 "-s", "--sleep", dest="sleep_time",
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
86 help="sleep time between runs",
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
87 type="int",
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
88 metavar="TIME", default=SLEEP)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
89
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
90 (options, _) = parser.parse_args()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
91
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
92 if not os.path.isfile(options.config):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
93 print >> sys.stderr, "config file '%s' does not exists."
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
94 sys.exit(1)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
95
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
96 modtime = os.stat(options.config).st_mtime
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
97
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
98 jobs = Jobs(options.config)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
99
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
100 while True:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
101 if os.path.isfile(options.config):
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
102 nmodtime = os.stat(options.config).st_mtime
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
103 if nmodtime > modtime:
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
104 modtime = nmodtime
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
105 jobs = Jobs(options.config)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
106 jobs.run()
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
107 time.sleep(options.sleep_time)
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
108
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
109 if __name__ == '__main__':
c1d5a6cb5cb0 added jobdaemon
Bjoern Ricks <bricks@intevation.de>
parents:
diff changeset
110 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)