fix: don't create missing proxies for hotspare

* also fixes setting creation time for pools
* improves logging in proxy creation pipeline
This commit is contained in:
Iain Learmonth 2024-11-29 16:08:33 +00:00
parent b91e078e22
commit 1bc2960278
2 changed files with 13 additions and 5 deletions

View file

@ -1,5 +1,6 @@
import logging
import secrets
from datetime import datetime
from datetime import datetime, UTC
from flask import render_template, url_for, flash, redirect, Response, Blueprint
from flask.typing import ResponseReturnValue
@ -55,15 +56,16 @@ def pool_new() -> ResponseReturnValue:
pool.description = form.description.data
pool.redirector_domain = form.redirector_domain.data if form.redirector_domain.data != "" else None
pool.api_key = secrets.token_urlsafe(nbytes=32)
pool.created = datetime.utcnow()
pool.updated = datetime.utcnow()
pool.added = datetime.now(UTC)
pool.updated = datetime.now(UTC)
try:
db.session.add(pool)
db.session.commit()
flash(f"Created new pool {pool.pool_name}.", "success")
return redirect(url_for("portal.pool.pool_edit", pool_id=pool.id))
except sqlalchemy.exc.SQLAlchemyError:
except sqlalchemy.exc.SQLAlchemyError as exc:
flash("Failed to create new pool.", "danger")
logging.exception(exc)
return redirect(url_for("portal.pool.pool_list"))
return render_template("new.html.j2", section="pool", form=form)
@ -86,7 +88,7 @@ def pool_edit(pool_id: int) -> ResponseReturnValue:
if form.api_key.data != pool.api_key:
pool.api_key = secrets.token_urlsafe(nbytes=32)
form.api_key.data = pool.api_key
pool.updated = datetime.utcnow()
pool.updated = datetime.now(UTC)
try:
db.session.commit()
flash("Saved changes to pool.", "success")

View file

@ -142,6 +142,7 @@ def auto_deprecate_proxies() -> None:
Proxy.deprecated.is_(None),
Origin.destroyed.is_not(None))
.all())
logging.debug("Origin destroyed: %s", origin_destroyed_proxies)
for proxy in origin_destroyed_proxies:
proxy.deprecate(reason="origin_destroyed")
max_age_proxies = (db.session.query(Proxy)
@ -152,6 +153,7 @@ def auto_deprecate_proxies() -> None:
Origin.assets,
Origin.auto_rotation)
.all())
logging.debug("Max age: %s", max_age_proxies)
for proxy in max_age_proxies:
max_age_cutoff = datetime.datetime.now(datetime.UTC) - datetime.timedelta(
days=1, seconds=86400 * random.random()) # nosec: B311
@ -232,6 +234,9 @@ class ProxyMetaAutomation(BaseAutomation):
"""
pools = Pool.query.all()
for pool in pools:
if pool.id == -1:
continue # Skip hotspare pool
logging.debug("Missing proxy check for %s", pool.pool_name)
for group in pool.groups:
for origin in group.origins:
if origin.destroyed is not None:
@ -240,6 +245,7 @@ class ProxyMetaAutomation(BaseAutomation):
x for x in origin.proxies
if x.pool_id == pool.id and x.deprecated is None and x.destroyed is None
]
logging.debug("Proxies for group %s: %s", group.group_name, proxies)
if not proxies:
logging.debug("Creating new proxy for %s in pool %s", origin, pool)
if not promote_hot_spare_proxy(pool.id, origin):