Loading...
Searching...
No Matches
transport_engine.py
1import certifi
2import os
3import sys
4import time
5import zipfile
6from gams import GamsWorkspace
7import gams.engine
8from gams.engine.api import jobs_api
9
10
11if __name__ == "__main__":
12 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
13 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
14 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
15
16 model = "trnsport"
17 ws.gamslib(model)
18
19 model_data_path = os.path.join(ws.working_directory, model + ".zip")
20
21 with zipfile.ZipFile(model_data_path, "w", zipfile.ZIP_DEFLATED) as model_data:
22 model_data.write(
23 os.path.join(ws.working_directory, model + ".gms"), arcname=model + ".gms"
24 )
25
26 stdout_filename = "log_stdout.txt" # str | name of the file that captures stdout (default to 'log_stdout.txt')
27 arguments = [
28 "gdx=default"
29 ] # list[str] | arguments that will be passed to GAMS call (optional)
30
31 data = None # file | file containing data in zip (optional)
32 pf_file_name = None # str | name of the pf file in the zip, if there is (optional)
33 text_entries = [] # list[str] | (optional)
34 stream_entries = [] # list[str] | (optional)
35 inex_file = None # file | optional file to filter what will be inside the result zip file (optional)
36
37 configuration = gams.engine.Configuration(
38 host=os.environ["ENGINE_URL"],
39 username=os.environ["ENGINE_USER"],
40 password=os.environ["ENGINE_PASSWORD"],
41 discard_unknown_keys=True,
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 with open(model_data_path, "rb") as model_data:
56 create_job_response = job_api_instance.create_job(
57 model,
58 namespace,
59 stdout_filename=stdout_filename,
60 model_data=model_data,
61 arguments=arguments,
62 )
63
64 token = create_job_response.token
65 print(f"Job token: {token}")
66
67 except gams.engine.ApiException as e:
68 print(f"Exception when calling JobsApi.create_job(): {e}\n")
69 sys.exit(1)
70
71 time_spent = 0
72 while True:
73 try:
74 resp = job_api_instance.pop_job_logs(token)
75 print(resp.message, end="")
76 if resp.queue_finished:
77 break
78 time.sleep(0.5)
79 except gams.engine.ApiException as e:
80 if e.status == 403:
81 print("Job still in queue. Wait 0.5 seconds.")
82 time.sleep(0.5)
83 time_spent += 0.5
84 if time_spent > 120:
85 print(
86 "The Engine instance seems to be busy. Please try again later."
87 )
88 sys.exit(1)
89 else:
90 raise e
91
92 if job_api_instance.get_job(token).process_status != 0:
93 print("Job did not terminate successfully.")
94
95 try:
96 print(f"Fetching results of model: {model}")
97 with zipfile.ZipFile(job_api_instance.get_job_zip(token)) as zf:
98 gdx_file = zf.extract(model + ".gdx", path=ws.working_directory)
99 except gams.engine.ApiException as e:
100 print(f"Exception when calling JobsApi.get_job_zip(): {e}\n")
101 sys.exit(1)
102
103 try:
104 # remove results from server
105 job_api_instance.delete_job_zip(token)
106 except gams.engine.ApiException as e:
107 print(f"Exception when calling JobsApi.delete_job_zip(): {e}\n")
108 sys.exit(1)
109
110 result_db = ws.add_database_from_gdx(
111 os.path.join(ws.working_directory, gdx_file)
112 )
113
114 for rec in result_db["x"]:
115 print(
116 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
117 )