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 | 12 |
13 from slave import goma_utils | 13 from slave import goma_utils |
14 | 14 |
15 | 15 |
16 def start_cloudtail(args): | 16 def start_cloudtail(args): |
17 """Write process id of started cloudtail to file object f""" | 17 """Write process id of started cloudtail to file object f""" |
18 | 18 |
19 proc = subprocess.Popen([args.cloudtail_path, | 19 proc = subprocess.Popen([args.cloudtail_path, |
20 'tail', | 20 'tail', |
21 '--log-id', 'goma_compiler_proxy', | 21 '--log-id', 'goma_compiler_proxy', |
22 '--path', | 22 '--path', |
23 goma_utils.GetLatestGomaCompilerProxyInfo()], | 23 goma_utils.GetLatestGomaCompilerProxyInfo()]) |
24 stdout=open(os.devnull, 'w'), | 24 with open(args.pid_file, 'w') as f: |
25 stderr=open(os.devnull, 'w'), | 25 f.write(str(proc.pid)) |
26 close_fds=True) | |
27 | |
28 sys.stdout.write(str(proc.pid)) | |
29 | |
30 | 26 |
31 def main(): | 27 def main(): |
32 parser = argparse.ArgumentParser( | 28 parser = argparse.ArgumentParser( |
33 description='cloudtail utility for goma recipe module.') | 29 description='cloudtail utility for goma recipe module.') |
34 | 30 |
35 subparsers = parser.add_subparsers(help='commands for cloudtail') | 31 subparsers = parser.add_subparsers(help='commands for cloudtail') |
36 | 32 |
37 parser_start = subparsers.add_parser('start', | 33 parser_start = subparsers.add_parser('start', |
38 help='subcommand to start cloudtail') | 34 help='subcommand to start cloudtail') |
39 parser_start.set_defaults(command='start') | 35 parser_start.set_defaults(command='start') |
40 parser_start.add_argument('--cloudtail-path', required=True, | 36 parser_start.add_argument('--cloudtail-path', required=True, |
41 help='path of cloudtail binary') | 37 help='path of cloudtail binary') |
| 38 parser_start.add_argument('--pid-file', required=True, |
| 39 help='file written pid') |
42 | 40 |
43 parser_stop = subparsers.add_parser('stop', | 41 parser_stop = subparsers.add_parser('stop', |
44 help='subcommand to stop cloudtail') | 42 help='subcommand to stop cloudtail') |
45 parser_stop.set_defaults(command='stop') | 43 parser_stop.set_defaults(command='stop') |
46 parser_stop.add_argument('--killed-pid', type=int, required=True, | 44 parser_stop.add_argument('--killed-pid-file', required=True, |
47 help='pid that is killed.') | 45 help='file written the pid to be killed.') |
48 | 46 |
49 args = parser.parse_args() | 47 args = parser.parse_args() |
50 | 48 |
51 if args.command == 'start': | 49 if args.command == 'start': |
52 start_cloudtail(args) | 50 start_cloudtail(args) |
53 elif args.command == 'stop': | 51 elif args.command == 'stop': |
54 try: | 52 with open(args.killed_pid_file) as f: |
55 os.kill(args.killed_pid, signal.SIGKILL) | 53 os.kill(int(f.read()), signal.SIGKILL) |
56 except OSError as e: | |
57 if e.errno != errno.ESRCH: | |
58 # TODO(tikuta): Resolve https://crbug.com/639910. | |
59 raise | |
60 | |
61 | 54 |
62 if '__main__' == __name__: | 55 if '__main__' == __name__: |
63 sys.exit(main()) | 56 sys.exit(main()) |
OLD | NEW |