最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[记录]Flask中给活动到期添加提醒功能

Flask crifan 2634浏览 0评论

折腾:

[已解决]Flask中添加后台进程用于提醒到期时发送通知

期间,基本搞清楚了

Flask-RQ2,redis,rq了

接下来,就是去折腾,添加:

把本地的时间转换为GMT的时间

再去根据传入的提醒时间的设置,

计算出提醒时间是什么

然后加入对应的任务

期间遇到:

[未解决]Python中给已有的类库增加新功能

所以只能是暂时使用单独的方法,而不是扩展后的datetime的新方法了。

然后遇到:

[已解决]SQLAlchemy中如何得到db.session.commit后的新增的对象

然后,此处已经可以用代码:

实现发送通知之前的逻辑了:

from . import rq
@rq.job
def notifyEvent(eventToNotif, notifDatetimeGmt):
    gLog.debug(“eventToNotif=%s, notifDatetimeGmt=%s”, eventToNotif, notifDatetimeGmt)
@app.route(‘/creat_event’, methods=[‘GET’, ‘POST’])
@login_required
def creat_event():
        。。。
        gLog.debug(“try add background work to notify for event”)
        if notificationTimeEnum == NotificationTime.NotNotify:
            gLog.debug(“no set notify time for event=%s”, newEvent)
        else:
            timedeltaAfter = timedelta(seconds=0)
            gLog.debug(“timedeltaAfter=%s”, timedeltaAfter)
            if notificationTimeEnum == NotificationTime.WhenEventHappen:
                timedeltaAfter = timedelta(seconds=0)
            elif notificationTimeEnum == NotificationTime.FiveMinutesBefore:
                timedeltaAfter = timedelta(minutes=5)
            elif notificationTimeEnum == NotificationTime.FiftyMinutesBefore:
                timedeltaAfter = timedelta(minutes=15)
            elif notificationTimeEnum == NotificationTime.ThirtyMinutesBefore:
                timedeltaAfter = timedelta(minutes=30)
            elif notificationTimeEnum == NotificationTime.OneHourBefore:
                timedeltaAfter = timedelta(hours=1)
            elif notificationTimeEnum == NotificationTime.TwoHoursBefore:
                timedeltaAfter = timedelta(hours=2)
            elif notificationTimeEnum == NotificationTime.OneDayBefore:
                timedeltaAfter = timedelta(days=1)
            elif notificationTimeEnum == NotificationTime.TwoDaysBefore:
                timedeltaAfter = timedelta(days=2)
            elif notificationTimeEnum == NotificationTime.OneWeekBefore:
                timedeltaAfter = timedelta(weeks=1)
            gLog.debug(“timedeltaAfter=%s”, timedeltaAfter)
            gLog.debug(“startDate=%s”, startDate)
            notificationDatetime = startDate + timedeltaAfter
            gLog.debug(“notificationDatetime=%s”, notificationDatetime)
            gmtNotificationDatetime = crifanLib.convertLocalToGmt(notificationDatetime)
            gLog.debug(“gmtNotificationDatetime=%s”, gmtNotificationDatetime)
            # schedule job at specific notification time
            notificationJob = notifyEvent.schedule(
                gmtNotificationDatetime,
                eventToNotif=newEvent,
                notifDatetimeGmt=gmtNotificationDatetime)
            gLog.debug(“notificationJob=%s”, notificationJob)

然后,确保运行了redis-server,rq worker,rqscheduler:

nohup redis-server /etc/redis/redis.conf >> redis.log 2>&1 &
nohup rqscheduler -i 30 >rqscheduler.log 2>&1 &
nohup rq worker > rq_worker.log 2>&1 &
gunicorn -w 4 -b 127.0.0.1:8080 run:app &

在新建活动,设置了对应的提醒时间后,时间到了之后,通过查看对应的log,看出来,被调度执行了:

(SIPEvents) ➜  SIPEvents cat rq_worker.log
。。。
21:45:24 default: sipevents.views.notifyEvent(eventToNotif=<Event id=27 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’tongz’>, notifDatetimeGmt=datetime.datetime(2016, 9, 4, 13, 45)) (2cb7666c-8201-4fdd-babe-578e9cdc64bc)

<div–<——————————————————————————

DEBUG in __init__ [./sipevents/__init__.py:47]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [./sipevents/__init__.py:62]:
type(rq)=<class ‘flask_rq2.app.RQ’>, rq=<flask_rq2.app.RQ object at 0x7fa7bbfd1d90>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [./sipevents/views.py:368]:
eventToNotif=<Event id=27 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’tongz’>, notifDatetimeGmt=2016-09-04 13:45:00

<div–<——————————————————————————

21:45:24 default: Job OK (2cb7666c-8201-4fdd-babe-578e9cdc64bc)
21:45:24 Result is kept for 500 seconds
21:45:24 
21:45:24 *** Listening on default…
(SIPEvents) ➜  SIPEvents tail logs/sipevents.log 
[2016-09-04 21:43:42,203 DEBUG views.py:809 index] tomorrowEventList=[]
[2016-09-04 21:43:42,206 DEBUG views.py:813 index] futureEventList=[]
[2016-09-04 21:45:24,408 DEBUG __init__.py:47 <module>] db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>
[2016-09-04 21:45:24,411 DEBUG __init__.py:62 <module>] type(rq)=<class ‘flask_rq2.app.RQ’>, rq=<flask_rq2.app.RQ object at 0x7fa7bbfd1d90>
[2016-09-04 21:45:24,594 DEBUG views.py:368 notifyEvent] eventToNotif=<Event id=27 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’tongz’>, notifDatetimeGmt=2016-09-04 13:45:00

接下来,就是去Flask中,去发送微信的模板消息,实现到期提醒了。

[记录]去微信公众号管理后台添加到期提醒的相关模板

转载请注明:在路上 » [记录]Flask中给活动到期添加提醒功能

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.174 seconds, using 22.16MB memory