garbage-collection #31
16
app/cleanup_events.py
Normal file
16
app/cleanup_events.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
def cleanupEvents(db, Event):
|
||||||
|
# calculate the date one month ago
|
||||||
|
one_month_ago = datetime.now() - timedelta(days=30)
|
||||||
|
|
||||||
|
# get events older than one month
|
||||||
|
old_events = db.session.query(Event).filter(
|
||||||
|
db.func.STR_TO_DATE(Event.scheduled_date, '%m-%d-%Y') < one_month_ago.date()
|
||||||
|
).all()
|
||||||
|
|
||||||
|
# delete old events
|
||||||
|
for event in old_events:
|
||||||
|
db.session.delete(event)
|
||||||
|
|
||||||
|
db.session.commit()
|
@@ -1,6 +1,7 @@
|
|||||||
from celery import Celery
|
from celery import Celery
|
||||||
from celery.schedules import crontab
|
from celery.schedules import crontab
|
||||||
from create_events import createEvents
|
from create_events import createEvents
|
||||||
|
from cleanup_events import cleanupEvents
|
||||||
import os
|
import os
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from misc import currDay, datetime, time, timedelta
|
from misc import currDay, datetime, time, timedelta
|
||||||
@@ -32,11 +33,20 @@ def runCreateEvents():
|
|||||||
with app.app_context():
|
with app.app_context():
|
||||||
createEvents(db, currDay, Period, Event)
|
createEvents(db, currDay, Period, Event)
|
||||||
|
|
||||||
|
def runCleanupEvents():
|
||||||
|
with app.app_context():
|
||||||
|
cleanupEvents(db, Event)
|
||||||
|
|
||||||
# Scheduled tasks
|
# Scheduled tasks
|
||||||
celerymsg.conf.beat_schedule = {
|
celerymsg.conf.beat_schedule = {
|
||||||
'hourly-createevents': {
|
'hourly-createevents': {
|
||||||
'task': 'worker.runCreateEvents',
|
'task': 'worker.runCreateEvents',
|
||||||
# Run hourly
|
# Run hourly
|
||||||
'schedule': crontab(hour="*", minute="59"),
|
'schedule': crontab(hour="*", minute="59"),
|
||||||
|
},
|
||||||
|
'monthly-cleanupevents': {
|
||||||
|
'task': 'worker.runCleanupEvents',
|
||||||
|
# Run monthly
|
||||||
|
'schedule': crontab(day_of_month="29")
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user