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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369 | class Worker(Service):
"""Start mode service from the command-line."""
BLOCK_DETECTOR: ClassVar[str] = BLOCK_DETECTOR
stdout: IO
stderr: IO
debug: bool
quiet: bool
blocking_timeout: Seconds
logging_config: Optional[Dict]
loglevel: Optional[Union[str, int]]
logfile: Optional[Union[str, IO]]
console_port: int
loghandlers: List[Handler]
redirect_stdouts: bool
redirect_stdouts_level: int
services: Iterable[ServiceT]
_blocking_detector: Optional[BlockingDetector] = None
_starting_fut: Optional[asyncio.Future] = None
# signals can be called multiple times,
# so when stopped by signal we record the time to make sure
# we don't start the process multiple times.
_signal_stop_time: Optional[float] = None
_signal_stop_future: Optional[asyncio.Future] = None
def __init__(
self,
*services: ServiceT,
debug: bool = False,
quiet: bool = False,
logging_config: Optional[Dict] = None,
loglevel: Optional[Union[str, int]] = None,
logfile: Optional[Union[str, IO]] = None,
redirect_stdouts: bool = True,
redirect_stdouts_level: logging.Severity = None,
stdout: Optional[IO] = sys.stdout,
stderr: Optional[IO] = sys.stderr,
console_port: int = 50101,
loghandlers: Optional[List[Handler]] = None,
blocking_timeout: Seconds = 10.0,
loop: Optional[asyncio.AbstractEventLoop] = None,
override_logging: bool = True,
daemon: bool = True,
**kwargs: Any,
) -> None:
self.services = services
self.debug = debug
self.quiet = quiet
self.logging_config = logging_config
self.loglevel = loglevel
self.logfile = logfile
self.loghandlers = loghandlers or []
self.redirect_stdouts = redirect_stdouts
self.redirect_stdouts_level = logging.level_number(
redirect_stdouts_level or "WARN"
)
self.override_logging = override_logging
self.stdout = sys.stdout if stdout is None else stdout
self.stderr = sys.stderr if stderr is None else stderr
self.console_port = console_port
self.blocking_timeout = blocking_timeout
self.daemon = daemon
super().__init__(loop=loop, **kwargs)
if self.services:
for service in self.services:
service.beacon.reattach(self.beacon)
assert service.beacon.root is self.beacon
def say(self, msg: str) -> None:
"""Write message to standard out."""
self._say(msg)
def carp(self, msg: str) -> None:
"""Write warning to standard err."""
self._say(msg, file=self.stderr)
def _say(
self,
msg: str,
file: Optional[IO] = None,
end: str = "\n",
**kwargs: Any,
) -> None:
if file is None:
file = self.stdout
if not self.quiet:
print(msg, file=file, end=end, **kwargs)
def on_init_dependencies(self) -> Iterable[ServiceT]:
return self.services
async def on_first_start(self) -> None:
await self.default_on_first_start()
async def default_on_first_start(self) -> None:
if self.override_logging:
self._setup_logging()
await self.on_execute()
if self.debug:
await self._add_monitor()
self.install_signal_handlers()
async def on_execute(self) -> None: ...
def _setup_logging(self) -> None:
_loglevel: int = 0
try:
_loglevel = logging.setup_logging(
loglevel=self.loglevel,
logfile=self.logfile,
logging_config=self.logging_config,
loghandlers=self.loghandlers,
)
except Exception as exc:
try:
self.stderr.write(f"CANNOT SETUP LOGGING: {exc!r} from ")
import traceback
traceback.print_stack(file=self.stderr)
except Exception: # noqa: S110
pass
raise
self.on_setup_root_logger(_logging.root, _loglevel)
if self.redirect_stdouts:
self._redirect_stdouts()
def _redirect_stdouts(self) -> None:
self.add_context(
logging.redirect_stdouts(severity=self.redirect_stdouts_level)
)
def on_setup_root_logger(self, logger: Logger, level: int) -> None: ...
async def maybe_start_blockdetection(self) -> None:
if self.debug:
await self.blocking_detector.maybe_start()
def install_signal_handlers(self) -> None:
if sys.platform == "win32":
self._install_signal_handlers_windows()
else:
self._install_signal_handlers_unix()
def _install_signal_handlers_windows(self) -> None:
signal.signal(signal.SIGTERM, self._on_win_sigterm)
def _install_signal_handlers_unix(self) -> None:
self.loop.add_signal_handler(signal.SIGINT, self._on_sigint)
self.loop.add_signal_handler(signal.SIGTERM, self._on_sigterm)
self.loop.add_signal_handler(signal.SIGUSR1, self._on_sigusr1)
self.loop.add_signal_handler(signal.SIGUSR2, self._on_sigusr2)
def _on_sigint(self) -> None:
self.carp("-INT- -INT- -INT- -INT- -INT- -INT-")
self._schedule_shutdown(signal.SIGINT)
def _on_sigterm(self) -> None:
self._schedule_shutdown(signal.SIGTERM)
def _on_win_sigterm(self, signum: int, frame: Any) -> None:
self._schedule_shutdown(signal.SIGTERM)
def _on_sigusr1(self) -> None:
self.add_future(self._cry())
def _on_sigusr2(self) -> None:
self._enter_debugger()
async def _cry(self) -> None:
logging.cry(file=self.stderr)
def _enter_debugger(self) -> None:
self.carp("Starting debugger...")
import pdb
pdb.set_trace()
def _schedule_shutdown(self, signal: signal.Signals) -> None:
if not self._signal_stop_time:
self._signal_stop_time = self.loop.time()
self._signal_stop_future = asyncio.ensure_future(
self._stop_on_signal(signal), loop=self.loop
)
async def _stop_on_signal(self, signal: signal.Signals) -> None:
self.log.info("Signal received: %s (%s)", signal, signal.value)
await self.stop()
maybe_cancel(self._starting_fut)
def execute_from_commandline(self) -> NoReturn:
self._starting_fut = None
with exiting(file=self.stderr):
try:
self._starting_fut = asyncio.ensure_future(
self.start(), loop=self.loop
)
self.loop.run_until_complete(self._starting_fut)
except asyncio.CancelledError:
pass
except MemoryError:
raise
except Exception as exc:
self.log.exception("Error: %r", exc)
raise
finally:
maybe_cancel(self._starting_fut)
self.on_worker_shutdown()
self.stop_and_shutdown()
# for mypy NoReturn
raise SystemExit(EX_OK) # pragma: no cover
def on_worker_shutdown(self) -> None: ...
def stop_and_shutdown(self) -> None:
if self._signal_stop_future and not self._signal_stop_future.done():
self.loop.run_until_complete(self._signal_stop_future)
elif not self._stopped.is_set():
self.loop.run_until_complete(self.stop())
self._shutdown_loop()
def _shutdown_loop(self) -> None:
# Gather futures created by us.
self.log.info("Gathering service tasks...")
with suppress(asyncio.CancelledError):
self.loop.run_until_complete(self._gather_futures())
# Gather absolutely all asyncio futures.
self.log.info("Gathering all futures...")
self._gather_all()
try:
# Wait until loop is fully stopped.
while self.loop.is_running():
self.log.info("Waiting for event loop to shutdown...")
self.loop.stop()
self.loop.run_until_complete(asyncio.sleep(1.0))
except BaseException as exc:
self.log.exception("Got exception while waiting: %r", exc)
finally:
# Then close the loop.
fut = asyncio.ensure_future(self._sentinel_task(), loop=self.loop)
self.loop.run_until_complete(fut)
self.loop.stop()
self.log.info("Closing event loop")
self.loop.close()
if self.crash_reason:
self.log.critical(
"We experienced a crash! Reraising original exception..."
)
raise self.crash_reason from self.crash_reason
async def _sentinel_task(self) -> None:
await asyncio.sleep(1.0)
def _gather_all(self) -> None:
# sleeps for at most 10 * 0.1s
for _ in range(10):
if not len(all_tasks(loop=self.loop)):
break
self.loop.run_until_complete(asyncio.sleep(0.1))
for task in all_tasks(loop=self.loop):
task.cancel()
async def on_started(self) -> None:
if self.daemon:
await self.wait_until_stopped()
async def _add_monitor(self) -> Any:
try:
import aiomonitor
except ImportError:
self.log.warning(
"Cannot start console: aiomonitor is not installed"
)
else:
monitor = aiomonitor.start_monitor(
port=self.console_port, loop=self.loop
)
self.add_context(monitor)
def _repr_info(self) -> str:
return _repr(self.services)
@property
def blocking_detector(self) -> BlockingDetector:
if self._blocking_detector is None:
BlockDetector: Callable = symbol_by_name(self.BLOCK_DETECTOR)
self._blocking_detector = BlockDetector(
self.blocking_timeout, beacon=self.beacon, loop=self.loop
)
return cast(BlockingDetector, self._blocking_detector)
|