-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_handlers.py
198 lines (149 loc) · 5.17 KB
/
server_handlers.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from ouroboros.common.file_system import (
load_options_for_backproject,
load_options_for_backproject_docker,
load_options_for_slice,
load_options_for_slice_docker,
save_output_for_backproject_docker,
save_output_for_slice_docker,
)
from ouroboros.common.logging import logger
from ouroboros.common.pipelines import backproject_pipeline, slice_pipeline
from ouroboros.common.server_types import BackProjectTask, SliceTask, Task
from ouroboros.common.volume_server_interface import clear_plugin_folder
from ouroboros.helpers.files import combine_unknown_folder
def handle_slice_core(task: SliceTask, slice_options):
pipeline, input_data = slice_pipeline(slice_options)
# Store the pipeline in the task
task.pipeline = pipeline
# Store the input data in the task
task.pipeline_input = input_data
task.status = "started"
_, error = pipeline.process(input_data)
if error:
return error
# Log the pipeline statistics
logger.info("Slice Pipeline Statistics:")
logger.info(pipeline.get_step_statistics())
def handle_slice(task: SliceTask):
slice_options = load_options_for_slice(task.options)
slice_result = handle_slice_core(task, slice_options)
if isinstance(slice_result, str):
task.error = slice_result
task.status = "error"
return
def handle_slice_docker(task: SliceTask):
load_result = load_options_for_slice_docker(task.options)
if isinstance(load_result, str):
task.error = load_result
task.status = "error"
clear_plugin_folder()
return
slice_options, host_output_file, host_output_slices = load_result
slice_result = handle_slice_core(task, slice_options)
if isinstance(slice_result, str):
task.error = slice_result
task.status = "error"
clear_plugin_folder()
return
save_result = save_output_for_slice_docker(
host_output_file, host_output_slices=host_output_slices
)
if save_result:
task.error = save_result
task.status = "error"
clear_plugin_folder()
def handle_backproject_core(task: BackProjectTask, options, slice_options):
pipeline, input_data = backproject_pipeline(options, slice_options)
# Store the pipeline in the task
task.pipeline = pipeline
# Store the input data in the task
task.pipeline_input = input_data
task.status = "started"
output, error = pipeline.process(input_data)
if error:
return error
# Log the pipeline statistics
logger.info("Backproject Pipeline Statistics:")
logger.info(pipeline.get_step_statistics())
return output
def handle_backproject(task: BackProjectTask):
options = load_options_for_backproject(task.options)
slice_options = load_options_for_slice(options.slice_options_path)
backproject_result = handle_backproject_core(task, options, slice_options)
if isinstance(backproject_result, str):
task.error = backproject_result
task.status = "error"
return
def handle_backproject_docker(task: BackProjectTask):
load_result = load_options_for_backproject_docker(task.options)
if isinstance(load_result, str):
task.error = load_result
task.status = "error"
clear_plugin_folder()
return
(
options,
slice_options,
host_output_file,
host_output_slices,
host_output_folder,
) = load_result
backproject_result = handle_backproject_core(task, options, slice_options)
if isinstance(backproject_result, str):
task.error = backproject_result
task.status = "error"
clear_plugin_folder()
return
else:
if options.make_single_file:
host_output_file = combine_unknown_folder(
host_output_folder, backproject_result.output_file_path
)
else:
host_output_slices = combine_unknown_folder(
host_output_folder, backproject_result.output_file_path
)
save_result = save_output_for_backproject_docker(
host_output_file,
host_output_slices=host_output_slices,
)
if save_result:
task.error = save_result
task.status = "error"
clear_plugin_folder()
def handle_task(task: Task):
"""
Handle a server task.
Parameters
----------
task : Task
The task to handle.
"""
try:
if isinstance(task, SliceTask):
handle_slice(task)
elif isinstance(task, BackProjectTask):
handle_backproject(task)
else:
raise ValueError("Invalid task type")
except BaseException as e:
task.status = "error"
task.error = str(e)
def handle_task_docker(task: Task):
"""
Handle a server task in a docker environment.
Parameters
----------
task : Task
The task to handle.
"""
try:
if isinstance(task, SliceTask):
handle_slice_docker(task)
elif isinstance(task, BackProjectTask):
handle_backproject_docker(task)
else:
raise ValueError("Invalid task type")
except BaseException as e:
task.status = "error"
task.error = str(e)