-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathrunner.py
64 lines (50 loc) · 2.26 KB
/
runner.py
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
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Function that starts a daemon runner."""
import logging
import signal
import asyncio
from aiida.common.log import configure_logging
from aiida.engine.daemon.client import get_daemon_client
from aiida.engine.runners import Runner
from aiida.manage.manager import get_manager
LOGGER = logging.getLogger(__name__)
async def shutdown_runner(runner: Runner) -> None:
"""Cleanup tasks tied to the service's shutdown."""
from asyncio import all_tasks
from asyncio import current_task
LOGGER.info('Received signal to shut down the daemon runner')
tasks = [task for task in all_tasks() if task is not current_task()]
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
runner.close()
LOGGER.info('Daemon runner stopped')
def start_daemon() -> None:
"""Start a daemon runner for the currently configured profile."""
daemon_client = get_daemon_client()
configure_logging(daemon=True, daemon_log_file=daemon_client.daemon_log_file)
try:
manager = get_manager()
runner = manager.create_daemon_runner()
manager.set_runner(runner)
except Exception:
LOGGER.exception('daemon runner failed to start')
raise
signals = (signal.SIGTERM, signal.SIGINT)
for s in signals: # pylint: disable=invalid-name
runner.loop.add_signal_handler(s, lambda s=s: asyncio.create_task(shutdown_runner(runner)))
try:
LOGGER.info('Starting a daemon runner')
runner.start()
except SystemError as exception:
LOGGER.info('Received a SystemError: %s', exception)
runner.close()
LOGGER.info('Daemon runner started')