rewrite worker to run on celery and redis
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from celery import Celery
|
||||
from celery.schedules import crontab
|
||||
from create_events import createEvents
|
||||
import os
|
||||
from flask import Flask
|
||||
from misc import currDay, datetime, time, timedelta
|
||||
from db import db, Period, Event
|
||||
from create_events import createEvents
|
||||
from apscheduler.schedulers.background import BlockingScheduler
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
@@ -18,18 +19,24 @@ app.config['SQLALCHEMY_DATABASE_URI'] =\
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db.init_app(app)
|
||||
|
||||
# Define function to run create_events with app context
|
||||
def run_create_events():
|
||||
redis_url = 'redis://' + \
|
||||
os.environ['REDIS_HOST'] + \
|
||||
':' + os.environ['REDIS_PORT'] + \
|
||||
'/' + os.environ['REDIS_DBNUM']
|
||||
|
||||
celerymsg = Celery('tasks', backend=redis_url, broker=redis_url)
|
||||
|
||||
# Task definitions
|
||||
@celerymsg.task
|
||||
def runCreateEvents():
|
||||
with app.app_context():
|
||||
createEvents(db, currDay, Period, Event)
|
||||
|
||||
# Call createEvents on initial launch of the script
|
||||
run_create_events()
|
||||
|
||||
|
||||
# Set up scheduler to run function at 59th minute of every hour
|
||||
scheduler = BlockingScheduler()
|
||||
scheduler.add_job(run_create_events, 'cron', minute=59)
|
||||
|
||||
# Start scheduler
|
||||
scheduler.start()
|
||||
# Scheduled tasks
|
||||
celerymsg.conf.beat_schedule = {
|
||||
'hourly-createevents': {
|
||||
'task': 'worker.runCreateEvents',
|
||||
# Run hourly
|
||||
'schedule': crontab(hour="*"),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user