-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_study.py
159 lines (131 loc) · 4.73 KB
/
run_study.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
"""Test osparc client API"""
import json
import os
import shutil
import time
import zipfile
from pathlib import Path
import numpy
import osparc
import osparc.api
import osparc_client
import pdb_attach
pdb_port = int(numpy.random.uniform(10000, 50000))
pdb_attach.listen(pdb_port)
print(f"PDB listening on port: {pdb_port}")
print(f"My PID is: {os.getpid()}")
osparc_conf = json.loads(Path("osparc_conf.json").read_text())
template_id = osparc_conf["template_id"]
del osparc_conf["template_id"]
osparc_cfg = osparc.Configuration(
retry_status_codes={429, 502, 503, 504, 404, 506}, **osparc_conf
)
MAX_N_OF_ATTEMPTS = 5
RETRY_MEAN_START_INTERVAL = 2
with osparc.ApiClient(
osparc_cfg,
) as api_client:
studies_api = osparc_client.StudiesApi(api_client)
# template_id = "61b1bb42-ba03-11ee-986b-02420a00001c" # dummy
# template_id = "1d99c612-bac5-11ee-986b-02420a00001c" # dummypython
# template_id = "ba57796c-bab2-11ee-986b-02420a00001c"
# template_id = (
# "851bad86-bb84-11ee-abfc-02420a00001c" # pythonrunnerservicetest
# )
# template_id = (
# "f580a480-bb9a-11ee-b73f-02420a000006" # PythonRunnerStudyService
# )
# template_id = "02621c5a-bbba-11ee-ba85-02420a000022"
# template_id = "a13d566e-c05b-11ee-95bf-02420a000008"
# job_id = "32ccb81c-bc34-11ee-ba85-02420a000022"
# local id template_id = "5b01fb90-f59f-11ee-9635-02420a140047"
# osparc-master template_id = "f5134716-fd88-11ee-ac84-02420a00f18a"
# template_id = "4b7a704a-007a-11ef-befd-0242ac114f07" # aws-osparc-master
print(studies_api.list_study_ports(study_id=template_id))
test_data_file = osparc.FilesApi(api_client).upload_file(
file=Path("input.data")
)
print("Uploaded test data file ")
test_json_file = osparc.FilesApi(api_client).upload_file(
file=Path("input.json")
)
print("Uploaded input json file ")
n_of_attempts = 0
time.sleep(
numpy.random.exponential(n_of_attempts * RETRY_MEAN_START_INTERVAL)
)
while True:
try:
n_of_attempts += 1
print("Creating job")
new_job = studies_api.create_study_job(
study_id=template_id,
job_inputs={
"values": {
# "InputNumber1": 0.5,
# "InputInteger1": 6,
"InputFile1": test_json_file,
}
},
)
print("Job created successfully")
break
except osparc_client.exceptions.ApiException:
if n_of_attempts >= MAX_N_OF_ATTEMPTS:
raise Exception(
f"Tried {n_of_attempts} times to create job but failed"
)
else:
print("Received API exception, retrying")
time.sleep(
numpy.random.exponential(
n_of_attempts * RETRY_MEAN_START_INTERVAL
)
)
print(f"New job created: {new_job}")
studies_api.start_study_job(study_id=template_id, job_id=new_job.id)
print("New job has started")
job_status = studies_api.inspect_study_job(
study_id=template_id, job_id=new_job.id
)
print(f"New job, status: {job_status.state}")
while job_status.state != "SUCCESS" and job_status.state != "FAILED":
job_status = studies_api.inspect_study_job(
study_id=template_id, job_id=new_job.id
)
print(f"Status: [{job_status.state}]")
time.sleep(1)
if job_status.state == "FAILED":
raise Exception("Job failed")
print(
studies_api.inspect_study_job(study_id=template_id, job_id=new_job.id)
)
output_file = None
n_of_attempts = 0
while output_file is None:
n_of_attempts += 1
job_results = studies_api.get_study_job_outputs(
study_id=template_id, job_id=new_job.id
).results
print(job_results)
output_file = job_results["OutputFile1"]
if n_of_attempts >= MAX_N_OF_ATTEMPTS:
raise Exception(
f"Tried {n_of_attempts} times to get job output file "
"but failed"
)
time.sleep(2)
output_filename = job_results["OutputFile1"].filename
output_file = Path(
osparc.FilesApi(api_client).download_file(
job_results["OutputFile1"].id
)
)
studies_api.delete_study_job(study_id=template_id, job_id=new_job.id)
shutil.move(output_file, output_filename)
output_file = Path(output_filename)
# print(output_file.resolve())
with zipfile.ZipFile(output_file, "r") as zip_ref:
zip_ref.extractall()
outdata_file = Path("output.json")
print(outdata_file.read_text())