Skip to content

Commit 6a36187

Browse files
committed
feat: implement daemon behaviour
1 parent cc98b1d commit 6a36187

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

config/arena0/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ runtime:
1111
namespace: public
1212
#uuid: # leave blank to generate a new uuid; or set a fixed uuid value
1313
uuid: cb65196b-0537-4364-939a-87d004babd4c # this would make the runtime have a fixed uuuid
14-
reg_attempts: 1 # -1 = skip registration; 0 = infinite
14+
reg_attempts: -1 # -1 = skip registration; 0 = infinite
1515
reg_timeout_seconds: 5
1616
reg_fail_error: false
1717
max_nmodules: 100

config/arenaxr/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ runtime:
1111
namespace: public
1212
#uuid: # leave blank to generate a new uuid; or set a fixed uuid value
1313
uuid: cb65196b-0537-4364-939a-87d004babd4c # this would make the runtime have a fixed uuuid
14-
reg_attempts: 1 # -1 = skip registration; 0 = infinite
14+
reg_attempts: -1 # -1 = skip registration; 0 = infinite
1515
reg_timeout_seconds: 5
1616
reg_fail_error: false
1717
max_nmodules: 100

config/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ runtime:
1111
namespace: public # a namespace your runtime can publish registration, keepalives, errors to; defaults to 'public' if not given
1212
#uuid: # leave blank to generate a new uuid; or set a fixed uuid value
1313
uuid: 0b99e423-e94e-435d-8e75-a3a3bf19a1c8 # this would make the runtime have a fixed uuuid
14-
reg_attempts: 0 # -1 = skip registration; 0 = infinite
14+
reg_attempts: -1 # -1 = skip registration; 0 = infinite
1515
reg_timeout_seconds: 5
1616
reg_fail_error: false # registration failure causes the runtime to exit
1717
max_nmodules: 100

src/main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logzero
99
from logzero import logger
1010
import time
11+
from threading import Event
1112

1213
import release
1314
from common.config import settings
@@ -38,13 +39,17 @@ def main(args):
3839
# pass runtime mngr as pubsub handler to mqtt client
3940
mqttc = MQTTListner(rtmngr, **settings.get('mqtt'), error_topic=settings.topics.runtimes)
4041

41-
# some time to init (just so following message does not appear in the middle of init log)
42-
time.sleep(2)
43-
44-
while True:
45-
choice = input("\nEnter Q to quit.\n")
46-
if choice.lower() == "q":
47-
break
42+
# wait for init to be done
43+
rtmngr.wait_init()
44+
45+
if args.daemon:
46+
print("Running as a daemon...")
47+
Event().wait()
48+
else:
49+
while True:
50+
choice = input("\nEnter Q to quit.\n")
51+
if choice.lower() == "q":
52+
break
4853

4954
if __name__ == "__main__":
5055
""" This is executed when run from the command line """

src/runtime/runtime_mngr.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ def __init__(self, **kwargs):
2929
self.__lastwill_msg = None
3030
self.__conn_event = threading.Event()
3131
self.__reg_event = threading.Event()
32+
self.__init_done_event = threading.Event()
3233
self.__ka_exit = threading.Event()
3334
self.__pending_delete_msgs: Dict[str, PubsubMessage] = {} # dictionary messages waiting module exit notification
3435
self.__exited=False;
3536

37+
3638
self.__rt = Runtime(topics=kwargs.get('topics', settings.get('topics')), **kwargs.get('runtime', settings.get('runtime')))
3739

3840
# register exit handler to send delete runtime request
@@ -94,7 +96,12 @@ def pubsub_connected(self, client):
9496
def pubsub_error(self, desc, data):
9597
logger.error(desc, data)
9698

97-
def wait_reg(self, timeout_secs):
99+
def wait_init(self, timeout_secs=15):
100+
evt_flag = self.__init_done_event.wait(timeout_secs)
101+
if not evt_flag:
102+
raise RuntimeException("timeout waiting for init.", f"Runtime init failed after {timeout_secs} secs")
103+
104+
def __wait_reg(self, timeout_secs=10):
98105
evt_flag = self.__conn_event.wait(10)
99106
if not evt_flag:
100107
raise RuntimeException("timeout waiting for MQTT connection.", "Could not connect.")
@@ -124,17 +131,16 @@ def __register_runtime_send(self, reg_msg, timeout_secs, reg_attempts, reg_fail_
124131
until register event is set"""
125132

126133
reg_count = reg_attempts
134+
if reg_attempts == 0: reg_count = -1
127135
reg_flag = False
128136
while True:
129-
if reg_count > 0:
130-
logger.info("Runtime attempting to register...");
131-
self.__pubsub_client.message_publish(reg_msg)
132-
reg_flag = self.wait_reg(timeout_secs)
133-
if reg_flag == True: break # event is set; registration response received
134-
reg_count = reg_count - 1
135-
if reg_count == 0: break
137+
logger.info(f"Runtime attempting to register... {reg_count}");
138+
self.__pubsub_client.message_publish(reg_msg)
139+
reg_flag = self.__wait_reg(timeout_secs)
140+
if reg_flag == True: break # event is set; registration response received
141+
if reg_count > 0: reg_count = reg_count - 1
142+
if reg_count == 0: break
136143

137-
138144
if not reg_flag:
139145
if reg_fail_error: raise RuntimeException("runtime registration failed.", "Could not register runtime after {} attempts.".format(reg_attempts))
140146
else:
@@ -159,7 +165,9 @@ def __register_runtime_done(self):
159165
args=(ka_interval_sec,))
160166
self.__ka_thread.start()
161167

162-
logger.info("Runtime registration done.")
168+
# flag init is done
169+
logger.info("Runtime registration done (or skiped).")
170+
self.__init_done_event.set()
163171

164172
def reg(self, decoded_msg):
165173
msg_data = decoded_msg.get('data')

start-runtime1.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
docker pull slframework/slruntime-python-runner-repo-head
66
docker pull slframework/slruntime-python-runner
77

8+
./stop-runtime1.sh > /dev/null
9+
810
TARGET="${1:-arenaxr}"
911
screen -L -Logfile slruntime1.log -S slruntime1 -dm bash -c "make config/${TARGET}"

0 commit comments

Comments
 (0)