fix: do not rotate hotspare proxies on max age

closes: #42
This commit is contained in:
Iain Learmonth 2024-11-16 19:25:55 +00:00
parent d9b62a373d
commit 5c0170bdef

View file

@ -3,6 +3,7 @@ import logging
import random import random
import string import string
from collections import OrderedDict from collections import OrderedDict
from datetime import timezone
from typing import Any, Dict, List, Optional, Tuple, Type from typing import Any, Dict, List, Optional, Tuple, Type
from typing import OrderedDict as OrderedDictT from typing import OrderedDict as OrderedDictT
@ -136,16 +137,33 @@ def auto_deprecate_proxies() -> None:
- The "max_age_reached" reason means the proxy has been in use for longer than the maximum allowed period. - The "max_age_reached" reason means the proxy has been in use for longer than the maximum allowed period.
The maximum age cutoff is randomly set to a time between 24 and 48 hours. The maximum age cutoff is randomly set to a time between 24 and 48 hours.
""" """
proxies: List[Proxy] = all_active_proxies() origin_destroyed_proxies = (db.session.query(Proxy)
for proxy in proxies: .join(Origin, Proxy.origin_id == Origin.id)
if proxy.origin.destroyed is not None: .filter(
Proxy.destroyed.is_(None),
Proxy.deprecated.is_(None),
Origin.destroyed.is_not(None),
)
.all()
)
for proxy in origin_destroyed_proxies:
proxy.deprecate(reason="origin_destroyed") proxy.deprecate(reason="origin_destroyed")
if proxy.origin.assets and proxy.origin.auto_rotation: max_age_proxies = (db.session.query(Proxy)
max_age_cutoff = datetime.datetime.utcnow() - datetime.timedelta( .join(Origin, Proxy.origin_id == Origin.id)
.filter(
Proxy.destroyed.is_(None),
Proxy.deprecated.is_(None),
Proxy.pool_id != -1, # do not rotate hotspare proxies
Origin.assets,
Origin.auto_rotation,
)
.all()
)
for proxy in max_age_proxies:
max_age_cutoff = datetime.datetime.now(datetime.UTC) - datetime.timedelta(
days=1, seconds=86400 * random.random()) # nosec: B311 days=1, seconds=86400 * random.random()) # nosec: B311
if proxy.added < max_age_cutoff: if proxy.added < max_age_cutoff:
proxy.deprecate(reason="max_age_reached") proxy.deprecate(reason="max_age_reached")
proxy.destroy()
def destroy_expired_proxies() -> None: def destroy_expired_proxies() -> None: