You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.4 KiB
46 lines
1.4 KiB
import time |
|
from apscheduler.schedulers.background import BackgroundScheduler |
|
import atexit |
|
from datetime import datetime |
|
|
|
from app import app, db |
|
from app.models import Url |
|
|
|
def remove_dead_urls(): |
|
for url in Url.query.all(): |
|
check_url(url) |
|
|
|
def check_url(url): |
|
if url.death is not None: |
|
if datetime.now() > url.death : |
|
app.logger.info(f"Removed hash '{url.hash}' because its retention time has passed.") |
|
db.session.delete(url) |
|
db.session.commit() |
|
return "Timeout" |
|
if url.view_counter is not None: |
|
if url.view_counter <= 0: |
|
app.logger.info(f"Removed hash '{url.hash}' because its viewcouner has run out.") |
|
db.session.delete(url) |
|
db.session.commit() |
|
return "Counter" |
|
return None |
|
|
|
scheduler = None |
|
|
|
def start_scheduler(): |
|
global scheduler |
|
if scheduler is not None: |
|
app.logger.info("Database_cleaner scheduler was already started, not launching a second instane.") |
|
return |
|
scheduler = BackgroundScheduler() |
|
scheduler.add_job(func=remove_dead_urls, trigger="interval", hours=1) |
|
scheduler.start() |
|
app.logger.info("Database_cleaner scheduler was started.") |
|
|
|
# Shut down the scheduler when exiting the app |
|
atexit.register(shutdown_scheduler) |
|
|
|
def shutdown_scheduler(): |
|
scheduler.shutdown() |
|
app.logger.info("Database_cleaner scheduler was shut down.") |
|
|