153 if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
153 if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
154 continue |
154 continue |
155 try: |
155 try: |
156 status = max(status, self.run_arg(appid)) |
156 status = max(status, self.run_arg(appid)) |
157 except (KeyboardInterrupt, SystemExit): |
157 except (KeyboardInterrupt, SystemExit): |
158 print >> sys.stderr, '%s aborted' % self.name |
158 sys.stderr.write('%s aborted\n' % self.name) |
159 return 2 # specific error code |
159 return 2 # specific error code |
160 sys.exit(status) |
160 sys.exit(status) |
161 |
161 |
162 def run_arg(self, appid): |
162 def run_arg(self, appid): |
163 cmdmeth = getattr(self, '%s_instance' % self.name) |
163 cmdmeth = getattr(self, '%s_instance' % self.name) |
164 try: |
164 try: |
165 status = cmdmeth(appid) |
165 status = cmdmeth(appid) |
166 except (ExecutionError, ConfigurationError), ex: |
166 except (ExecutionError, ConfigurationError), ex: |
167 print >> sys.stderr, 'instance %s not %s: %s' % ( |
167 sys.stderr.write('instance %s not %s: %s\n' % ( |
168 appid, self.actionverb, ex) |
168 appid, self.actionverb, ex)) |
169 status = 4 |
169 status = 4 |
170 except Exception, ex: |
170 except Exception, ex: |
171 import traceback |
171 import traceback |
172 traceback.print_exc() |
172 traceback.print_exc() |
173 print >> sys.stderr, 'instance %s not %s: %s' % ( |
173 sys.stderr.write('instance %s not %s: %s\n' % ( |
174 appid, self.actionverb, ex) |
174 appid, self.actionverb, ex)) |
175 status = 8 |
175 status = 8 |
176 return status |
176 return status |
177 |
177 |
178 class InstanceCommandFork(InstanceCommand): |
178 class InstanceCommandFork(InstanceCommand): |
179 """Same as `InstanceCommand`, but command is forked in a new environment |
179 """Same as `InstanceCommand`, but command is forked in a new environment |
546 config = cwcfg.config_for(appid) |
546 config = cwcfg.config_for(appid) |
547 helper = self.config_helper(config, cmdname='stop') |
547 helper = self.config_helper(config, cmdname='stop') |
548 helper.poststop() # do this anyway |
548 helper.poststop() # do this anyway |
549 pidf = config['pid-file'] |
549 pidf = config['pid-file'] |
550 if not exists(pidf): |
550 if not exists(pidf): |
551 print >> sys.stderr, "%s doesn't exist." % pidf |
551 sys.stderr.write("%s doesn't exist.\n" % pidf) |
552 return |
552 return |
553 import signal |
553 import signal |
554 pid = int(open(pidf).read().strip()) |
554 pid = int(open(pidf).read().strip()) |
555 try: |
555 try: |
556 kill(pid, signal.SIGTERM) |
556 kill(pid, signal.SIGTERM) |
557 except Exception: |
557 except Exception: |
558 print >> sys.stderr, "process %s seems already dead." % pid |
558 sys.stderr.write("process %s seems already dead.\n" % pid) |
559 else: |
559 else: |
560 try: |
560 try: |
561 wait_process_end(pid) |
561 wait_process_end(pid) |
562 except ExecutionError, ex: |
562 except ExecutionError, ex: |
563 print >> sys.stderr, ex |
563 sys.stderr.write('%s\ntrying SIGKILL\n' % ex) |
564 print >> sys.stderr, 'trying SIGKILL' |
|
565 try: |
564 try: |
566 kill(pid, signal.SIGKILL) |
565 kill(pid, signal.SIGKILL) |
567 except Exception: |
566 except Exception: |
568 # probably dead now |
567 # probably dead now |
569 pass |
568 pass |