Loading...
Searching...
No Matches
transport_engine.py
1import io
2import os
3import sys
4import time
5import zipfile
6
7import certifi
8import gams.engine
9from gams import GamsWorkspace
10from gams.engine.api import jobs_api
11
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)
16
17 model = "trnsport"
18 ws.gamslib(model)
19
20 model_data_path = os.path.join(ws.working_directory, model + ".zip")
21
22 with zipfile.ZipFile(model_data_path, "w", zipfile.ZIP_DEFLATED) as model_data:
23 model_data.write(
24 os.path.join(ws.working_directory, model + ".gms"), arcname=model + ".gms"
25 )
26
27 stdout_filename = "log_stdout.txt" # str | name of the file that captures stdout (default to 'log_stdout.txt')
28 arguments = [
29 "gdx=default"
30 ] # list[str] | arguments that will be passed to GAMS call (optional)
31
32 data = None # file | file containing data in zip (optional)
33 pf_file_name = None # str | name of the pf file in the zip, if there is (optional)
34 text_entries = [] # list[str] | (optional)
35 stream_entries = [] # list[str] | (optional)
36 inex_file = None # file | optional file to filter what will be inside the result zip file (optional)
37
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(),
43 )
44 configuration.temp_folder_path = ws.working_directory
45 namespace = os.environ[
46 "ENGINE_NAMESPACE"
47 ] # str | namespace containing(or will contain) the model
48
49 # enter a context with an instance of the API client
50 with gams.engine.ApiClient(configuration) as api_client:
51 # create an instance of the API class
52 job_api_instance = jobs_api.JobsApi(api_client)
53 try:
54 print(f"Posting {model}")
55
56 with open(model_data_path, "rb") as f:
57 file_content = f.read()
58
59 create_job_response = job_api_instance.create_job(
60 model,
61 namespace,
62 stdout_filename=stdout_filename,
63 model_data=(os.path.basename(model_data_path), file_content),
64 arguments=arguments,
65 )
66
67 token = create_job_response.token
68 print(f"Job token: {token}")
69
70 except gams.engine.ApiException as e:
71 print(f"Exception when calling JobsApi.create_job(): {e}\n")
72 sys.exit(1)
73
74 time_spent = 0.0
75 while True:
76 try:
77 resp = job_api_instance.pop_job_logs(token)
78 print(resp.message, end="")
79 if resp.queue_finished:
80 break
81 time.sleep(0.5)
82 except gams.engine.ApiException as e:
83 if e.status == 403:
84 print("Job still in queue. Wait 0.5 seconds.")
85 time.sleep(0.5)
86 time_spent += 0.5
87 if time_spent > 120:
88 print(
89 "The Engine instance seems to be busy. Please try again later."
90 )
91 sys.exit(1)
92 else:
93 raise e
94
95 if job_api_instance.get_job(token).process_status != 0:
96 print("Job did not terminate successfully.")
97
98 try:
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")
105 sys.exit(1)
106
107 try:
108 # remove results from server
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")
112 sys.exit(1)
113
114 result_db = ws.add_database_from_gdx(
115 os.path.join(ws.working_directory, gdx_file)
116 )
117
118 for rec in result_db["x"]:
119 print(
120 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
121 )