feat(block): enable timed replacement of all proxies
This commit is contained in:
parent
710fe05983
commit
c424b9a5fa
1 changed files with 17 additions and 10 deletions
|
@ -4,6 +4,7 @@ import random
|
||||||
import string
|
import string
|
||||||
from typing import Tuple, List
|
from typing import Tuple, List
|
||||||
|
|
||||||
|
from flask import current_app
|
||||||
from tldextract import tldextract
|
from tldextract import tldextract
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
|
@ -68,16 +69,7 @@ class ProxyMetaAutomation(BaseAutomation):
|
||||||
frequency = 1
|
frequency = 1
|
||||||
|
|
||||||
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
||||||
# Destroy expired proxies
|
# Deprecate orphaned proxies, old proxies and mismatched 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
|
|
||||||
proxies = Proxy.query.filter(
|
proxies = Proxy.query.filter(
|
||||||
Proxy.deprecated.is_(None),
|
Proxy.deprecated.is_(None),
|
||||||
Proxy.destroyed.is_(None),
|
Proxy.destroyed.is_(None),
|
||||||
|
@ -85,6 +77,12 @@ class ProxyMetaAutomation(BaseAutomation):
|
||||||
for proxy in proxies:
|
for proxy in proxies:
|
||||||
if proxy.origin.destroyed is not None:
|
if proxy.origin.destroyed is not None:
|
||||||
proxy.deprecate(reason="origin_destroyed")
|
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]
|
if proxy.origin.smart and not PROXY_PROVIDERS[proxy.provider].smart_proxies: # type: ignore[attr-defined]
|
||||||
proxy.deprecate(reason="not_smart_enough")
|
proxy.deprecate(reason="not_smart_enough")
|
||||||
# Create new proxies
|
# Create new proxies
|
||||||
|
@ -101,5 +99,14 @@ class ProxyMetaAutomation(BaseAutomation):
|
||||||
if not proxies:
|
if not proxies:
|
||||||
logging.debug("Creating new proxy for %s in pool %s", origin, pool)
|
logging.debug("Creating new proxy for %s in pool %s", origin, pool)
|
||||||
create_proxy(pool, origin)
|
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()
|
db.session.commit()
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue