From 53d81e0d9f66f09b63e5b36bd33c79e8bc930215 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 16 Feb 2021 20:25:54 +0300 Subject: 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. --- gn3/commands.py | 12 ++++++++---- tests/unit/test_commands.py | 42 +++++++++++++++++++++++++++++++++++------- 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"), -- cgit v1.2.3