-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_data_processor.py
218 lines (190 loc) · 7.11 KB
/
main_data_processor.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import logging
from typing import Tuple, Optional
import time
import os
from dane.config import cfg
from dane.s3_util import validate_s3_uri
from io_util import (
get_base_output_dir,
get_output_file_path,
get_s3_output_file_uri,
generate_output_dirs,
get_source_id_from_tar,
obtain_input_file,
transfer_output,
delete_local_output,
delete_input_file,
validate_data_dirs,
)
from models import (
CallbackResponse,
ThisWorkerInput,
ThisWorkerOutput,
OutputType,
)
from dane.provenance import (
Provenance,
obtain_software_versions,
generate_initial_provenance,
stop_timer_and_persist_provenance_chain,
)
logger = logging.getLogger(__name__)
DANE_WORKER_ID = "dane-example-worker"
def run(input_file_path: str) -> Tuple[CallbackResponse, Optional[Provenance]]:
"""Main function to start the process.
Triggered by running: python worker.py --run-test-file
Params:
input_file_path: where to read input from
Returns:
CallbackResponse: the main processing result
Provenance: a Provenance object describing the processing
"""
# there must be an input file
if not input_file_path:
logger.error("input file empty")
return {"state": 403, "message": "Error, no input file"}, []
# check if the file system is setup properly
if not validate_data_dirs():
logger.info("ERROR: data dirs not configured properly")
return {"state": 500, "message": "Input & output dirs not ok"}, []
# create the top-level provenance
# TODO: add proper name and description
top_level_provenance = generate_initial_provenance(
name="",
description=(""),
input_data={"input_file_path": input_file_path},
parameters=dict(cfg.WORKER_SETTINGS),
software_version=obtain_software_versions(DANE_WORKER_ID),
)
provenance_chain = [] # will contain the steps of the top-level provenance
# S3 URI, local tar.gz or locally extracted tar.gz is allowed
if validate_s3_uri(input_file_path):
model_input = obtain_input_file(input_file_path)
else:
logger.info("Using local input instead of fetching from S3")
if input_file_path.find(".tar.gz") != -1:
source_id = get_source_id_from_tar(input_file_path)
else:
source_id = input_file_path.split("/")[-2]
model_input = ThisWorkerInput(
200,
f"Processing tar.gz archive: {input_file_path}",
source_id,
input_file_path,
None, # no download provenance when using local file
)
# add the download provenance
if model_input.provenance:
provenance_chain.append(model_input.provenance)
# first generate the output dirs
generate_output_dirs(model_input.source_id)
# apply model to input & extract features
proc_result = apply_model(model_input)
if proc_result.provenance:
provenance_chain.append(proc_result.provenance)
# as a last piece of output, generate the provenance.json before packaging&uploading
full_provenance_chain = stop_timer_and_persist_provenance_chain(
provenance=top_level_provenance,
output_data={
"output_path": get_base_output_dir(model_input.source_id),
"output_uri": get_s3_output_file_uri(model_input.source_id),
},
provenance_chain=provenance_chain,
provenance_file_path=get_output_file_path(
model_input.source_id, OutputType.PROVENANCE
),
)
# if all is ok, apply the I/O steps on the outputted features
validated_output: CallbackResponse = apply_desired_io_on_output(
model_input,
proc_result,
cfg.INPUT.DELETE_ON_COMPLETION,
cfg.OUTPUT.DELETE_ON_COMPLETION,
cfg.OUTPUT.TRANSFER_ON_COMPLETION,
)
logger.info("Results after applying desired I/O")
logger.info(validated_output)
return validated_output, full_provenance_chain
def apply_model(
feature_extraction_input: ThisWorkerInput,
) -> ThisWorkerOutput:
logger.info("Starting model application")
start = time.time() * 1000 # convert to ms
file_to_read = os.path.join(
feature_extraction_input.input_file_path,
feature_extraction_input.source_id + ".input",
)
with open(file_to_read, "r") as f:
cnt = len(f.readline().split())
destination = get_output_file_path(
feature_extraction_input.source_id, OutputType.FOOBAR
)
with open(destination, "w") as f:
for i in range(cnt):
f.write("Hello world")
time.sleep(3) # wait 3 seconds
end = time.time() * 1000 # convert to ms
model_application_provenance = Provenance(
activity_name="hello world\n",
activity_description="some dummy processing",
input_data="", # TODO: what what
start_time_unix=start,
parameters={},
software_version="",
output_data={},
processing_time_ms=end - start,
)
if not model_application_provenance:
return ThisWorkerOutput(500, "Failed to apply model")
return ThisWorkerOutput(
200,
"Succesfully applied model",
get_base_output_dir(feature_extraction_input.source_id),
model_application_provenance,
)
# assesses the output and makes sure input & output is handled properly
def apply_desired_io_on_output(
model_input: ThisWorkerInput,
proc_result: ThisWorkerOutput,
delete_input_on_completion: bool,
delete_output_on_completetion: bool,
transfer_output_on_completion: bool,
) -> CallbackResponse:
# raise exception on failure
if proc_result.state != 200:
logger.error(f"Could not process the input properly: {proc_result.message}")
# something went wrong inside the work processor, return that response
return {"state": proc_result.state, "message": proc_result.message}
# process returned successfully, generate the output
source_id = model_input.source_id
output_path = get_base_output_dir(source_id) # TODO actually make sure this works
# transfer the output to S3 (if configured so)
transfer_success = True
if transfer_output_on_completion:
transfer_success = transfer_output(source_id)
# failure of transfer, impedes the workflow, so return error
if not transfer_success:
return {
"state": 500,
"message": "Failed to transfer output to S3",
}
# clear the output files (if configured so)
if delete_output_on_completetion:
delete_success = delete_local_output(source_id)
if not delete_success:
# NOTE: just a warning for now, but one to keep an eye out for
logger.warning(f"Could not delete output files: {output_path}")
# clean the input file (if configured so)
if not delete_input_file(
model_input.input_file_path,
model_input.source_id,
delete_input_on_completion,
):
return {
"state": 500,
"message": "Applied model, but could not delete the input file",
}
return {
"state": 200,
"message": "Successfully applied model",
}