aboutsummaryrefslogtreecommitdiff
path: root/sheepdog/worker.py
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-16 16:19:05 +0300
committerBonfaceKilz2021-02-16 16:22:11 +0300
commitec0f9f611796ab67ce816d07f807701cb688cbeb (patch)
tree38de1161dc7b2f691505bc445cdb101a22c872e2 /sheepdog/worker.py
parentca3ed6f2c954649429275d544f3e48a00ab4905a (diff)
downloadgenenetwork3-ec0f9f611796ab67ce816d07f807701cb688cbeb.tar.gz
Add basic worker for processing commands
This will later be replaced with GNU Guile
Diffstat (limited to 'sheepdog/worker.py')
-rw-r--r--sheepdog/worker.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/sheepdog/worker.py b/sheepdog/worker.py
new file mode 100644
index 0000000..b5681ce
--- /dev/null
+++ b/sheepdog/worker.py
@@ -0,0 +1,36 @@
+"""Daemon that processes commands"""
+import os
+import sys
+import time
+import redis
+import redis.connection
+
+
+# Enable importing from one dir up since gn3 isn't installed as a globally
+sys.path.append(
+ os.path.abspath(os.path.join(os.path.dirname(__file__),
+ '..')))
+
+
+def run_jobs(conn):
+ """Process the redis using a redis connection, CONN"""
+ # pylint: disable=E0401, C0415
+ from gn3.commands import run_cmd
+ cmd_id = str(conn.lpop("GN2::job-queue"))
+ if bool(cmd_id):
+ cmd = conn.hget("cmd", cmd_id)
+ if cmd and (str(conn.hget(cmd, "status")) not in ["success",
+ "error"]):
+ result = run_cmd(cmd)
+ cmd.hset("result", result.get("output"), cmd_id)
+ if result.get("code") == 0: # Success
+ cmd.hset("status", "success", cmd_id)
+ else:
+ cmd.hset("status", "error", cmd_id)
+
+
+if __name__ == "__main__":
+ redis_conn = redis.Redis()
+ while True: # Daemon that keeps running forever:
+ run_jobs(redis_conn)
+ time.sleep(0.1)