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

时间:2018-12-20 07:13:26

标签: python flask redis celery flask-sqlalchemy

我正在使用烧瓶创建一个webapp,并使用celery定期发送邮件。 问题是,只要数据库中有新条目,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表中添加内容时,芹菜看不到新条目。重新启动芹菜工人后,它会看到它。

我正在使用以下命令运行beat和worker:

celery -A periods beat --loglevel=INFO

celery -A periods worker --loglevel=INFO --concurrency=2

0 个答案:

没有答案