forked from mercure-imaging/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.py
executable file
·177 lines (141 loc) · 5.92 KB
/
dispatcher.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
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
"""
dispatcher.py
=============
The dispatcher service of mercure that executes the DICOM transfer to the different targets.
"""
# Standard python includes
import asyncio
import logging
import os
import signal
import sys
from pathlib import Path
import daiquiri
import graphyte
import hupper
# App-specific includes
import common.config as config
import common.helper as helper
import common.monitor as monitor
from common.constants import mercure_names
from dispatch.status import is_ready_for_sending
from dispatch.send import execute
from common.constants import mercure_defs
# Create local logger instance
logger = config.get_logger()
main_loop = None # type: helper.AsyncTimer # type: ignore
dispatcher_lockfile = None
dispatcher_is_locked = False
async def terminate_process(signalNumber, frame) -> None:
"""Triggers the shutdown of the service."""
helper.g_log("events.shutdown", 1)
logger.info("Shutdown requested")
monitor.send_event(monitor.m_events.SHUTDOWN_REQUEST, monitor.severity.INFO)
# Note: main_loop can be read here because it has been declared as global variable
if "main_loop" in globals() and main_loop.is_running:
main_loop.stop()
helper.trigger_terminate()
def dispatch() -> None:
global dispatcher_lockfile
global dispatcher_is_locked
"""Main entry function."""
if helper.is_terminated():
return
helper.g_log("events.run", 1)
try:
config.read_config()
except Exception:
logger.exception("Unable to read configuration. Skipping processing.")
monitor.send_event(
monitor.m_events.CONFIG_UPDATE,
monitor.severity.WARNING,
"Unable to read configuration (possibly locked)",
)
return
success_folder = Path(config.mercure.success_folder)
error_folder = Path(config.mercure.error_folder)
retry_max = config.mercure.retry_max
retry_delay = config.mercure.retry_delay
try:
# Obtain a sorted folder list, so that the oldest DICOMs get dispatched first
items = sorted(Path(config.mercure.outgoing_folder).iterdir(), key=os.path.getmtime)
for entry in items:
# First, check if dispatching might have been suspended via the UI
if dispatcher_lockfile and dispatcher_lockfile.exists():
if not dispatcher_is_locked:
dispatcher_is_locked = True
logger.info(f"Dispatching halted")
break
else:
if dispatcher_is_locked:
dispatcher_is_locked = False
logger.info("Dispatching resumed")
# Now process the folders that are ready for dispatching
if entry.is_dir() and is_ready_for_sending(entry):
execute(Path(entry), success_folder, error_folder, retry_max, retry_delay)
# If termination is requested, stop processing series after the
# active one has been completed
if helper.is_terminated():
break
except:
return
def exit_dispatcher(args) -> None:
"""Stop the asyncio event loop."""
helper.loop.call_soon_threadsafe(helper.loop.stop)
def main(args=sys.argv[1:]) -> None:
global dispatcher_lockfile
if "--reload" in args or os.getenv("MERCURE_ENV", "PROD").lower() == "dev":
# start_reloader will only return in a monitored subprocess
reloader = hupper.start_reloader("dispatcher.main")
logger.info("")
logger.info(f"mercure DICOM Dispatcher ver {mercure_defs.VERSION}")
logger.info("--------------------------------------------")
logger.info("")
# Register system signals to be caught
signals = (signal.SIGTERM, signal.SIGINT)
for s in signals:
helper.loop.add_signal_handler(s, lambda s=s: asyncio.create_task(terminate_process(s, helper.loop)))
instance_name = "main"
if len(sys.argv) > 1:
instance_name = sys.argv[1]
try:
config.read_config()
except Exception:
logger.exception("Cannot start service. Going down.")
sys.exit(1)
appliance_name = config.mercure.appliance_name
logger.info(f"Appliance name = {appliance_name}")
logger.info(f"Instance name = {instance_name}")
logger.info(f"Instance PID = {os.getpid()}")
logger.info(sys.version)
monitor.configure("dispatcher", instance_name, config.mercure.bookkeeper)
monitor.send_event(monitor.m_events.BOOT, monitor.severity.INFO, f"PID = {os.getpid()}")
if len(config.mercure.graphite_ip) > 0:
logging.info(f"Sending events to graphite server: {config.mercure.graphite_ip}")
graphite_prefix = "mercure." + appliance_name + ".dispatcher." + instance_name
graphyte.init(
config.mercure.graphite_ip,
config.mercure.graphite_port,
prefix=graphite_prefix,
)
logger.info(f"Dispatching folder: {config.mercure.outgoing_folder}")
dispatcher_lockfile = Path(config.mercure.outgoing_folder + "/" + mercure_names.HALT)
global main_loop
main_loop = helper.AsyncTimer(config.mercure.dispatcher_scan_interval, dispatch)
helper.g_log("events.boot", 1)
try:
# Start the asyncio event loop for asynchronous function calls
main_loop.run_until_complete(helper.loop)
# Process will exit here once the asyncio loop has been stopped
monitor.send_event(monitor.m_events.SHUTDOWN, monitor.severity.INFO)
except Exception as e:
# Process will exit here once the asyncio loop has been stopped
monitor.send_event(monitor.m_events.SHUTDOWN, monitor.severity.ERROR, str(e))
finally:
# Finish all asyncio tasks that might be still pending
remaining_tasks = helper.asyncio.all_tasks(helper.loop) # type: ignore[attr-defined]
if remaining_tasks:
helper.loop.run_until_complete(helper.asyncio.gather(*remaining_tasks))
logging.info("Going down now")
if __name__ == "__main__":
main()