1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
"""
Extremely basic subprocess control
"""
import argparse
import json
import os
import random
import signal
import subprocess
import sys
import time
import traceback
from datetime import datetime, timedelta
from pathlib import Path
from typing import TYPE_CHECKING, NoReturn, Sequence, Union, cast
if TYPE_CHECKING:
from typing import (Literal, NamedTuple, TypedDict)
INTERUPT_SIGNAL = signal.SIGINT if os.name != 'nt' else signal.CTRL_C_SIGNAL
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser('proc-ctl')
grp = parser.add_subparsers(title='Commands',
dest='command',
metavar='<subcommand>')
start = grp.add_parser('start', help='Start a new subprocess')
start.add_argument('--ctl-dir',
help='The control directory for the subprocess',
required=True,
type=Path)
start.add_argument('--cwd',
help='The new subdirectory of the spawned process',
type=Path)
start.add_argument(
'--spawn-wait',
help='Number of seconds to wait for child to be running',
type=float,
default=3)
start.add_argument('child_command',
nargs='+',
help='The command to execute',
metavar='<command> [args...]')
stop = grp.add_parser('stop', help='Stop a running subprocess')
stop.add_argument('--ctl-dir',
help='The control directory for the subprocess',
required=True,
type=Path)
stop.add_argument('--stop-wait',
help='Number of seconds to wait for stopping',
type=float,
default=5)
stop.add_argument('--if-not-running',
help='Action to take if the child is not running',
choices=['fail', 'ignore'],
default='fail')
ll_run = grp.add_parser('__run')
ll_run.add_argument('--ctl-dir', type=Path, required=True)
ll_run.add_argument('child_command', nargs='+')
return parser
if TYPE_CHECKING:
StartCommandArgs = NamedTuple('StartCommandArgs', [
('command', Literal['start']),
('ctl_dir', Path),
('cwd', Path),
('child_command', Sequence[str]),
('spawn_wait', float),
])
StopCommandArgs = NamedTuple('StopCommandArgs', [
('command', Literal['stop']),
('ctl_dir', Path),
('stop_wait', float),
('if_not_running', Literal['fail', 'ignore']),
])
_RunCommandArgs = NamedTuple('_RunCommandArgs', [
('command', Literal['__run']),
('child_command', Sequence[str]),
('ctl_dir', Path),
])
CommandArgs = Union[StartCommandArgs, StopCommandArgs, _RunCommandArgs]
_ResultType = TypedDict('_ResultType', {
'exit': 'str | int | None',
'error': 'str | None'
})
def parse_argv(argv: 'Sequence[str]') -> 'CommandArgs':
parser = create_parser()
args = parser.parse_args(argv)
return cast('CommandArgs', args)
class _ChildControl:
def __init__(self, ctl_dir: Path) -> None:
self._ctl_dir = ctl_dir
@property
def pid_file(self):
"""The file containing the child PID"""
return self._ctl_dir / 'pid.txt'
@property
def result_file(self):
"""The file containing the exit result"""
return self._ctl_dir / 'exit.json'
def set_pid(self, pid: int):
write_text(self.pid_file, str(pid))
def get_pid(self) -> 'int | None':
try:
txt = self.pid_file.read_text()
except FileNotFoundError:
return None
return int(txt)
def set_exit(self, exit: 'str | int | None', error: 'str | None') -> None:
write_text(self.result_file, json.dumps({
'exit': exit,
'error': error
}))
remove_file(self.pid_file)
def get_result(self) -> 'None | _ResultType':
try:
txt = self.result_file.read_text()
except FileNotFoundError:
return None
return json.loads(txt)
def clear_result(self) -> None:
remove_file(self.result_file)
def _start(args: 'StartCommandArgs') -> int:
ll_run_cmd = [
sys.executable,
'-u',
'--',
__file__,
'__run',
'--ctl-dir={}'.format(args.ctl_dir),
'--',
*args.child_command,
]
args.ctl_dir.mkdir(exist_ok=True, parents=True)
child = _ChildControl(args.ctl_dir)
if child.get_pid() is not None:
raise RuntimeError('Child process is already running [PID {}]'.format(
child.get_pid()))
child.clear_result()
# Spawn the child controller
subprocess.Popen(
ll_run_cmd,
cwd=args.cwd,
stderr=subprocess.STDOUT,
stdout=args.ctl_dir.joinpath('runner-output.txt').open('wb'),
stdin=subprocess.DEVNULL)
expire = datetime.now() + timedelta(seconds=args.spawn_wait)
# Wait for the PID to appear
while child.get_pid() is None and child.get_result() is None:
if expire < datetime.now():
break
time.sleep(0.1)
# Check that it actually spawned
if child.get_pid() is None:
result = child.get_result()
if result is None:
raise RuntimeError('Failed to spawn child runner?')
if result['error']:
print(result['error'], file=sys.stderr)
raise RuntimeError('Child exited immediately [Exited {}]'.format(
result['exit']))
# Wait to see that it is still running after --spawn-wait seconds
while child.get_result() is None:
if expire < datetime.now():
break
time.sleep(0.1)
# A final check to see if it is running
result = child.get_result()
if result is not None:
if result['error']:
print(result['error'], file=sys.stderr)
raise RuntimeError('Child exited prematurely [Exited {}]'.format(
result['exit']))
return 0
def _stop(args: 'StopCommandArgs') -> int:
child = _ChildControl(args.ctl_dir)
pid = child.get_pid()
if pid is None:
if args.if_not_running == 'fail':
raise RuntimeError('Child process is not running')
elif args.if_not_running == 'ignore':
# Nothing to do
return 0
else:
assert False
os.kill(pid, INTERUPT_SIGNAL)
expire_at = datetime.now() + timedelta(seconds=args.stop_wait)
while expire_at > datetime.now() and child.get_result() is None:
time.sleep(0.1)
result = child.get_result()
if result is None:
raise RuntimeError(
'Child process did not exit within the grace period')
return 0
def __run(args: '_RunCommandArgs') -> int:
this = _ChildControl(args.ctl_dir)
try:
pipe = subprocess.Popen(
args.child_command,
stdout=args.ctl_dir.joinpath('child-output.txt').open('wb'),
stderr=subprocess.STDOUT,
stdin=subprocess.DEVNULL)
except:
this.set_exit('spawn-failed', traceback.format_exc())
raise
this.set_pid(pipe.pid)
retc = None
try:
while 1:
try:
retc = pipe.wait(0.5)
except subprocess.TimeoutExpired:
pass
except KeyboardInterrupt:
pipe.send_signal(INTERUPT_SIGNAL)
if retc is not None:
break
finally:
this.set_exit(retc, None)
return 0
def write_text(fpath: Path, content: str):
"""
"Atomically" write a new file.
This writes the given ``content`` into a temporary file, then renames that
file into place. This prevents readers from seeing a partial read.
"""
tmp = fpath.with_name(fpath.name + '.tmp')
remove_file(tmp)
tmp.write_text(content)
os.sync()
remove_file(fpath)
tmp.rename(fpath)
def remove_file(fpath: Path):
"""
Safely remove a file.
Because Win32, deletes are asynchronous, so we rename to a random filename,
then delete that file. This ensures the file is "out of the way", even if
it takes some time to delete.
"""
delname = fpath.with_name(fpath.name + '.delete-' +
str(random.randint(0, 999999)))
try:
fpath.rename(delname)
except FileNotFoundError:
return
delname.unlink()
def main(argv: 'Sequence[str]') -> int:
args = parse_argv(argv)
if args.command == 'start':
return _start(args)
if args.command == '__run':
return __run(args)
if args.command == 'stop':
return _stop(args)
return 0
def start_main() -> NoReturn:
sys.exit(main(sys.argv[1:]))
if __name__ == '__main__':
start_main()
|