-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.py
executable file
·278 lines (237 loc) · 9.73 KB
/
executor.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#! /usr/bin/env python3
import os
import random
import string
import subprocess
import sys
import threading
import time
from commlib.node import Node
from commlib.utils import Rate
from commlib.msg import RPCMessage, PubSubMessage
from goal_dsl.codegen import generate_str
import config as config
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
def stream_logger(stream, log_func):
for line in iter(stream.readline, ""):
log_func(line.strip())
class ExecuteModelMsg(RPCMessage):
class Request(RPCMessage.Request):
model: str
class Response(RPCMessage.Response):
status: int = 1
result: str = ""
class ExecuteModelAsyncMsg(PubSubMessage):
model: str
class KillAllAsyncMsg(PubSubMessage):
pass
class KillAllRPCMsg(RPCMessage):
class Request(RPCMessage.Request):
pass
class Response(RPCMessage.Response):
status: int = 1
error: str = ""
class CodeRunnerState:
IDLE = 0
RUNNING = 1
TERMINATED = 2
class CodeRunner:
def __init__(self, code: str, timeout: float = None):
self._code = code
self._state = CodeRunnerState.IDLE
self._timeout = timeout
self._uid = CodeRunner.generate_random_string()
@staticmethod
def generate_random_string(length: int = 16) -> str:
"""Generate a random string of fixed length."""
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for _ in range(length))
@property
def state(self) -> CodeRunnerState:
return self._state
@property
def elapsed_time(self) -> float:
return round(time.time() - self._start_t, 2)
def run(self, wait: bool = True):
self._state = CodeRunnerState.RUNNING
self._start_t = time.time()
self._run_subprocess(wait)
logging.info(f"Started CodeRunner <{self._uid}>")
def stop(self):
logging.warning(f'Stopping CodeRunner {self._uid}...')
self.force_exit()
def _run_subprocess(self, wait: bool = True):
logging.info(f"Running Subprocess {self._uid} with flag wait={wait}...")
self._process = subprocess.Popen(
["python3", "-c", self._code],
env={**os.environ.copy(), "U_ID": self._uid},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
# shell=True,
text=True,
bufsize=1
)
logging.info("Process started successfully")
self._stdout_thread = threading.Thread(target=stream_logger,
args=(self._process.stdout, logging.info))
self._stderr_thread = threading.Thread(target=stream_logger,
args=(self._process.stderr, logging.error))
self._stdout_thread.start()
self._stderr_thread.start()
logging.info("Started logging threads")
if wait:
logging.warning(f"Waiting for Coderunner <UID:{self._uid}> to terminate")
self._process.wait()
self._stdout_thread.join()
self._stderr_thread.join()
self._state = CodeRunnerState.TERMINATED
logging.warning(f"CodeRunner <UID:{self._uid}> terminated")
def force_exit(self):
self._process.kill()
self._stdout_thread.join(1)
self._stderr_thread.join(1)
self._process.wait(1)
self._state = CodeRunnerState.TERMINATED
logging.warning(f"CodeRunner <PID:{self._process.pid}> forcefully terminated")
def report(self):
if self._process.returncode != 0:
logging.error(f"Subprocess failed with return code {self._process.returncode}")
else:
logging.info("Subprocess completed successfully!")
class GoalDSLExecutorNode(Node):
def __init__(self, local_redis: bool = False, *args, **kwargs):
super().__init__(node_name="goadsl-executor", *args, **kwargs)
self._rate = Rate(100) # 100 Hz
self._local_redis = local_redis
self._runners = []
self._execution_timeout = config.EXECUTION_TIMEOUT # 2 seconds
self._thread = None
self._tstop_event = threading.Event()
def start(self):
logging.info("Starting GoadslExecutorNode...")
self.report_conn_params()
self._init_endpoints()
self.run()
threading.Thread(target=self._run_tick_loop, daemon=True).start()
def _run_tick_loop(self):
while not self._tstop_event.is_set():
self.tick()
self._rate.sleep()
def stop(self):
logging.info("Stopping GoadslExecutorNode...")
self._tstop_event.set()
self._thread.join(1)
def tick(self):
for runner in self._runners:
timeout_cond = runner.elapsed_time >= self._execution_timeout \
if self._execution_timeout is not None else False
if runner and runner.state == CodeRunnerState.RUNNING and timeout_cond:
runner.stop()
for runner in self._runners:
if runner.state == CodeRunnerState.TERMINATED:
self._runners.remove(runner)
def report_conn_params(self):
logging.info("Connection parameters:")
logging.info(self._conn_params)
def _init_endpoints(self):
execute_model_rpc = self.create_rpc(
msg_type=ExecuteModelMsg,
rpc_name=config.EXECUTE_MODEL_RPC,
on_request=self.on_request_model_execution
)
logging.info(f"Registered RPC endpoint: {config.EXECUTE_MODEL_RPC}")
killall_goals_rpc = self.create_rpc(
msg_type=KillAllRPCMsg,
rpc_name=config.KILL_ALL_GOALS_RPC,
on_request=self.on_request_killall
)
logging.info(f"Registered RPC endpoint: {config.KILL_ALL_GOALS_RPC}")
execute_model_async_endpoint = self.create_subscriber(
topic=config.EXECUTE_MODEL_SUB,
on_message=self.on_message_execute_model,
msg_type=ExecuteModelAsyncMsg
)
logging.info(f"Registered Subscriber endpoint: {config.EXECUTE_MODEL_SUB}")
kill_all_goals_sub = self.create_subscriber(
topic=config.KILL_ALL_GOALS_SUB,
on_message=self.on_message_killall,
msg_type=KillAllAsyncMsg
)
logging.info(f"Registered Subscriber endpoint: {config.KILL_ALL_GOALS_SUB}")
self._execute_model_endpoint = execute_model_rpc
self._execute_model_async_endpoint = execute_model_async_endpoint
self._kill_all_goals_sub = kill_all_goals_sub
self._kill_all_goals_rpc = killall_goals_rpc
def on_request_model_execution(self, msg: ExecuteModelMsg.Request) -> ExecuteModelMsg.Response:
logging.info("Received model execution request")
try:
self._deploy_model(msg.model)
return ExecuteModelMsg.Response(status=1, result="Model executed successfully!")
except Exception as e:
logging.error(f"Error executing model: {e}", exc_info=False)
return ExecuteModelMsg.Response(status=0, result=f"Error executing model: {str(e)}")
def on_request_killall(self, msg: KillAllRPCMsg.Request) -> KillAllRPCMsg.Response:
logging.info("Received KillAll request")
try:
self._killall_goals()
return KillAllRPCMsg.Response(result="KillAll GoalDSL CodeRunners was successfully!")
except Exception as e:
logging.error(f"Error killing GoalDSL CodeRunners: {e}", exc_info=False)
return KillAllRPCMsg.Response(status=0, result=f"Error killing GoalDSL CodeRunners: {str(e)}")
def on_message_execute_model(self, msg: ExecuteModelAsyncMsg):
logging.info("Received model execution request")
try:
self._deploy_model(msg.model)
except Exception as e:
logging.error(f"Error deploying model: {e}", exc_info=False)
def on_message_killall(self, msg: KillAllAsyncMsg):
logging.warning("Received KillAll request!!!")
try:
self._killall_goals()
except Exception as e:
logging.error(f"Error while terminating goal executions in coderunner: {e}",
exc_info=False)
def _killall_goals(self):
for runner in self._runners:
runner.force_exit()
def _deploy_model(self, model_str: str):
logging.info("Running code-generator on input model")
code = generate_str(model_str)
logging.info("Running code-runner on generated code")
code_runner = CodeRunner(code)
code_runner.run(wait=config.WAIT_FOR_EXECUTION_TERMINATION)
self._runners.append(code_runner)
if __name__ == "__main__":
broker = config.BROKER_TYPE
if broker == "REDIS":
from commlib.transports.redis import ConnectionParameters
elif broker == "AMQP":
from commlib.transports.amqp import ConnectionParameters
elif broker == "MQTT":
from commlib.transports.mqtt import ConnectionParameters
else:
print("Not a valid broker-type was given!")
sys.exit(1)
conn_params = ConnectionParameters(
host=config.BROKER_HOST, port=config.BROKER_PORT,
username=config.BROKER_USERNAME, password=config.BROKER_PASSWORD,
ssl=config.BROKER_SSL
)
executor = GoalDSLExecutorNode(
connection_params=conn_params,
debug=config.DEBUG,
heartbeats=config.HEARTBEATS
)
executor.start()
if config.LOCAL_REDIS:
from commlib.transports.redis import ConnectionParameters as RedisConnParams
local_executor = GoalDSLExecutorNode(
connection_params=RedisConnParams(),
debug=config.DEBUG,
heartbeats=config.HEARTBEATS
)
local_executor.start()
while True:
time.sleep(0.01)