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) == []