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:
parent
b91e078e22
commit
1bc2960278
2 changed files with 13 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
|
import logging
|
||||||
import secrets
|
import secrets
|
||||||
from datetime import datetime
|
from datetime import datetime, UTC
|
||||||
|
|
||||||
from flask import render_template, url_for, flash, redirect, Response, Blueprint
|
from flask import render_template, url_for, flash, redirect, Response, Blueprint
|
||||||
from flask.typing import ResponseReturnValue
|
from flask.typing import ResponseReturnValue
|
||||||
|
@ -55,15 +56,16 @@ def pool_new() -> ResponseReturnValue:
|
||||||
pool.description = form.description.data
|
pool.description = form.description.data
|
||||||
pool.redirector_domain = form.redirector_domain.data if form.redirector_domain.data != "" else None
|
pool.redirector_domain = form.redirector_domain.data if form.redirector_domain.data != "" else None
|
||||||
pool.api_key = secrets.token_urlsafe(nbytes=32)
|
pool.api_key = secrets.token_urlsafe(nbytes=32)
|
||||||
pool.created = datetime.utcnow()
|
pool.added = datetime.now(UTC)
|
||||||
pool.updated = datetime.utcnow()
|
pool.updated = datetime.now(UTC)
|
||||||
try:
|
try:
|
||||||
db.session.add(pool)
|
db.session.add(pool)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash(f"Created new pool {pool.pool_name}.", "success")
|
flash(f"Created new pool {pool.pool_name}.", "success")
|
||||||
return redirect(url_for("portal.pool.pool_edit", pool_id=pool.id))
|
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")
|
flash("Failed to create new pool.", "danger")
|
||||||
|
logging.exception(exc)
|
||||||
return redirect(url_for("portal.pool.pool_list"))
|
return redirect(url_for("portal.pool.pool_list"))
|
||||||
return render_template("new.html.j2", section="pool", form=form)
|
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:
|
if form.api_key.data != pool.api_key:
|
||||||
pool.api_key = secrets.token_urlsafe(nbytes=32)
|
pool.api_key = secrets.token_urlsafe(nbytes=32)
|
||||||
form.api_key.data = pool.api_key
|
form.api_key.data = pool.api_key
|
||||||
pool.updated = datetime.utcnow()
|
pool.updated = datetime.now(UTC)
|
||||||
try:
|
try:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash("Saved changes to pool.", "success")
|
flash("Saved changes to pool.", "success")
|
||||||
|
|
|
@ -142,6 +142,7 @@ def auto_deprecate_proxies() -> None:
|
||||||
Proxy.deprecated.is_(None),
|
Proxy.deprecated.is_(None),
|
||||||
Origin.destroyed.is_not(None))
|
Origin.destroyed.is_not(None))
|
||||||
.all())
|
.all())
|
||||||
|
logging.debug("Origin destroyed: %s", origin_destroyed_proxies)
|
||||||
for proxy in origin_destroyed_proxies:
|
for proxy in origin_destroyed_proxies:
|
||||||
proxy.deprecate(reason="origin_destroyed")
|
proxy.deprecate(reason="origin_destroyed")
|
||||||
max_age_proxies = (db.session.query(Proxy)
|
max_age_proxies = (db.session.query(Proxy)
|
||||||
|
@ -152,6 +153,7 @@ def auto_deprecate_proxies() -> None:
|
||||||
Origin.assets,
|
Origin.assets,
|
||||||
Origin.auto_rotation)
|
Origin.auto_rotation)
|
||||||
.all())
|
.all())
|
||||||
|
logging.debug("Max age: %s", max_age_proxies)
|
||||||
for proxy in max_age_proxies:
|
for proxy in max_age_proxies:
|
||||||
max_age_cutoff = datetime.datetime.now(datetime.UTC) - datetime.timedelta(
|
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
|
||||||
|
@ -232,6 +234,9 @@ class ProxyMetaAutomation(BaseAutomation):
|
||||||
"""
|
"""
|
||||||
pools = Pool.query.all()
|
pools = Pool.query.all()
|
||||||
for pool in pools:
|
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 group in pool.groups:
|
||||||
for origin in group.origins:
|
for origin in group.origins:
|
||||||
if origin.destroyed is not None:
|
if origin.destroyed is not None:
|
||||||
|
@ -240,6 +245,7 @@ class ProxyMetaAutomation(BaseAutomation):
|
||||||
x for x in origin.proxies
|
x for x in origin.proxies
|
||||||
if x.pool_id == pool.id and x.deprecated is None and x.destroyed is None
|
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:
|
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)
|
||||||
if not promote_hot_spare_proxy(pool.id, origin):
|
if not promote_hot_spare_proxy(pool.id, origin):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue