summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormartinko2011-11-08 12:39:23 +0000
committermartinko2011-11-08 12:39:23 +0000
commit501f5456af30c465df740996f9bd1c7a6c17b07f (patch)
treebc6ae62bd6f9f5599f6e39a87e1b0d6e1c2885a0
parentf4114d9ffb7c5480e486ee322456a3642217ae97 (diff)
scripts/scriptmgr.py: added --wait option
The new option will make ScriptMgr to wait for all jobs to finish their processing. This applies to both "stop" and "restart" commands. Btw, restarting should be (much) faster now.
-rwxr-xr-xscripts/scriptmgr.py61
1 files changed, 41 insertions, 20 deletions
diff --git a/scripts/scriptmgr.py b/scripts/scriptmgr.py
index 75e33bea..26317036 100755
--- a/scripts/scriptmgr.py
+++ b/scripts/scriptmgr.py
@@ -78,6 +78,7 @@ class ScriptMgr(skytools.DBScript):
def init_optparse(self, p = None):
p = skytools.DBScript.init_optparse(self, p)
p.add_option("-a", "--all", action="store_true", help="apply command to all jobs")
+ p.add_option("-w", "--wait", action="store_true", help="wait for job(s) after signaling")
p.set_usage(command_usage.strip())
return p
@@ -170,13 +171,9 @@ class ScriptMgr(skytools.DBScript):
print(job)
def cmd_start(self, job_name):
- if job_name not in self.job_map:
- self.log.error('Unknown job: '+job_name)
- return 1
- job = self.job_map[job_name]
- if job['disabled']:
- self.log.info("Skipping %s" % job_name)
- return 0
+ job = self.get_job_by_name (job_name)
+ if isinstance (job, int):
+ return job # ret.code
self.log.info('Starting %s' % job_name)
os.chdir(job['cwd'])
pidfile = job['pidfile']
@@ -199,26 +196,42 @@ class ScriptMgr(skytools.DBScript):
return 0
def cmd_stop(self, job_name):
- if job_name not in self.job_map:
- self.log.error('Unknown job: '+job_name)
- return
- job = self.job_map[job_name]
- if job['disabled']:
- self.log.info("Skipping %s" % job_name)
- return
+ job = self.get_job_by_name (job_name)
+ if isinstance (job, int):
+ return job # ret.code
self.log.info('Stopping %s' % job_name)
self.signal_job(job, signal.SIGINT)
def cmd_reload(self, job_name):
+ job = self.get_job_by_name (job_name)
+ if isinstance (job, int):
+ return job # ret.code
+ self.log.info('Reloading %s' % job_name)
+ self.signal_job(job, signal.SIGHUP)
+
+ def get_job_by_name (self, job_name):
if job_name not in self.job_map:
- self.log.error('Unknown job: '+job_name)
- return
+ self.log.error ("Unknown job: %s" % job_name)
+ return 1
job = self.job_map[job_name]
if job['disabled']:
- self.log.info("Skipping %s" % job_name)
- return
- self.log.info('Reloading %s' % job_name)
- self.signal_job(job, signal.SIGHUP)
+ self.log.info ("Skipping %s" % job_name)
+ return 0
+ return job
+
+ def wait_for_stop (self, job_name):
+ job = self.get_job_by_name (job_name)
+ if isinstance (job, int):
+ return job # ret.code
+ msg = False
+ while True:
+ if skytools.signal_pidfile (job['pidfile'], 0):
+ if not msg:
+ self.log.info ("Waiting for %s to stop" % job_name)
+ msg = True
+ time.sleep (0.1)
+ else:
+ return 0
def signal_job(self, job, sig):
os.chdir(job['cwd'])
@@ -274,10 +287,18 @@ class ScriptMgr(skytools.DBScript):
elif cmd == "stop":
for n in jobs:
self.cmd_stop(n)
+ if self.options.wait:
+ for n in jobs:
+ self.wait_for_stop(n)
elif cmd == "restart":
for n in jobs:
self.cmd_stop(n)
+ if self.options.wait:
+ for n in jobs:
+ self.wait_for_stop(n)
+ else:
time.sleep(2)
+ for n in jobs:
self.cmd_start(n)
elif cmd == "reload":
for n in jobs: