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 string
from collections import OrderedDict
from datetime import timezone
from typing import Any, Dict, List, Optional, Tuple, Type
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 maximum age cutoff is randomly set to a time between 24 and 48 hours.
"""
proxies: List[Proxy] = all_active_proxies()
for proxy in proxies:
if proxy.origin.destroyed is not None:
proxy.deprecate(reason="origin_destroyed")
if proxy.origin.assets and proxy.origin.auto_rotation:
max_age_cutoff = datetime.datetime.utcnow() - datetime.timedelta(
days=1, seconds=86400 * random.random()) # nosec: B311
if proxy.added < max_age_cutoff:
proxy.deprecate(reason="max_age_reached")
proxy.destroy()
origin_destroyed_proxies = (db.session.query(Proxy)
.join(Origin, Proxy.origin_id == Origin.id)
.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")
max_age_proxies = (db.session.query(Proxy)
.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
if proxy.added < max_age_cutoff:
proxy.deprecate(reason="max_age_reached")
def destroy_expired_proxies() -> None: