JobQueue: run_once Instability Issue in python-telegram-bot: Seeking Solutions

79 views Asked by At

In my bot, the task is set to daily reading of reminders for employees and sending them throughout the day at a specified time. To update the task list daily, I use run_daily, which runs every day at 00:05 local time.

For reminders, I use run_once with the conversion of local time to the UTC time zone (any other method does not work, only this one).

The bot works fine for the first few days, but then problems with jobs start to arise: EITHER reminders are sent sporadically and not to everyone. Some receive them, some do not. OR run_daily itself does not trigger.

I have enabled logs, and the last time there was a 'Run time of job' error for three jobs.

At the same time, there is a very similar bot of version 15 that works without interruptions, but rolling back to version 15 is not possible.

Has anyone experienced similar issues, and how can this be resolved/fixed?

My code for run_once

active_jobs = context.job_queue.jobs()
active_jobs = [job.schedule_removal() for job in active_jobs if job.name != 'Auto generate']  # Delete active jobs before to add new

# Create task for run_once
notif = NotifPlant(**notification)

date_time = datetime.datetime(date_time_now.year, date_time_now.month, date_time_now.day, notif.hour, notif.min)
date_time = time_to_utc(date_time, configs.TIME_ZONE_STR)
context.job_queue.run_once(
            callback=send_notification,
            when=date_time,
            data=notif,  # dict with data
            name=str(notif.id_tel)
        )

My code for run_daily

def auto_generate_notification(application):
    func = get_notification

    time_to_call_func = time(
        hour=0, minute=5, tzinfo=pytz.timezone(configs.TIME_ZONE_STR)
    )
    application.job_queue.run_daily(callback=func, time=time_to_call_func, name='Auto generate')
    logging.info(AUTO GENERATE — {datetime.datetime.now(tz=pytz.timezone(configs.TIME_ZONE_STR))}')

I switched from run_once to run_daily and back, but it didn't work. I also assigned names to jobs and added extra logging, in addition to Sentry, to track job queue additions. However, this did not provide significant information.

My logs:

2023-11-21 10:01:02,982 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot6281555078:AAEBaCiRDjn-WjQAhj5VE-dEAkB_WNA-qTg/getUpdates "HTTP/1.1 200 OK"
2023-11-21 10:01:20,132 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
2023-11-21 10:01:20,134 - root - INFO - АВТООБНОВЛЕНИЕ — 2023-11-21 17:01:20.133789+07:00
2023-11-21 10:01:20,394 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot6281555078:AAEBaCiRDjn-WjQAhj5VE-dEAkB_WNA-qTg/getMe "HTTP/1.1 200 OK"
2023-11-21 10:01:20,442 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot6281555078:AAEBaCiRDjn-WjQAhj5VE-dEAkB_WNA-qTg/deleteWebhook "HTTP/1.1 200 OK"
2023-11-21 10:01:20,447 - apscheduler.scheduler - INFO - Added job "Auto generate" to job store "default"
2023-11-21 10:01:20,447 - apscheduler.scheduler - INFO - Scheduler started
2023-11-21 10:01:20,447 - telegram.ext.Application - INFO - Application started
2023-11-21 10:01:20,580 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot6281555078:AAEBaCiRDjn-WjQAhj5VE-dEAkB_WNA-qTg/getUpdates "HTTP/1.1 200 OK"
2023-11-21 10:01:25,926 - apscheduler.scheduler - INFO - Added job "189198380" to job store "default"
2023-11-21 10:01:25,928 - apscheduler.scheduler - INFO - Added job "434001077" to job store "default"
2023-11-21 10:01:25,928 - apscheduler.scheduler - INFO - Added job "368856606" to job store "default"
2023-11-21 10:01:25,929 - apscheduler.scheduler - INFO - Added job "1705470860" to job store "default"
2023-11-21 10:01:25,929 - apscheduler.scheduler - INFO - Added job "476787052" to job store "default"
2023-11-21 10:01:25,930 - apscheduler.scheduler - INFO - Added job "1387994997" to job store "default"
2023-11-21 10:01:25,930 - apscheduler.scheduler - INFO - Added job "368856606" to job store "default"
2023-11-21 10:01:25,931 - apscheduler.scheduler - INFO - Added job "434001077" to job store "default"
2023-11-21 10:01:25,934 - apscheduler.scheduler - INFO - Removed job 5aa7fc9dab4941668ba29dae1e2540a7
2023-11-21 10:01:25,934 - apscheduler.scheduler - INFO - Removed job a93566ed31c44a35b577c1b8f8f87bfb
2023-11-21 10:01:25,935 - apscheduler.scheduler - INFO - Removed job 03ffdb9d0f364602813f54dfa12af8ec
2023-11-21 10:01:25,937 - apscheduler.executors.default - WARNING - Run time of job "368856606 (trigger: date[2023-11-21 05:00:00 UTC], next run at: 2023-11-21 05:00:00 UTC)" was missed by 5:01:25.937786
2023-11-21 10:01:25,938 - apscheduler.executors.default - WARNING - Run time of job "434001077 (trigger: date[2023-11-21 05:00:00 UTC], next run at: 2023-11-21 05:00:00 UTC)" was missed by 5:01:25.938192
2023-11-21 10:01:25,938 - apscheduler.executors.default - WARNING - Run time of job "368856606 (trigger: date[2023-11-21 09:45:00 UTC], next run at: 2023-11-21 09:45:00 UTC)" was missed by 0:16:25.938356
2023-11-21 10:01:26,168 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot6281555078:AAEBaCiRDjn-WjQAhj5VE-dEAkB_WNA-qTg/sendMessage "HTTP/1.1 200 OK"

0

There are 0 answers