aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-16 20:25:54 +0300
committerBonfaceKilz2021-02-16 20:25:54 +0300
commit53d81e0d9f66f09b63e5b36bd33c79e8bc930215 (patch)
treecb7e3c42573a3332bb9b63b3351cc049f31e704b
parent9e638622d93cc405ecd14c3a5f4288e08aecdddb (diff)
downloadgenenetwork3-53d81e0d9f66f09b63e5b36bd33c79e8bc930215.tar.gz
Add extra option to set email when queueing CMD
The e-mail field will be used to send the results of the computation when it is completed.
-rw-r--r--gn3/commands.py12
-rw-r--r--tests/unit/test_commands.py42
2 files changed, 43 insertions, 11 deletions
diff --git a/gn3/commands.py b/gn3/commands.py
index e5a3b1e..7201a68 100644
--- a/gn3/commands.py
+++ b/gn3/commands.py
@@ -48,10 +48,10 @@ def compose_gemma_cmd(
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:
+def queue_cmd(conn: Redis, cmd: str, email: Optional[str] = None) -> str:
+ """Given a command CMD; (optional) EMAIL; 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
@@ -70,6 +70,10 @@ supported:
conn.hset(key, value, unique_id)
conn.rpush("GN2::job-queue",
unique_id)
+ if email:
+ conn.hset("email",
+ email,
+ unique_id)
return unique_id
diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py
index b0c217d..9dfa9f9 100644
--- a/tests/unit/test_commands.py
+++ b/tests/unit/test_commands.py
@@ -46,15 +46,15 @@ class TestCommands(unittest.TestCase):
"""Test that the correct error is raised when Redis is unavailable"""
self.assertRaises(RedisConnectionError,
queue_cmd,
- "ls",
- MockRedis(ping=lambda: False,
- hset=mock.MagicMock(),
- rpush=mock.MagicMock()))
+ cmd="ls",
+ conn=MockRedis(ping=lambda: False,
+ hset=mock.MagicMock(),
+ rpush=mock.MagicMock()))
@mock.patch("gn3.commands.datetime")
@mock.patch("gn3.commands.uuid4")
- def test_queue_cmd_right_correct_calls_to_redis(self, mock_uuid4,
- mock_datetime):
+ def test_queue_cmd_correct_calls_to_redis(self, mock_uuid4,
+ mock_datetime):
"""Test that the cmd is queued properly"""
mock_uuid4.return_value = 1234
mock_datetime.now.return_value = datetime.fromisoformat('2021-02-12 '
@@ -64,7 +64,9 @@ class TestCommands(unittest.TestCase):
hset=mock.MagicMock(),
rpush=mock.MagicMock())
actual_unique_id = "cmd::2021-02-1217-3224-3224-1234"
- self.assertEqual(queue_cmd("ls", mock_redis_conn), actual_unique_id)
+ self.assertEqual(queue_cmd(cmd="ls",
+ conn=mock_redis_conn),
+ actual_unique_id)
mock_redis_conn.hset.assert_has_calls(
[mock.call("cmd", "ls", actual_unique_id),
mock.call("result", "", actual_unique_id),
@@ -72,6 +74,32 @@ class TestCommands(unittest.TestCase):
mock_redis_conn.rpush.assert_has_calls(
[mock.call("GN2::job-queue", actual_unique_id)])
+ @mock.patch("gn3.commands.datetime")
+ @mock.patch("gn3.commands.uuid4")
+ def test_queue_cmd_right_calls_to_redis_with_email(self,
+ mock_uuid4,
+ mock_datetime):
+ """Test that the cmd is queued properly when given the email"""
+ mock_uuid4.return_value = 1234
+ mock_datetime.now.return_value = datetime.fromisoformat('2021-02-12 '
+ '17:32:24.'
+ '859097')
+ mock_redis_conn = MockRedis(ping=lambda: True,
+ hset=mock.MagicMock(),
+ rpush=mock.MagicMock())
+ actual_unique_id = "cmd::2021-02-1217-3224-3224-1234"
+ self.assertEqual(queue_cmd(cmd="ls",
+ conn=mock_redis_conn,
+ email="me@me.com"),
+ actual_unique_id)
+ mock_redis_conn.hset.assert_has_calls(
+ [mock.call("cmd", "ls", actual_unique_id),
+ mock.call("result", "", actual_unique_id),
+ mock.call("status", "queued", actual_unique_id),
+ mock.call("email", "me@me.com", actual_unique_id)])
+ mock_redis_conn.rpush.assert_has_calls(
+ [mock.call("GN2::job-queue", actual_unique_id)])
+
def test_run_cmd_correct_input(self):
"""Test that a correct cmd is processed correctly"""
self.assertEqual(run_cmd("echo test"),