From 58fff1c5d8c6233e9316ab6d8498cebb8199d6d9 Mon Sep 17 00:00:00 2001 From: Tanmoy Sarkar <57363826+tanmoysrt@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:33:48 +0530 Subject: [PATCH] fix: stop processing in create_usage_records job if it took more than 15min --- press/press/doctype/subscription/subscription.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/press/press/doctype/subscription/subscription.py b/press/press/doctype/subscription/subscription.py index b8d5a7c938..9d1d44accc 100644 --- a/press/press/doctype/subscription/subscription.py +++ b/press/press/doctype/subscription/subscription.py @@ -3,6 +3,8 @@ from __future__ import annotations +from datetime import datetime, timedelta + import frappe import rq from frappe.model.document import Document @@ -252,6 +254,7 @@ def create_usage_records(): """ Creates daily usage records for paid Subscriptions """ + start_time = datetime.now() free_sites = sites_with_free_hosting() settings = frappe.get_single("Press Settings") subscriptions = frappe.db.get_all( @@ -271,6 +274,10 @@ def create_usage_records(): for name in subscriptions: if has_job_timeout_exceeded(): return + if timedelta(datetime.now() - start_time).total_seconds() > 900: + # if job takes more than 15 minutes, stop process pending ones + # those ones will be picked up in the next job + break subscription = frappe.get_cached_doc("Subscription", name) try: subscription.create_usage_record()