Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1042)

Side by Side Diff: scripts/slave/recipe_modules/goma/resources/cloudtail_utils.py

Issue 2404213002: Reland Wait cloudtail termination in goma module (Closed)
Patch Set: add NotDiedError Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
29 def is_running_posix(pid):
30 """Return True if process of pid is running.
31
32 Args:
33 pid(int): pid of process which this function checks
34 whether it is running or not.
35
36 Returns:
37 bool: True if process of pid is running.
38
39 Raises:
40 OSError if something happens in os.kill(pid, 0)
41 """
42
43 try:
44 os.kill(pid, 0)
45 except OSError as e:
46 if e.errno == errno.ESRCH or e.errno == errno.EPERM:
47 return False
48 raise e
49 return True
50
51
52 class NotDiedError(Exception):
53 def __str__(self):
54 return "NotDiedError"
55
56
57 def wait_termination(pid):
58 """Send SIGINT to pid and wait termination of pid.
59
60 Args:
61 pid(int): pid of process which this function waits termination.
62
63 Raises:
64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError.
65 NotDiedError: if cloudtail is running after 10 seconds waiting,
66 NotDiedError is raised.
67 """
68
69 os.kill(pid, signal.SIGINT)
70
71 if os.name == 'nt':
72 try:
73 os.waitpid(pid, 0)
74 except OSError as e:
75 if e.errno == errno.ECHILD:
76 print('ignore errno.ECHILD %s, '
ukai 2016/10/12 07:05:21 need to print this?
tikuta 2016/10/12 07:12:32 Done.
77 'process of pid %d died before waitpitd' %
78 (e, pid))
79 return
80 raise e
81 else:
82 for _ in xrange(10):
83 if not is_running_posix(pid):
84 break
85 time.sleep(1)
86
87 if is_running_posix(pid):
88 print('process %d running more than 10 seconds' % pid)
89 raise NotDiedError()
90
91
27 def main(): 92 def main():
28 parser = argparse.ArgumentParser( 93 parser = argparse.ArgumentParser(
29 description='cloudtail utility for goma recipe module.') 94 description='cloudtail utility for goma recipe module.')
30 95
31 subparsers = parser.add_subparsers(help='commands for cloudtail') 96 subparsers = parser.add_subparsers(help='commands for cloudtail')
32 97
33 parser_start = subparsers.add_parser('start', 98 parser_start = subparsers.add_parser('start',
34 help='subcommand to start cloudtail') 99 help='subcommand to start cloudtail')
35 parser_start.set_defaults(command='start') 100 parser_start.set_defaults(command='start')
36 parser_start.add_argument('--cloudtail-path', required=True, 101 parser_start.add_argument('--cloudtail-path', required=True,
37 help='path of cloudtail binary') 102 help='path of cloudtail binary')
38 parser_start.add_argument('--pid-file', required=True, 103 parser_start.add_argument('--pid-file', required=True,
39 help='file written pid') 104 help='file written pid')
40 105
41 parser_stop = subparsers.add_parser('stop', 106 parser_stop = subparsers.add_parser('stop',
42 help='subcommand to stop cloudtail') 107 help='subcommand to stop cloudtail')
43 parser_stop.set_defaults(command='stop') 108 parser_stop.set_defaults(command='stop')
44 parser_stop.add_argument('--killed-pid-file', required=True, 109 parser_stop.add_argument('--killed-pid-file', required=True,
45 help='file written the pid to be killed.') 110 help='file written the pid to be killed.')
46 111
47 args = parser.parse_args() 112 args = parser.parse_args()
48 113
49 if args.command == 'start': 114 if args.command == 'start':
50 start_cloudtail(args) 115 start_cloudtail(args)
51 elif args.command == 'stop': 116 elif args.command == 'stop':
52 with open(args.killed_pid_file) as f: 117 with open(args.killed_pid_file) as f:
53 # cloudtail flushes log and terminates 118 # cloudtail flushes log and terminates
54 # within 5 seconds when it recieves SIGINT. 119 # within 5 seconds when it recieves SIGINT.
55 os.kill(int(f.read()), signal.SIGINT) 120 pid = int(f.read())
121 try:
122 wait_termination(int(f.read()))
123 except (OSError, NotDiedError) as e:
124 os.kill(pid, signal.SIGKILL)
125 print('killed process %d due to Error %s' % (pid, e))
126 raise e
127
56 128
57 if '__main__' == __name__: 129 if '__main__' == __name__:
58 sys.exit(main()) 130 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698