aboutsummaryrefslogtreecommitdiff
path: root/gn3/commands.py
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-12 21:12:47 +0300
committerBonfaceKilz2021-02-12 21:13:30 +0300
commit9d5908f8fbf6e9f0b4f4567f61098653109c436c (patch)
treed1c4f9fb465a74402230ed6ffd78957a8fe24aea /gn3/commands.py
parentf887baaf66cc157f0c77237c41e70e42fe03549c (diff)
downloadgenenetwork3-9d5908f8fbf6e9f0b4f4567f61098653109c436c.tar.gz
Add new procedure for queueing a command
* gn3/commands.py (queue_cmd): New procedure. * tests/unit/test_commands.py: New test-cases for ^^.
Diffstat (limited to 'gn3/commands.py')
-rw-r--r--gn3/commands.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/gn3/commands.py b/gn3/commands.py
index 50b6d29..9692f62 100644
--- a/gn3/commands.py
+++ b/gn3/commands.py
@@ -1,9 +1,13 @@
"""Procedures used to work with the various bio-informatics cli
commands"""
+from datetime import datetime
from typing import Dict
from typing import List
from typing import Optional
+from uuid import uuid4
+from redis.client import Redis # Used only in type hinting
+from gn3.exceptions import RedisConnectionError
from gn3.file_utils import lookup_file
from gn3.file_utils import jsonfile_to_dict
@@ -40,3 +44,28 @@ def compose_gemma_cmd(
cmd += (" "
" ".join([f" {arg}" for arg in gemma_args]))
return cmd
+
+
+def queue_cmd(cmd: str, conn: Redis) -> str:
+ """Given a command CMD, and a redis connection CONN, queue it in Redis
+with an initial status of 'queued'. The following status codes are
+supported:
+
+ queued: Unprocessed; Still in the queue
+ running: Still running
+ success: Successful completion
+ error: Erroneous completion
+
+ """
+ if not conn.ping():
+ raise RedisConnectionError
+ unique_id = ("cmd::"
+ f"{datetime.now().strftime('%Y-%m-%d%H-%M%S-%M%S-')}"
+ f"{str(uuid4())}")
+ for key, value in {"cmd": cmd,
+ "result": "",
+ "status": "queued"}.items():
+ conn.hset(key, value, unique_id)
+ conn.rpush("GN2::job-queue",
+ unique_id)
+ return unique_id