2017-11-19 6 views
0

DBにジョブを追加しようとしていて、スクリプトが再び実行されたときにジョブをロードしようとしています。次のコードはdbに追加しますが、スクリプトを再起動すると、ジョブはロードされますが、実行されます。 仕事のインポートは正しいと思われますが、なぜそれがWildeを実行しているのかわかりません。ロードされたときにDBに保存されたジョブが、なぜ永遠に実行されるのですか?

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() 

答えて

0

これは私が見つけた解決策です。我々は、DBからのロードされた時間をTEXTとして保存される日付の時間オブジェクトに変換する必要があります。

私が質問のために入力したコードでは、変換を完了させるために.time()を追加する必要がありました。

​​
関連する問題