9from gams
import GamsWorkspace
10from gams.engine.api
import jobs_api
12if __name__ ==
"__main__":
13 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
14 work_dir = sys.argv[2]
if len(sys.argv) > 2
else None
15 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
20 model_data_path = os.path.join(ws.working_directory, model +
".zip")
22 with zipfile.ZipFile(model_data_path,
"w", zipfile.ZIP_DEFLATED)
as model_data:
24 os.path.join(ws.working_directory, model +
".gms"), arcname=model +
".gms"
27 stdout_filename =
"log_stdout.txt"
38 configuration = gams.engine.Configuration(
39 host=os.environ[
"ENGINE_URL"],
40 username=os.environ[
"ENGINE_USER"],
41 password=os.environ[
"ENGINE_PASSWORD"],
42 ssl_ca_cert=certifi.where(),
44 configuration.temp_folder_path = ws.working_directory
45 namespace = os.environ[
50 with gams.engine.ApiClient(configuration)
as api_client:
52 job_api_instance = jobs_api.JobsApi(api_client)
54 print(f
"Posting {model}")
56 with open(model_data_path,
"rb")
as f:
57 file_content = f.read()
59 create_job_response = job_api_instance.create_job(
62 stdout_filename=stdout_filename,
63 model_data=(os.path.basename(model_data_path), file_content),
67 token = create_job_response.token
68 print(f
"Job token: {token}")
70 except gams.engine.ApiException
as e:
71 print(f
"Exception when calling JobsApi.create_job(): {e}\n")
77 resp = job_api_instance.pop_job_logs(token)
78 print(resp.message, end=
"")
79 if resp.queue_finished:
82 except gams.engine.ApiException
as e:
84 print(
"Job still in queue. Wait 0.5 seconds.")
89 "The Engine instance seems to be busy. Please try again later."
95 if job_api_instance.get_job(token).process_status != 0:
96 print(
"Job did not terminate successfully.")
99 print(f
"Fetching results of model: {model}")
100 zip_content = job_api_instance.get_job_zip(token)
101 with zipfile.ZipFile(io.BytesIO(zip_content))
as zf:
102 gdx_file = zf.extract(model +
".gdx", path=ws.working_directory)
103 except gams.engine.ApiException
as e:
104 print(f
"Exception when calling JobsApi.get_job_zip(): {e}\n")
109 job_api_instance.delete_job_zip(token)
110 except gams.engine.ApiException
as e:
111 print(f
"Exception when calling JobsApi.delete_job_zip(): {e}\n")
114 result_db = ws.add_database_from_gdx(
115 os.path.join(ws.working_directory, gdx_file)
118 for rec
in result_db[
"x"]:
120 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"