test: Cancel pending jobs in RQ tests (#32970)

This casues flake sometimes when other tests don't cleanup long running
pending jobs.
This commit is contained in:
Ankush Menat 2025-06-17 18:57:36 +05:30 committed by GitHub
parent 764410761f
commit 7e2b4955f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -112,6 +112,16 @@ class RQJob(Document):
except InvalidJobOperation:
frappe.msgprint(_("Job is not running."), title=_("Invalid Operation"))
@check_permissions
def cancel(self):
if self.status == "queued":
self.job.cancel()
else:
frappe.msgprint(
_("Job is in {0} state and can't be cancelled").format(self.status),
title=_("Invalid Operation"),
)
@staticmethod
def get_count(filters=None) -> int:
return len(RQJob.get_matching_job_ids(filters))

View file

@ -26,6 +26,12 @@ def wait_for_completion(job: Job):
class TestRQJob(IntegrationTestCase):
BG_JOB = "frappe.core.doctype.rq_job.test_rq_job.test_func"
def setUp(self) -> None:
# Cleanup all pending jobs
for job in frappe.get_all("RQ Job", {"status": "queued"}):
frappe.get_doc("RQ Job", job.name).cancel()
return super().setUp()
def check_status(self, job: Job, status, wait=True):
if wait:
wait_for_completion(job)