-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·36 lines (33 loc) · 1.3 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
import os
import atexit
from app import create_app, db
from app.models import User
from app.api_v1.stat import populate_stat_table, update_stat_table
if __name__ == '__main__':
app = create_app(os.environ.get('FLASK_ENV', 'development'))
with app.app_context():
# TODO: this is ONLY for DEV, it'd should be removed in PROD.
db.drop_all()
db.create_all()
# populate statistics table
populate_stat_table()
# create test user if user doesn't exit
if User.query.get(1) is None:
user = User(username='demo')
user.set_password('demo')
db.session.add(user)
db.session.commit()
# TODO: some cleanup needed, it's a bit ugly
# this for the scheduler, in principle, this should work
from apscheduler.schedulers.background import BackgroundScheduler
# initialize scheduler
scheduler = BackgroundScheduler()
# add job - hack to pass app.context
scheduler.add_job(func=update_stat_table, trigger='interval', args=[app], hours=5, id='update_statistics_table')
# start scheduler
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
# if you reach here, it means the app should start:-)
app.run()