poprawki w zadaniach zaplanowanych

This commit is contained in:
Mateusz Gruszczyński 2025-03-04 14:00:31 +01:00
parent 1f16e5d130
commit 982a5049d7

29
app.py
View File

@ -7,6 +7,7 @@ from datetime import datetime, timezone, timedelta
from io import StringIO
import socket
import ipaddress
import pytz
from werkzeug.serving import WSGIRequestHandler
WSGIRequestHandler.server_version = ""
@ -64,6 +65,7 @@ class UserSettings(db.Model):
auto_deploy_enabled = db.Column(db.Boolean, default=False)
deploy_interval = db.Column(db.Integer, default=60) # interwał wdrożeń (minuty)
backup_interval = db.Column(db.Integer, default=60) # interwał backupów (minuty)
auto_backup_enabled = db.Column(db.Boolean, default=False)
last_deploy_time = db.Column(db.DateTime, nullable=True)
regex_deploy_enabled = db.Column(db.Boolean, default=True)
backup_retention_days = db.Column(db.Integer, default=0)
@ -174,22 +176,35 @@ def automated_backup_for_host(host):
def automated_backups():
with app.app_context():
logger.debug("Rozpoczynam funkcję automated_backups")
hosts = Host.query.all()
now = datetime.now(timezone.utc)
for host in hosts:
settings = UserSettings.query.filter_by(user_id=host.user_id).first()
backup_interval = settings.backup_interval if settings and settings.backup_interval else 60
last_backup = Backup.query.filter_by(user_id=host.user_id, host_id=host.id).order_by(Backup.created_at.desc()).first()
if not settings or not settings.auto_backup_enabled:
logger.debug(f"Pomijam host {host.hostname} - auto_backup nie włączone")
continue
backup_interval = settings.backup_interval if settings.backup_interval else 60
logger.debug(f"Backup interval dla hosta {host.hostname}: {backup_interval} minut")
last_backup = Backup.query.filter_by(user_id=host.user_id, host_id=host.id)\
.order_by(Backup.created_at.desc()).first()
if last_backup:
last_backup_time = last_backup.created_at
if last_backup_time.tzinfo is None:
# Zakładamy, że zapisany czas jest już w UTC
last_backup_time = last_backup_time.replace(tzinfo=timezone.utc)
diff = (now - last_backup_time).total_seconds()
logger.debug(f"Różnica czasu dla hosta {host.hostname}: {diff} sekund")
else:
last_backup_time = None
logger.debug(f"Brak poprzedniego backupu dla hosta {host.hostname}")
if (last_backup_time is None) or ((now - last_backup_time).total_seconds() >= backup_interval * 60):
logger.debug(f"Wykonuję backup dla hosta {host.hostname}")
automated_backup_for_host(host)
db.session.commit()
db.session.expire_all()
else:
logger.debug(f"Backup dla hosta {host.hostname} nie jest jeszcze potrzebny")
def wrap_content_with_comments(content):
@ -825,11 +840,12 @@ def settings():
auto_deploy = request.form.get('auto_deploy')
deploy_interval = request.form.get('deploy_interval')
backup_interval = request.form.get('backup_interval')
auto_backup = request.form.get('auto_backup')
enable_regex_entries = request.form.get('enable_regex_entries')
retention_val = request.form.get('backup_retention_days', '0')
user_settings.auto_deploy_enabled = bool(auto_deploy)
user_settings.auto_backup_enabled = bool(auto_backup)
try:
user_settings.deploy_interval = int(deploy_interval)
except ValueError:
@ -839,7 +855,6 @@ def settings():
except ValueError:
user_settings.backup_interval = 60
user_settings.regex_deploy_enabled = bool(enable_regex_entries)
try:
user_settings.backup_retention_days = int(retention_val)
except ValueError:
@ -851,6 +866,7 @@ def settings():
return render_template('settings.html', settings=user_settings)
@app.route('/delete-backup/<int:backup_id>', methods=['POST'])
def delete_backup(backup_id):
if 'user_id' not in session:
@ -1133,7 +1149,6 @@ scheduler.add_job(func=scheduled_deployments, trigger="interval", minutes=1, nex
scheduler.add_job(func=automated_backups, trigger="interval", minutes=1, next_run_time=datetime.now())
scheduler.add_job(func=cleanup_old_backups, trigger="interval", hours=24, next_run_time=datetime.now())
if __name__ == '__main__':
with app.app_context():
db.create_all()