From 1b2432bd5f44731479f68ca2e34bac64c3f9ba29 Mon Sep 17 00:00:00 2001 From: Adnan Awan Date: Mon, 15 Apr 2024 22:37:59 +0500 Subject: [PATCH] [spidermon] Fix subtract offset-naive and offset-aware (#439) --- spidermon/contrib/scrapy/monitors/monitors.py | 7 ++++++- spidermon/core/monitors.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spidermon/contrib/scrapy/monitors/monitors.py b/spidermon/contrib/scrapy/monitors/monitors.py index e530633e..33d0d5c5 100644 --- a/spidermon/contrib/scrapy/monitors/monitors.py +++ b/spidermon/contrib/scrapy/monitors/monitors.py @@ -489,11 +489,16 @@ def test_execution_time(self): max_execution_time = crawler.settings.getint(SPIDERMON_MAX_EXECUTION_TIME) if not max_execution_time: return - now = datetime.datetime.utcnow() + start_time = self.data.stats.get("start_time") if not start_time: return + if start_time.tzinfo: + now = self.utc_now_with_timezone() + else: + now = datetime.datetime.utcnow() + duration = now - start_time msg = "The job has exceeded the maximum execution time" diff --git a/spidermon/core/monitors.py b/spidermon/core/monitors.py index 5403a071..896d624c 100644 --- a/spidermon/core/monitors.py +++ b/spidermon/core/monitors.py @@ -1,3 +1,4 @@ +from datetime import datetime, timezone from unittest import TestCase from spidermon import settings @@ -101,6 +102,9 @@ def debug_tree(self, level=0): def _init_method(self): MonitorOptions.add_or_create(self.method.__func__) + def utc_now_with_timezone(self): + return datetime.utcnow().replace(tzinfo=timezone.utc) + def __repr__(self): return "".format(self.name, hex(id(self)))