feat(block): enable timed replacement of all proxies

This commit is contained in:
Iain Learmonth 2023-04-26 15:54:24 +01:00
parent 710fe05983
commit c424b9a5fa

View file

@ -4,6 +4,7 @@ import random
import string
from typing import Tuple, List
from flask import current_app
from tldextract import tldextract
from app import db
@ -68,16 +69,7 @@ class ProxyMetaAutomation(BaseAutomation):
frequency = 1
def automate(self, full: bool = False) -> Tuple[bool, str]:
# Destroy expired proxies
cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=4)
proxies: List[Proxy] = Proxy.query.filter(
Proxy.destroyed.is_(None),
Proxy.deprecated < cutoff
).all()
for proxy in proxies:
logging.debug("Destroying expired proxy")
proxy.destroy()
# Deprecate orphaned proxies and mismatched proxies
# Deprecate orphaned proxies, old proxies and mismatched proxies
proxies = Proxy.query.filter(
Proxy.deprecated.is_(None),
Proxy.destroyed.is_(None),
@ -85,6 +77,12 @@ class ProxyMetaAutomation(BaseAutomation):
for proxy in proxies:
if proxy.origin.destroyed is not None:
proxy.deprecate(reason="origin_destroyed")
if proxy.origin_id in current_app.config.get("DAILY_REPLACEMENT_ORIGINS", []):
max_age_cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=1, seconds=86400 * random.random())
else:
max_age_cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=5, seconds=86400 * random.random())
if proxy.added < max_age_cutoff:
proxy.deprecate(reason="max_age_reached")
if proxy.origin.smart and not PROXY_PROVIDERS[proxy.provider].smart_proxies: # type: ignore[attr-defined]
proxy.deprecate(reason="not_smart_enough")
# Create new proxies
@ -101,5 +99,14 @@ class ProxyMetaAutomation(BaseAutomation):
if not proxies:
logging.debug("Creating new proxy for %s in pool %s", origin, pool)
create_proxy(pool, origin)
# Destroy expired proxies
expiry_cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=4)
proxies: List[Proxy] = Proxy.query.filter(
Proxy.destroyed.is_(None),
Proxy.deprecated < expiry_cutoff
).all()
for proxy in proxies:
logging.debug("Destroying expired proxy")
proxy.destroy()
db.session.commit()
return True, ""