changed scheduling to apscheduler

This commit is contained in:
iou1name 2018-06-12 08:51:54 -04:00
parent 671e7a107d
commit 992587fac7
2 changed files with 19 additions and 23 deletions

View File

@ -6,6 +6,6 @@ Note: I switched to Gunicorn at some point because Bjoern was somehow annoying.
Dependencies: Dependencies:
```passlib argon2_cffi flask gunicorn flask-paranoid``` ```passlib argon2_cffi flask gunicorn flask-paranoid apscheduler```
This application makes use of the `secrets` module (a cryptographically strong version of `random`) from the standard library, which is only available in Python 3.6+. If you really can't be bothered use the latest version of python3, or just don't want cryptographically strong random character filenames/prefixes for some reason, you can directly replace all instances of `secrets` with `random`. This application makes use of the `secrets` module (a cryptographically strong version of `random`) from the standard library, which is only available in Python 3.6+. If you really can't be bothered use the latest version of python3, or just don't want cryptographically strong random character filenames/prefixes for some reason, you can directly replace all instances of `secrets` with `random`.

View File

@ -17,6 +17,7 @@ from flask import Flask, session, request, abort, redirect, url_for, g, \
render_template render_template
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from flask_paranoid import Paranoid from flask_paranoid import Paranoid
from apscheduler.schedulers.background import BackgroundScheduler
class ReverseProxied(object): class ReverseProxied(object):
""" """
@ -53,22 +54,6 @@ class ReverseProxied(object):
return self.app(environ, start_response) return self.app(environ, start_response)
class CronThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stop = threading.Event()
def run(self):
while not self.stop.is_set():
records = db_execute(
"SELECT filename, delete_date FROM uploads WHERE delete_date"
).fetchall()
for filename, delete_date in records:
if time.time() >= delete_date:
delete_file(filename)
self.stop.wait(60)
app = Flask(__name__) app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app) app.wsgi_app = ReverseProxied(app.wsgi_app)
app.config['MAX_CONTENT_LENGTH'] = 128 * 1024 * 1024 app.config['MAX_CONTENT_LENGTH'] = 128 * 1024 * 1024
@ -80,6 +65,9 @@ app.config["DB_LOCK"] = threading.Lock()
paranoid = Paranoid(app) paranoid = Paranoid(app)
paranoid.redirect_view = 'login' paranoid.redirect_view = 'login'
scheduler = BackgroundScheduler(timezone="America/New_York")
scheduler.start()
def db_execute(*args, **kwargs): def db_execute(*args, **kwargs):
""" """
@ -94,6 +82,19 @@ def db_execute(*args, **kwargs):
return res return res
@scheduler.scheduled_job("interval", minutes=1)
def delete_this():
"""
Removes files that are past the expiration date.
"""
records = db_execute(
"SELECT filename, delete_date FROM uploads WHERE delete_date"
).fetchall()
for filename, delete_date in records:
if time.time() >= delete_date:
delete_file(filename)
def init(): def init():
""" """
Initializes the application. Initializes the application.
@ -127,11 +128,6 @@ def init():
"uploaded_date INTEGER DEFAULT (STRFTIME('%s', 'now'))," "uploaded_date INTEGER DEFAULT (STRFTIME('%s', 'now')),"
"delete_date INTEGER)") "delete_date INTEGER)")
# init cron thread
t = CronThread()
t.start()
app.config["CRON_THREAD"] = t
def add_user(username, password, admin="FALSE"): def add_user(username, password, admin="FALSE"):
""" """
@ -429,7 +425,7 @@ def get_rand_chars(n):
init() init()
atexit.register(app.config["CRON_THREAD"].stop.set) atexit.register(scheduler.shutdown)
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
if len(sys.argv) > 1: if len(sys.argv) > 1: