adjust worker for mysql, add more stable background process #9
@@ -1,31 +1,37 @@
|
|||||||
import os
|
import os
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from misc import currDay
|
from misc import currDay, datetime, time, timedelta
|
||||||
from db import db, Period, Event
|
from db import db, Period, Event
|
||||||
from create_events import createEvents
|
from create_events import createEvents
|
||||||
from apscheduler.schedulers.background import BlockingScheduler
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from apscheduler.triggers.cron import CronTrigger
|
|
||||||
|
|
||||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
|
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] =\
|
app.config['SQLALCHEMY_DATABASE_URI'] =\
|
||||||
'sqlite:///' + os.path.join(basedir, os.environ['SQLITE_DB'])
|
'mysql://' + os.environ['MYSQL_USER'] + \
|
||||||
|
':' + os.environ['MYSQL_PASSWORD'] + \
|
||||||
|
'@' + os.environ['MYSQL_HOST'] + \
|
||||||
|
':' + os.environ['MYSQL_PORT'] + \
|
||||||
|
'/' + os.environ['MYSQL_DB']
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
# declare BlockingScheduler
|
# Calculate the next time the function should run (at the :59 mark of the next hour)
|
||||||
sched = BlockingScheduler()
|
now = datetime.now()
|
||||||
|
next_hour = now.replace(hour=now.hour + 1, minute=0, second=0, microsecond=0)
|
||||||
|
next_run = next_hour - timedelta(minutes=1)
|
||||||
|
|
||||||
# run createEvents upon app launch
|
# Create scheduler and add job to call createEvents at the specified time
|
||||||
|
scheduler = BackgroundScheduler()
|
||||||
|
scheduler.add_job(createEvents, 'date', run_date=next_run, args=[db, currDay, Period, Event])
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
|
# Call createEvents on initial launch of the script
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
createEvents(db, currDay, Period, Event)
|
createEvents(db, currDay, Period, Event)
|
||||||
|
|
||||||
def eventsTask():
|
# Keep the program running indefinitely
|
||||||
with app.app_context():
|
while True:
|
||||||
createEvents(db, currDay, Period, Event)
|
time.sleep(60)
|
||||||
sched.add_job(eventsTask, CronTrigger.from_crontab('00 * * * *'))
|
|
||||||
print("Background worker started")
|
|
||||||
sched.start()
|
|
||||||
|
|
@@ -17,6 +17,23 @@ services:
|
|||||||
- PYTHONUNBUFFERED=1
|
- PYTHONUNBUFFERED=1
|
||||||
ports:
|
ports:
|
||||||
- 127.0.0.1:80:80
|
- 127.0.0.1:80:80
|
||||||
|
worker:
|
||||||
|
image: container-registry.infra.dubyatp.xyz/bellscheduler/app:latest-testing
|
||||||
|
restart: always
|
||||||
|
entrypoint: python3
|
||||||
|
command: "-m worker"
|
||||||
|
environment:
|
||||||
|
- MYSQL_USER=root
|
||||||
|
- MYSQL_PASSWORD=notasecuresecretkeyonlyuseforlocaldevelopment
|
||||||
|
- MYSQL_HOST=db
|
||||||
|
- MYSQL_PORT=3306
|
||||||
|
- MYSQL_DB=bellscheduler
|
||||||
|
- SECRET_KEY=notasecuresecretkeyonlyuseforlocaldevelopment
|
||||||
|
- NODE_NAME=local
|
||||||
|
- POD_NAME=local
|
||||||
|
- FLASK_ENV=development
|
||||||
|
- FLASK_DEBUG=1
|
||||||
|
- PYTHONUNBUFFERED=1
|
||||||
db:
|
db:
|
||||||
image: mariadb:10.7.8-focal
|
image: mariadb:10.7.8-focal
|
||||||
restart: always
|
restart: always
|
||||||
|
Reference in New Issue
Block a user