Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import errno | 7 import errno |
| 8 import os | 8 import os |
| 9 import signal | 9 import signal |
| 10 import subprocess | 10 import subprocess |
| 11 import sys | 11 import sys |
| 12 import time | |
| 12 | 13 |
| 13 from slave import goma_utils | 14 from slave import goma_utils |
| 14 | 15 |
| 15 | 16 |
| 16 def start_cloudtail(args): | 17 def start_cloudtail(args): |
| 17 """Write process id of started cloudtail to file object f""" | 18 """Write process id of started cloudtail to file object f""" |
| 18 | 19 |
| 19 proc = subprocess.Popen([args.cloudtail_path, | 20 proc = subprocess.Popen([args.cloudtail_path, |
| 20 'tail', | 21 'tail', |
| 21 '--log-id', 'goma_compiler_proxy', | 22 '--log-id', 'goma_compiler_proxy', |
| 22 '--path', | 23 '--path', |
| 23 goma_utils.GetLatestGomaCompilerProxyInfo()]) | 24 goma_utils.GetLatestGomaCompilerProxyInfo()]) |
| 24 with open(args.pid_file, 'w') as f: | 25 with open(args.pid_file, 'w') as f: |
| 25 f.write(str(proc.pid)) | 26 f.write(str(proc.pid)) |
| 26 | 27 |
| 28 def is_running_posix(pid): | |
|
shinyak
2016/10/11 06:47:02
Two blank lines between top-level definitions
htt
tikuta
2016/10/11 08:09:00
Done.
| |
| 29 """Return True if process of pid is running. | |
| 30 | |
| 31 Args: | |
| 32 pid(int): pid of process which this function checks | |
| 33 whether it is running or not. | |
| 34 | |
| 35 Returns: | |
| 36 bool: True if process of pid is running. | |
| 37 | |
| 38 Raises: | |
| 39 OSError if something happens in os.kill(pid, 0) | |
| 40 """ | |
| 41 | |
| 42 try: | |
| 43 os.kill(pid, 0) | |
| 44 except OSError as e: | |
| 45 if e.errno == errno.ESRCH or e.errno == errno.EPERM: | |
| 46 return False | |
| 47 os.kill(pid, signal.SIGKILL) | |
|
shinyak
2016/10/11 06:47:02
Oh, is_running_posix(pid) sends SIGKILL? I feel it
tikuta
2016/10/11 08:09:00
Done.
| |
| 48 print('killed process %d due to OSError %s' % (pid, e)) | |
| 49 raise e | |
| 50 return True | |
| 51 | |
| 52 def wait_termination(pid): | |
| 53 """Send SIGINT to pid and wait termination of pid. | |
| 54 | |
| 55 Args: | |
| 56 pid(int): pid of process which this function waits termination. | |
| 57 | |
| 58 Raises: | |
| 59 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. | |
| 60 """ | |
| 61 | |
| 62 try: | |
| 63 os.kill(pid, signal.SIGINT) | |
| 64 except: | |
| 65 os.kill(pid, signal.SIGKILL) | |
| 66 raise | |
| 67 | |
| 68 if os.name == 'nt': | |
| 69 try: | |
| 70 os.waitpid(pid, 0) | |
| 71 except OSError as e: | |
| 72 if e.errno == errno.ECHILD: | |
| 73 print('ignore errno.ECHILD %s' % e) | |
| 74 return | |
| 75 raise e | |
| 76 else: | |
| 77 for _ in xrange(10): | |
| 78 if not is_running_posix(pid): | |
| 79 break | |
| 80 time.sleep(1) | |
| 81 | |
| 82 if is_running_posix(pid): | |
| 83 os.kill(pid, signal.SIGKILL) | |
| 84 print('killed process %d running more than 10 seconds' % pid) | |
| 85 | |
| 27 def main(): | 86 def main(): |
|
shinyak
2016/10/11 06:47:02
Two blank lines between top-level definitions
tikuta
2016/10/11 08:09:00
Done.
| |
| 28 parser = argparse.ArgumentParser( | 87 parser = argparse.ArgumentParser( |
| 29 description='cloudtail utility for goma recipe module.') | 88 description='cloudtail utility for goma recipe module.') |
| 30 | 89 |
| 31 subparsers = parser.add_subparsers(help='commands for cloudtail') | 90 subparsers = parser.add_subparsers(help='commands for cloudtail') |
| 32 | 91 |
| 33 parser_start = subparsers.add_parser('start', | 92 parser_start = subparsers.add_parser('start', |
| 34 help='subcommand to start cloudtail') | 93 help='subcommand to start cloudtail') |
| 35 parser_start.set_defaults(command='start') | 94 parser_start.set_defaults(command='start') |
| 36 parser_start.add_argument('--cloudtail-path', required=True, | 95 parser_start.add_argument('--cloudtail-path', required=True, |
| 37 help='path of cloudtail binary') | 96 help='path of cloudtail binary') |
| 38 parser_start.add_argument('--pid-file', required=True, | 97 parser_start.add_argument('--pid-file', required=True, |
| 39 help='file written pid') | 98 help='file written pid') |
| 40 | 99 |
| 41 parser_stop = subparsers.add_parser('stop', | 100 parser_stop = subparsers.add_parser('stop', |
| 42 help='subcommand to stop cloudtail') | 101 help='subcommand to stop cloudtail') |
| 43 parser_stop.set_defaults(command='stop') | 102 parser_stop.set_defaults(command='stop') |
| 44 parser_stop.add_argument('--killed-pid-file', required=True, | 103 parser_stop.add_argument('--killed-pid-file', required=True, |
| 45 help='file written the pid to be killed.') | 104 help='file written the pid to be killed.') |
| 46 | 105 |
| 47 args = parser.parse_args() | 106 args = parser.parse_args() |
| 48 | 107 |
| 49 if args.command == 'start': | 108 if args.command == 'start': |
| 50 start_cloudtail(args) | 109 start_cloudtail(args) |
| 51 elif args.command == 'stop': | 110 elif args.command == 'stop': |
| 52 with open(args.killed_pid_file) as f: | 111 with open(args.killed_pid_file) as f: |
| 53 # cloudtail flushes log and terminates | 112 # cloudtail flushes log and terminates |
| 54 # within 5 seconds when it recieves SIGINT. | 113 # within 5 seconds when it recieves SIGINT. |
|
shinyak
2016/10/11 06:47:02
5 seconds. Right?
tikuta
2016/10/11 08:09:00
From
https://codereview.chromium.org/2375843005/#m
| |
| 55 os.kill(int(f.read()), signal.SIGINT) | 114 wait_termination(int(f.read())) |
| 56 | 115 |
| 57 if '__main__' == __name__: | 116 if '__main__' == __name__: |
| 58 sys.exit(main()) | 117 sys.exit(main()) |
| OLD | NEW |