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): | |
29 """Return True if process of pid is running.""" | |
ukai
2016/10/05 01:08:32
Args:
Returns:
Raises:
tikuta
2016/10/05 04:16:28
Done.
| |
30 | |
31 try: | |
32 os.kill(pid, 0) | |
33 except OSError as e: | |
34 if e.errno == errno.ESRCH or e.errno == errno.EPERM: | |
35 return False | |
36 os.kill(pid, signal.SIGKILL) | |
37 raise e | |
38 return True | |
39 | |
40 def wait_termination(pid): | |
41 """Wait termination of pid.""" | |
ukai
2016/10/05 01:08:32
Send SIGINT and wait ..
?
Args:
Raises:
tikuta
2016/10/05 04:16:28
Done.
| |
42 | |
43 try: | |
44 os.kill(pid, signal.SIGINT) | |
45 except: | |
46 os.kill(pid, signal.SIGKILL) | |
47 raise | |
48 | |
49 if os.name == 'nt': | |
50 os.waitpid(pid, 0) | |
51 else: | |
52 for _ in range(10): | |
Yoshisato Yanagisawa
2016/10/05 02:15:42
nit xrange?
tikuta
2016/10/05 04:16:28
Done.
| |
53 if not is_running_posix(pid): | |
54 break | |
55 time.sleep(1) | |
56 | |
57 if is_running_posix(pid): | |
58 os.kill(pid, signal.SIGKILL) | |
Yoshisato Yanagisawa
2016/10/05 02:15:42
not sure but no need to print you killed the proce
tikuta
2016/10/05 04:16:28
Done.
It is needed.
| |
59 | |
27 def main(): | 60 def main(): |
28 parser = argparse.ArgumentParser( | 61 parser = argparse.ArgumentParser( |
29 description='cloudtail utility for goma recipe module.') | 62 description='cloudtail utility for goma recipe module.') |
30 | 63 |
31 subparsers = parser.add_subparsers(help='commands for cloudtail') | 64 subparsers = parser.add_subparsers(help='commands for cloudtail') |
32 | 65 |
33 parser_start = subparsers.add_parser('start', | 66 parser_start = subparsers.add_parser('start', |
34 help='subcommand to start cloudtail') | 67 help='subcommand to start cloudtail') |
35 parser_start.set_defaults(command='start') | 68 parser_start.set_defaults(command='start') |
36 parser_start.add_argument('--cloudtail-path', required=True, | 69 parser_start.add_argument('--cloudtail-path', required=True, |
37 help='path of cloudtail binary') | 70 help='path of cloudtail binary') |
38 parser_start.add_argument('--pid-file', required=True, | 71 parser_start.add_argument('--pid-file', required=True, |
39 help='file written pid') | 72 help='file written pid') |
40 | 73 |
41 parser_stop = subparsers.add_parser('stop', | 74 parser_stop = subparsers.add_parser('stop', |
42 help='subcommand to stop cloudtail') | 75 help='subcommand to stop cloudtail') |
43 parser_stop.set_defaults(command='stop') | 76 parser_stop.set_defaults(command='stop') |
44 parser_stop.add_argument('--killed-pid-file', required=True, | 77 parser_stop.add_argument('--killed-pid-file', required=True, |
45 help='file written the pid to be killed.') | 78 help='file written the pid to be killed.') |
46 | 79 |
47 args = parser.parse_args() | 80 args = parser.parse_args() |
48 | 81 |
49 if args.command == 'start': | 82 if args.command == 'start': |
50 start_cloudtail(args) | 83 start_cloudtail(args) |
51 elif args.command == 'stop': | 84 elif args.command == 'stop': |
52 with open(args.killed_pid_file) as f: | 85 with open(args.killed_pid_file) as f: |
53 # cloudtail flushes log and terminates | 86 # cloudtail flushes log and terminates |
54 # within 5 seconds when it recieves SIGINT. | 87 # within 5 seconds when it recieves SIGINT. |
55 os.kill(int(f.read()), signal.SIGINT) | 88 wait_termination(int(f.read())) |
56 | 89 |
57 if '__main__' == __name__: | 90 if '__main__' == __name__: |
58 sys.exit(main()) | 91 sys.exit(main()) |
OLD | NEW |