From fb017d2c29b96b3c3234437eef1687e7254c6f2d Mon Sep 17 00:00:00 2001 From: Ethan Wolinsky <58117461+eswolinsky3241@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:08:33 -0500 Subject: [PATCH] Delete expired groups from registry (#2150) --- rq/group.py | 8 +++++++- tests/test_group.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/rq/group.py b/rq/group.py index 94baf0dc..15e90256 100644 --- a/rq/group.py +++ b/rq/group.py @@ -89,7 +89,13 @@ class Group: def all(cls, connection: 'Redis') -> List['Group']: "Returns an iterable of all Groupes." group_keys = [as_text(key) for key in connection.smembers(cls.REDIS_GROUP_KEY)] - return [cls.fetch(key, connection=connection) for key in group_keys] + groups = [] + for key in group_keys: + try: + groups.append(cls.fetch(key, connection=connection)) + except NoSuchGroupError: + connection.srem(cls.REDIS_GROUP_KEY, key) + return groups @classmethod def get_key(cls, name: str) -> str: diff --git a/tests/test_group.py b/tests/test_group.py index adbfa59c..6383498c 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -142,3 +142,11 @@ class TestGroup(RQTestCase): assert len(all_groups) == 1 assert "group1" in [group.name for group in all_groups] assert "group2" not in [group.name for group in all_groups] + + def test_all_deletes_missing_groups(self): + q = Queue(connection=self.connection) + group = Group.create(connection=self.connection) + jobs = group.enqueue_many(q, [self.job_1_data]) + jobs[0].delete() + assert not self.connection.exists(Group.get_key(group.name)) + assert Group.all(connection=self.connection) == []