Commit 3b53dfc7 authored by Administrator's avatar Administrator
Browse files

Use uwsgi timers instead of apscheduler for cron-like tasks

parent 38e014df
......@@ -89,9 +89,6 @@ class ProtocolType(db.Model):
or (self.private_group != "" and self.private_group in user.groups))))
def has_private_view_right(self, user):
print(user, self.private_group)
if user is not None:
print(user.groups)
return (user is not None and self.private_group != "" and self.private_group in user.groups)
def has_modify_right(self, user):
......
......@@ -29,6 +29,7 @@ MarkupSafe==0.23
nose==1.3.7
packaging==16.8
pathtools==0.1.2
pkg-resources==0.0.0
psycopg2==2.6.2
Pygments==2.2.0
pyldap==2.4.28
......@@ -46,6 +47,7 @@ requests==2.13.0
six==1.10.0
SQLAlchemy==1.1.5
tzlocal==1.3
uwsgidecorators==1.1.0
vine==1.1.3
vobject==0.9.4.1
watchdog==0.8.3
......
......@@ -9,9 +9,9 @@ from flask_migrate import Migrate, MigrateCommand
#from flask_socketio import SocketIO
from celery import Celery
from sqlalchemy import or_, and_
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
#from apscheduler.schedulers.background import BackgroundScheduler
#from apscheduler.triggers.cron import CronTrigger
#from apscheduler.triggers.interval import IntervalTrigger
import atexit
from io import StringIO, BytesIO
import os
......@@ -45,16 +45,16 @@ celery = make_celery(app, config)
# return socketio
#socketio = make_socketio(app, config)
def make_scheduler(app, config, function):
scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(
func=function,
trigger=CronTrigger(hour='*', minute=30),
id="scheduler",
name="Do an action regularly",
replace_existing=True)
atexit.register(scheduler.shutdown)
#def make_scheduler(app, config, function):
# scheduler = BackgroundScheduler()
# scheduler.start()
# scheduler.add_job(
# func=function,
# trigger=CronTrigger(hour='*', minute=23),
# id="scheduler",
# name="Do an action regularly",
# replace_existing=True)
# atexit.register(scheduler.shutdown)
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
......@@ -1226,25 +1226,33 @@ def logout():
flash("You are not logged in.", "alert-error")
return redirect(url_for(".index"))
def check_and_send_reminders():
if not config.MAIL_ACTIVE:
return
with app.app_context():
current_time = datetime.now()
current_day = current_time.date()
print("regular action for reminders")
for protocol in Protocol.query.filter(Protocol.done == False).all():
day_difference = (protocol.date - current_day).days
usual_time = protocol.protocoltype.usual_time
protocol_time = datetime(1, 1, 1, usual_time.hour, usual_time.minute)
hour_difference = (protocol_time - current_time).seconds // 3600
print("diff: {} days, {} hours".format(day_difference, hour_difference))
for reminder in protocol.protocoltype.reminders:
print(reminder)
if day_difference == reminder.days_before and hour_difference == 0:
print("reminder matching, sending")
tasks.send_reminder(reminder, protocol)
def make_scheduler():
from uwsgidecorators import timer as uwsgitimer, signal as uwsgisignal, cron as uwsgicron
@uwsgicron(30, -1, -1, -1, -1, target="mule")
def uwsgi_timer(signum):
if signum == 0:
check_and_send_reminders()
def check_and_send_reminders():
print("check and send reminders")
if not config.MAIL_ACTIVE:
return
with app.app_context():
current_time = datetime.now()
current_day = current_time.date()
print("regular action for reminders")
for protocol in Protocol.query.filter(Protocol.done == False).all():
day_difference = (protocol.date - current_day).days
usual_time = protocol.protocoltype.usual_time
protocol_time = datetime(1, 1, 1, usual_time.hour, usual_time.minute)
hour_difference = (protocol_time - current_time).seconds // 3600
print("diff: {} days, {} hours".format(day_difference, hour_difference))
for reminder in protocol.protocoltype.reminders:
print(reminder)
if day_difference == reminder.days_before and hour_difference == 0:
print("reminder matching, sending")
tasks.send_reminder(reminder, protocol)
if __name__ == "__main__":
make_scheduler(app, config, check_and_send_reminders)
make_scheduler()
manager.run()
......@@ -74,7 +74,7 @@ def date_filter_long(date):
def date_filter_short(date):
return date.strftime("%d.%m.%Y")
def time_filter(time):
return time.strftime("%H:%M")
return time.strftime("%H:%M Uhr")
def needs_date_test(todostate):
return todostate.needs_date()
......
......@@ -78,7 +78,6 @@ class ProtocolsTable(Table):
result.append("Fertig" if protocol.is_done() else "Geplant")
elif protocol in self.search_results:
result.append(Markup(self.search_results[protocol]))
print(user, protocol.protocoltype.has_private_view_right(user))
if check_login():
if user is not None and protocol.protocoltype.has_private_view_right(user):
result.append(Table.link(url_for("show_type", type_id=protocol.protocoltype.id), protocol.protocoltype.short_name))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment