diff --git a/cps/services/worker.py b/cps/services/worker.py
index 2c57689f91..cca65db882 100644
--- a/cps/services/worker.py
+++ b/cps/services/worker.py
@@ -25,7 +25,7 @@
import queue
except ImportError:
import Queue as queue
-from datetime import datetime
+from datetime import datetime, timezone
from collections import namedtuple
from cps import logger
@@ -92,7 +92,7 @@ def add(cls, user, task, hidden=False):
ins.queue.put(QueuedTask(
num=ins.num,
user=username,
- added=datetime.now(),
+ added=datetime.now(timezone.utc),
task=task,
hidden=hidden
))
@@ -191,7 +191,7 @@ def is_cancellable(self):
raise NotImplementedError
def start(self, *args):
- self.start_time = datetime.now()
+ self.start_time = datetime.now(timezone.utc)
self.stat = STAT_STARTED
# catch any unhandled exceptions in a task and automatically fail it
@@ -201,7 +201,7 @@ def start(self, *args):
self._handleError(str(ex))
log.error_or_exception(ex)
- self.end_time = datetime.now()
+ self.end_time = datetime.now(timezone.utc)
@property
def stat(self):
@@ -231,7 +231,8 @@ def error(self, x):
@property
def runtime(self):
- return (self.end_time or datetime.now()) - self.start_time
+ now = datetime.now(timezone.utc)
+ return (self.end_time or now) - self.start_time
@property
def dead(self):
diff --git a/cps/tasks/download.py b/cps/tasks/download.py
index d5d28945d8..19b64f1130 100644
--- a/cps/tasks/download.py
+++ b/cps/tasks/download.py
@@ -3,7 +3,7 @@
import requests
import select
import sqlite3
-from datetime import datetime
+from datetime import datetime, timezone
from flask_babel import lazy_gettext as N_, gettext as _
from cps.constants import XKLB_DB_FILE
@@ -25,14 +25,14 @@ def __init__(self, task_message, media_url, original_url, current_user_name, she
self.shelf_id = shelf_id
self.duration = datetime.utcfromtimestamp(int(duration)).strftime("%H:%M:%S") if duration else "unknown"
self.live_status = live_status
- self.start_time = self.end_time = datetime.now()
+ self.start_time = self.end_time = datetime.now(timezone.utc)
self.stat = STAT_WAITING
self.progress = 0
def run(self, worker_thread):
"""Run the download task"""
self.worker_thread = worker_thread
- self.start_time = self.end_time = datetime.now()
+ self.end_time = datetime.now(timezone.utc)
self.stat = STAT_STARTED
self.progress = 0
@@ -53,14 +53,14 @@ def run(self, worker_thread):
complete_progress_cycle = 0
- last_progress_time = datetime.now()
+ last_progress_time = datetime.now(timezone.utc)
timeout = 120 # seconds
self.message = f"Downloading {self.media_url_link}..."
if self.live_status == "was_live":
self.message += f" (formerly live video, length/duration {self.duration})"
while p.poll() is None:
- self.end_time = datetime.now()
+ self.end_time = datetime.now(timezone.utc)
# Check if there's data available to read
rlist, _, _ = select.select([p.stdout], [], [], 0.1)
if rlist:
@@ -77,9 +77,9 @@ def run(self, worker_thread):
self.progress = min(0.99, (complete_progress_cycle + (percentage / 100)) / 4)
if percentage == 100:
complete_progress_cycle += 1
- last_progress_time = datetime.now()
+ last_progress_time = datetime.now(timezone.utc)
else:
- elapsed_time = (datetime.now() - last_progress_time).total_seconds()
+ elapsed_time = (datetime.now(timezone.utc) - last_progress_time).total_seconds()
if elapsed_time >= timeout:
self.message = f"{self.media_url_link} is taking longer than expected. It could be a stuck download due to unavailable fragments (yt-dlp/yt-dlp#2137) and/or an error in xklb's media_check. Please wait as we keep trying. See #223 for more info."
sleep(0.1)
@@ -132,7 +132,7 @@ def run(self, worker_thread):
self.message = f"{self.media_url_link} failed to download: {self.read_error_from_database()}"
finally:
- self.end_time = datetime.now()
+ self.end_time = datetime.now(timezone.utc)
if p.returncode == 0 or self.progress == 1.0:
self.stat = STAT_FINISH_SUCCESS
log.info("Download task for %s completed successfully", self.media_url)
diff --git a/cps/tasks/metadata_extract.py b/cps/tasks/metadata_extract.py
index 8ec614d4a9..cc1018a271 100644
--- a/cps/tasks/metadata_extract.py
+++ b/cps/tasks/metadata_extract.py
@@ -2,7 +2,7 @@
import re
import requests
import sqlite3
-from datetime import datetime
+from datetime import datetime, timezone
from flask_babel import lazy_gettext as N_, gettext as _
from cps.constants import XKLB_DB_FILE, MAX_VIDEOS_PER_DOWNLOAD
@@ -23,7 +23,7 @@ def __init__(self, task_message, media_url, original_url, current_user_name):
self.original_url = self._format_original_url(original_url)
self.is_playlist = None
self.current_user_name = current_user_name
- self.start_time = self.end_time = datetime.now()
+ self.start_time = self.end_time = datetime.now(timezone.utc)
self.stat = STAT_WAITING
self.progress = 0
self.columns = None
@@ -112,7 +112,7 @@ def _update_metadata(self, requested_urls):
requested_urls = {url: requested_urls[url] for url in requested_urls.keys() if "shorts" not in url and url not in failed_urls}
def _calculate_views_per_day(self, requested_urls, conn):
- now = datetime.now()
+ now = datetime.now(timezone.utc)
for requested_url in requested_urls.keys():
try:
view_count = conn.execute("SELECT view_count FROM media WHERE path = ?", (requested_url,)).fetchone()[0]
@@ -153,7 +153,7 @@ def _add_download_tasks_to_worker(self, requested_urls):
def run(self, worker_thread):
self.worker_thread = worker_thread
log.info("Starting to fetch metadata for URL: %s", self.media_url)
- self.start_time = self.end_time = datetime.now()
+ self.end_time = datetime.now(timezone.utc)
self.stat = STAT_STARTED
self.progress = 0