expiration dates work
This commit is contained in:
parent
a8c4ddd787
commit
ec4a034581
36
saddle.py
36
saddle.py
|
@ -6,6 +6,7 @@ import os
|
||||||
import time
|
import time
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
@ -180,11 +181,46 @@ def download_file(url, timeout=10, max_file_size=config.client_max_size):
|
||||||
return (fname, temp)
|
return (fname, temp)
|
||||||
|
|
||||||
|
|
||||||
|
async def cleaner(app):
|
||||||
|
"""Removes files marked for deletion."""
|
||||||
|
async with app['pool'].acquire() as conn:
|
||||||
|
expired = await conn.fetch(
|
||||||
|
"SELECT * FROM upload WHERE expiration_date < NOW()")
|
||||||
|
if not expired:
|
||||||
|
return
|
||||||
|
for record in expired:
|
||||||
|
os.remove(os.path.join(config.upload_dir, record['filename']))
|
||||||
|
await conn.executemany(
|
||||||
|
"DELETE FROM upload WHERE id = $1",
|
||||||
|
[(record['id'],) for record in expired])
|
||||||
|
|
||||||
|
|
||||||
|
async def cleaner_loop(app):
|
||||||
|
"""Loops cleaner() continuously until shutdown."""
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
await cleaner(app)
|
||||||
|
await asyncio.sleep(60)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
async def start_background_tasks(app):
|
||||||
|
app['cleaner'] = asyncio.create_task(cleaner_loop(app))
|
||||||
|
|
||||||
|
|
||||||
|
async def cleanup_background_tasks(app):
|
||||||
|
app['cleaner'].cancel()
|
||||||
|
await app['cleaner']
|
||||||
|
|
||||||
|
|
||||||
async def init_app():
|
async def init_app():
|
||||||
"""Initializes the application."""
|
"""Initializes the application."""
|
||||||
app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
|
app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
|
||||||
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
|
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
|
||||||
app['pool'] = await asyncpg.create_pool(**config.db)
|
app['pool'] = await asyncpg.create_pool(**config.db)
|
||||||
|
app.on_startup.append(start_background_tasks)
|
||||||
|
app.on_cleanup.append(cleanup_background_tasks)
|
||||||
|
|
||||||
async with app['pool'].acquire() as conn:
|
async with app['pool'].acquire() as conn:
|
||||||
with open('saddle.sql', 'r') as file:
|
with open('saddle.sql', 'r') as file:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user