Fix scheduled delete
This commit is contained in:
parent
72d7a2196f
commit
c2762b37f9
2 changed files with 16 additions and 31 deletions
|
|
@ -11,7 +11,6 @@ from flask_session import Session
|
||||||
import redis
|
import redis
|
||||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||||
from argon2 import PasswordHasher
|
from argon2 import PasswordHasher
|
||||||
from flask_apscheduler import APScheduler
|
|
||||||
|
|
||||||
# Load environment variables from .env file
|
# Load environment variables from .env file
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
@ -111,8 +110,8 @@ limiter = Limiter(get_remote_address, app=app)
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
# Initialize scheduler
|
# Initialize scheduler
|
||||||
scheduler = APScheduler()
|
from . import tasks
|
||||||
scheduler.init_app(app)
|
tasks.setup_tasks(app)
|
||||||
|
|
||||||
from .admin_routes import is_admin
|
from .admin_routes import is_admin
|
||||||
app.jinja_env.globals['is_admin'] = is_admin
|
app.jinja_env.globals['is_admin'] = is_admin
|
||||||
|
|
@ -137,25 +136,6 @@ register_error_handlers(app)
|
||||||
from . import routes
|
from . import routes
|
||||||
from . import admin_routes
|
from . import admin_routes
|
||||||
|
|
||||||
# Setup scheduler jobs
|
|
||||||
def setup_scheduler_jobs():
|
|
||||||
# Import the function from tasks.py
|
|
||||||
from .tasks import check_and_delete_expired_inquiries
|
|
||||||
|
|
||||||
# Schedule the job to run every 6 hours
|
|
||||||
scheduler.add_job(
|
|
||||||
id='check_and_delete_expired_inquiries',
|
|
||||||
func=check_and_delete_expired_inquiries,
|
|
||||||
trigger='interval',
|
|
||||||
hours=6
|
|
||||||
)
|
|
||||||
|
|
||||||
# Start the scheduler
|
|
||||||
scheduler.start()
|
|
||||||
|
|
||||||
# Start the scheduler when the app is initialized
|
|
||||||
setup_scheduler_jobs()
|
|
||||||
|
|
||||||
# Flask CLI command to run migrations
|
# Flask CLI command to run migrations
|
||||||
@app.cli.command("run-migrations")
|
@app.cli.command("run-migrations")
|
||||||
def run_migrations_command():
|
def run_migrations_command():
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,19 @@
|
||||||
from flask import current_app
|
from flask import Flask, current_app
|
||||||
|
from flask_apscheduler import APScheduler
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import Inquiry
|
from .models import Inquiry
|
||||||
|
|
||||||
def check_and_delete_expired_inquiries():
|
scheduler = APScheduler()
|
||||||
"""Check and delete inquiries that have been closed for more than the configured number of hours"""
|
|
||||||
# Get app context from current_app
|
|
||||||
app = current_app._get_current_object()
|
|
||||||
|
|
||||||
with app.app_context():
|
def setup_tasks(app: Flask):
|
||||||
app.logger.info(f"Running scheduled task: check_and_delete_expired_inquiries")
|
scheduler.init_app(app)
|
||||||
|
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
|
@scheduler.task('interval', hours=6)
|
||||||
|
def _check_and_delete_expired_inquiries():
|
||||||
|
with scheduler.app.app_context():
|
||||||
expired_inquiries = Inquiry.get_expired_inquiries()
|
expired_inquiries = Inquiry.get_expired_inquiries()
|
||||||
deleted_count = 0
|
deleted_count = 0
|
||||||
|
|
||||||
|
|
@ -19,4 +24,4 @@ def check_and_delete_expired_inquiries():
|
||||||
|
|
||||||
if deleted_count > 0:
|
if deleted_count > 0:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
app.logger.info(f"Automatically deleted {deleted_count} expired closed inquiries")
|
scheduler.app.logger.info(f"Automatically deleted {deleted_count} expired (long closed) inquiries")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue