This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanager.py
More file actions
135 lines (120 loc) · 4.35 KB
/
Copy pathmanager.py
File metadata and controls
135 lines (120 loc) · 4.35 KB
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
#!/usr/bin/env python3
import sys
import time
import os.path
import signal
import subprocess
from task_maker.config import Config
from task_maker.task_maker_frontend import Frontend
from typing import List
SERVER_SPAWN_TIME = 1
MAX_SPAWN_ATTEMPT = 3
def get_task_maker_path():
"""
Get the path of the cpp executable
"""
task_maker = os.path.dirname(__file__)
task_maker = os.path.join(task_maker, "bin", "task-maker")
return os.path.abspath(task_maker)
def spawn_backend(type: str, args: List[str], daemonize: bool):
"""
Spawn a backend service, eventually daemonizing it
"""
task_maker = get_task_maker_path()
if daemonize:
args.append("--daemon")
streams = subprocess.DEVNULL
else:
streams = None
subprocess.run(
[task_maker, type] + args,
stdin=streams,
stdout=streams,
stderr=streams)
def spawn_server(config: Config):
"""
Spawn the server, passing its arguments from the config
"""
args = []
if config.server_logfile is not None:
args += ["--logfile", config.server_logfile]
if config.server_pidfile is not None:
args += ["--pidfile", config.server_pidfile]
if config.storedir is not None:
args += ["--store-dir", config.storedir]
if config.tempdir is not None:
args += ["--temp-dir", config.tempdir]
if config.cache_size is not None:
args += ["--cache-size", str(config.cache_size)]
if config.server_address is not None:
args += ["--address", config.server_address]
if config.server_port is not None:
args += ["--port", str(config.server_port)]
if config.server_verbose:
args += ["--verbose"]
spawn_backend("server", args, not config.run_server)
def spawn_worker(config: Config):
"""
Spawn the worker, passing its arguments from the config
"""
args = []
if config.worker_logfile is not None:
args += ["--logfile", config.worker_logfile]
if config.worker_pidfile is not None:
args += ["--pidfile", config.worker_pidfile]
if config.storedir is not None:
args += ["--store-dir", config.storedir]
if config.tempdir is not None:
args += ["--temp-dir", config.tempdir]
if config.cache_size is not None:
args += ["--cache-size", str(config.cache_size)]
if config.worker_keep_sandboxes:
args += ["--keep_sandboxes"]
if config.worker_name is not None:
args += ["--name", config.worker_name]
if config.worker_num_cores is not None:
args += ["--num-cores", str(config.worker_num_cores)]
if config.worker_port is not None:
args += ["--port", str(config.worker_port)]
if config.worker_address is not None:
args += ["--server", config.worker_address]
if config.worker_pending_requests is not None:
args += ["--pending-requests", str(config.worker_pending_requests)]
if config.worker_verbose:
args += ["--verbose"]
spawn_backend("worker", args, not config.run_worker)
def get_frontend(config: Config) -> Frontend:
"""
Run the frontend module connecting to the server and eventually spawning it
if needed.
"""
try:
return Frontend(config.host, config.port)
except:
if config.no_spawn:
raise RuntimeError(
"Cannot connect to the server and spawning is forbidden")
spawn_server(config)
print(
"Spawning server and workers", end="", flush=True, file=sys.stderr)
for _ in range(3):
print(".", end="", flush=True, file=sys.stderr)
time.sleep(SERVER_SPAWN_TIME / 3)
print(file=sys.stderr)
spawn_worker(config)
for t in range(MAX_SPAWN_ATTEMPT):
try:
return Frontend(config.host, config.port)
except:
print("Attempt {} failed".format(t + 1), file=sys.stderr)
time.sleep(1)
raise RuntimeError("Failed to spawn the server")
def stop():
proc = subprocess.run(["ps", "ax", "-o", "pid,cmd"],
stdout=subprocess.PIPE)
path = get_task_maker_path()
running = [p.split()[:2] for p in proc.stdout.decode().splitlines()]
pids = [int(pid) for pid, proc in running if proc == path]
for pid in pids:
print("Sending SIGTERM to pid %d" % pid, file=sys.stderr)
os.kill(pid, signal.SIGTERM)