From 9e3f5a8d09c2ecf0bc0eb5e497b52ba53b2e8435 Mon Sep 17 00:00:00 2001 From: Lukasz Maslanka Date: Wed, 13 Dec 2023 19:57:56 +0100 Subject: [PATCH] supervisord listener for updating clients --- bin/update_clients.py | 42 ++++++++++++++++++++++++---- etc/supervisor.d/update_clients.conf | 10 +++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 etc/supervisor.d/update_clients.conf diff --git a/bin/update_clients.py b/bin/update_clients.py index 8a26dc9..9dd8478 100755 --- a/bin/update_clients.py +++ b/bin/update_clients.py @@ -1,8 +1,9 @@ #! /usr/bin/env python3 -import toml, json, subprocess +import toml, json, subprocess, sys +from datetime import datetime, time -HERMES = "hermes" +HERMES = "/usr/local/bin/hermes" HERMES_CONFIG = "/hermes/config.toml" @@ -75,9 +76,40 @@ def update_clients(): result = output["result"] if output["status"] == "success": - print(f"Success: {client_id} ({chain_id})") + write_stderr(f"Success: {client_id} ({chain_id})\n") else: - print(f"Failed: {result}") + write_stderr(f"Failed: {result}\n") -update_clients() +def is_update_time(): + current_time = datetime.utcnow().time() + + update_start_time = time(0, 0) + update_end_time = time(1, 0) + + return update_start_time <= current_time < update_end_time + + +def write_stdout(s): + sys.stdout.write(s) + sys.stdout.flush() + + +def write_stderr(s): + sys.stderr.write(s) + sys.stderr.flush() + + +def main(): + while True: + write_stdout("READY\n") + sys.stdin.readline() + + if is_update_time(): + update_clients() + + write_stdout("RESULT 2\nOK") + + +if __name__ == "__main__": + main() diff --git a/etc/supervisor.d/update_clients.conf b/etc/supervisor.d/update_clients.conf new file mode 100644 index 0000000..4d49de0 --- /dev/null +++ b/etc/supervisor.d/update_clients.conf @@ -0,0 +1,10 @@ +[eventlistener:update_clients] +command=/usr/local/bin/update_clients.py +user=hermes +environment=HOME="/hermes" +autostart=true +events=TICK_3600 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0