为什么加载时存储在数据库中的作业永远运行?

Why the job stored in DB when loaded runs for ever?

我正在尝试将作业添加到数据库,然后在脚本再次运行时加载它们。下面的代码将它添加到 db 但是当你重新启动脚本时它加载作业但永远运行它。 似乎作业导入是正确的,但我不明白为什么它是 运行 Wilde?

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler
import logging,sqlite3, datetime, json
from telegram import Update



# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)
def seturl(bot,job):
    bot.send_message(chat_id= job.context.message.chat_id, text=job.context.message.text)

def userinfo(bot,update,job_queue):
    interval = datetime.time(5)
    context = update
    db = sqlite3.connect('thedb.db')
    job = job_queue.run_daily(seturl, interval, context=context)

    with db as connection:
        c = connection.cursor()
        c.execute('INSERT INTO jobq(interval, context) VALUES (?, ?)', (str(interval), json.dumps(context.to_dict())))
    db.commit()
    db.close()

def error(bot, update, error):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, error)

def main():
    updater = Updater("TOKEN")
    dp = updater.dispatcher
    db = sqlite3.connect('thedb.db')
    c = db.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS jobq(interval INTEGER, context TEXT)")
    c.execute('SELECT * FROM jobq')
    results = c.fetchall()
    for row in results:
        dp.job_queue.run_daily(seturl, datetime.datetime.strptime(row[0],"%H:%M:%S"), context = Update.de_json(json.loads(row[1]),dp.bot))

    dp.add_handler(MessageHandler(Filters.text , userinfo,pass_job_queue=True))
    db.commit()
    db.close()

    dp.add_error_handler(error)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

这是我找到的解决方案。我们需要将加载的时间从数据库转换为日期时间对象,因为它存储为文本。

在我输入的问题代码中,我需要添加 .time() 才能完成转换。

dp.job_queue.run_daily(seturl, datetime.datetime.strptime(row[0],'%H:%M:%S').time(), context = Update.de_json(json.loads(row[1]),dp.bot))