Celery worker不从数据库中获取更新的值

2024-10-04 03:16:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用烧瓶和芹菜定期发送邮件来建立一个网络应用程序。 问题是,每当数据库中有一个新条目时,celery就看不到它并继续使用旧条目。我每次都得重新启动芹菜工人才能让它正常工作。Celery beat正在运行,我使用redis作为经纪人。在

芹菜相关功能:

@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(30.0, appointment_checkout, name='appointment_checkout')


@celery.task(name='app.Blueprints.periods.appointment_checkout')
def appointment_checkout():
    from app.Blueprints.api.routes import fetchAllAppointments, fetch_user_email, fetch_user_phone
    from app.Blueprints import db
    dt = datetime.now() + timedelta(minutes=10)
    #fa = Appointment.query.filter_by(date = dt.strftime("%Y-%m-%d"))
    fa = fetchAllAppointments()
    for i in fa:
        # send emails to clients and counsellors
        try:
            if(str(i.date.year) != dt.strftime("%Y") or str(i.date.month) != dt.strftime("%m") or str(i.date.day) != dt.strftime("%d")):
                continue
        except:
            continue
        if(i.reminderFlag == 1):
            continue
        if(int(dt.strftime("%H")) == int(i.time.hour) and int(dt.strftime("%M")) == int(i.time.minute)):
            client = fetch_user_email(i.user)
            counsellor = fetch_user_email(i.counsellor)
            client_phone = fetch_user_phone(i.user)
            counsellor_phone = fetch_user_phone(i.counsellor)
            i.reminderFlag = 1 
            db.session.add(i)
            db.session.commit()
            # client email
            subject = "appointment notification"
            msg = "<h1>Greetings</h1></p>This is to notify you that your appointment is about to begin soon.</p>"
            sendmail.delay(subject, msg, client)
            sendmail.delay(subject, msg, counsellor)
            sendmsg.delay(client_phone, msg)
            sendmsg.delay(counsellor_phone, msg)

当我在appointment表中添加内容时,celery看不到新条目。重启芹菜工人后,它看到了它。在

我使用以下命令运行beat and worker:

^{pr2}$

Tags: clientdateemaildtphonemsgfetchint