feat: switch all timezone naive datetimes to timezone aware

This commit is contained in:
Iain Learmonth 2024-12-06 16:08:48 +00:00
parent 41fc0a73a5
commit e22abb383c
30 changed files with 322 additions and 226 deletions

View file

@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timezone
from typing import Any
from app.extensions import db
@ -122,7 +122,7 @@ class ProxyCloudfrontAutomation(ProxyAutomation):
proxy = Proxy.query.filter(Proxy.id == mod['address'][len('module.cloudfront_'):]).first()
proxy.url = "https://" + res['values']['domain_name']
proxy.slug = res['values']['id']
proxy.terraform_updated = datetime.datetime.utcnow()
proxy.terraform_updated = datetime.now(tz=timezone.utc)
break
# EC2 instances (smart proxies)
for g in state["values"]["root_module"]["child_modules"]:

View file

@ -1,16 +1,17 @@
import datetime
import logging
import random
import string
from collections import OrderedDict
from typing import Any, Dict, List, Optional, Tuple, Type
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional
from typing import OrderedDict as OrderedDictT
from typing import Tuple, Type
from tldextract import tldextract
from app import db
from app.models.base import Pool
from app.models.mirrors import Proxy, Origin
from app.models.mirrors import Origin, Proxy
from app.terraform import BaseAutomation
from app.terraform.proxy import ProxyAutomation
from app.terraform.proxy.azure_cdn import ProxyAzureCdnAutomation
@ -155,7 +156,7 @@ def auto_deprecate_proxies() -> None:
.all())
logging.debug("Max age: %s", max_age_proxies)
for proxy in max_age_proxies:
max_age_cutoff = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
max_age_cutoff = datetime.now(timezone.utc) - timedelta(
days=1, seconds=86400 * random.random()) # nosec: B311
if proxy.added < max_age_cutoff:
proxy.deprecate(reason="max_age_reached")
@ -168,7 +169,7 @@ def destroy_expired_proxies() -> None:
This function finds all proxies that are not already destroyed and have been deprecated for more than 4 days.
It then destroys these proxies.
"""
expiry_cutoff = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=4)
expiry_cutoff = datetime.now(timezone.utc) - timedelta(days=4)
proxies = Proxy.query.filter(
Proxy.destroyed.is_(None),
Proxy.deprecated < expiry_cutoff
@ -200,7 +201,7 @@ def promote_hot_spare_proxy(pool_id: int, origin: Origin) -> bool:
if not proxy:
return False
proxy.pool_id = pool_id
proxy.added = datetime.datetime.utcnow()
proxy.added = datetime.now(tz=timezone.utc)
return True
@ -281,8 +282,8 @@ class ProxyMetaAutomation(BaseAutomation):
# The random usage below is good enough for its purpose: to create a slug that
# hasn't been used recently.
proxy.slug = random_slug(origin.domain_name)
proxy.added = datetime.datetime.utcnow()
proxy.updated = datetime.datetime.utcnow()
proxy.added = datetime.now(tz=timezone.utc)
proxy.updated = datetime.now(tz=timezone.utc)
logging.debug("Creating proxy %s", proxy)
db.session.add(proxy)
return True