I am learning celery and facing some problem. I am trying to set up celery task, which is called by user from telegram bot. I am trying to set up task_soft_time_limit, but it doesn’t work.
celery_app.py:
from celery import Celery
app = Celery(
'reminder',
broker='redis://localhost:6379/1',
)
app.conf.update(
task_soft_time_limit=3,
worker_max_memory_per_child=300 * 1024,
)
app.conf.beat_schedule = {
'check-reminders-every-minute': {
'task': 'tasks.schedule_reminders',
'schedule': 30.0,
},
}
tasks.py:
import os
import telebot
from dotenv import load_dotenv
from celery_app import app
from celery.exceptions import SoftTimeLimitExceeded
load_dotenv()
CHAT_ID = os.getenv('CHAT_ID')
bot_token = os.getenv('BOT_API')
bot = telebot.TeleBot(bot_token)
@app.task
def time_task():
try:
import time
time.sleep(10)
bot.send_message(CHAT_ID, 'task is finished.')
except SoftTimeLimitExceeded:
bot.send_message(CHAT_ID, 'something went wrong.')
telebot.py:
from tasks import time_task
import os
import telebot
from dotenv import load_dotenv
from celery_app import app
load_dotenv()
bot_token = os.getenv('BOT_API')
bot = telebot.TeleBot(bot_token)
@bot.message_handler(commands=['test'])
def test(message):
time_task.apply_async()
bot.polling(non_stop=True)
I call the Celery worker with celery -A tasks worker --loglevel=info -P eventlet
command and call task from telegram bot, but the tusk waits 10 second and sends message, whereas i should recive an error message.
I tried to set up limits for the current task,
@app.task(max_retries=3, soft_time_limit=1)
def time_task():
try:
import time
time.sleep(10)
bot.send_message(CHAT_ID, 'task is finished.')
except Exception:
bot.send_message(CHAT_ID, 'something went wrong.')
but it didn’t work either.
Maksim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.