Skip to content

Commit

Permalink
[spidermon] Fix subtract offset-naive and offset-aware (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
adnan-awan authored Apr 15, 2024
1 parent 049cb00 commit 1b2432b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion spidermon/contrib/scrapy/monitors/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions spidermon/core/monitors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from unittest import TestCase

from spidermon import settings
Expand Down Expand Up @@ -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 "<MONITOR:({}) at {}>".format(self.name, hex(id(self)))

Expand Down

0 comments on commit 1b2432b

Please sign in to comment.