Inaccurate celery task status

I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae
Search for a command to run...

I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae
No comments yet. Be the first to comment.
You Should Tell Yourself

A URL shortener is a proxy service that provides a mapping between the short and full representation of a URL. The short URL has the advantage of being small. The service can also provide useful analy

Listening to the Mel Robbins's podcast on regrets was a 'ear'-opener. One can have reqrets of action or inaction. 4 types of reqrets foundation - should've done the work boldness - should've taken t

We need to create a unique ID generator for our high-traffic web application, generating about 10K IDs/second. The IDs can't simply be monotonically increasing integers, which are good for data access
doing + telling

Getting the status of a celery task
task_result = AsyncResult(task_id, app=celery_app)
works correctly if you have a result backend configured and tasks are defined with @app.task(bind=True). The result would be STARTED; SUCCESS; FAILURE; RETRY; REVOKED.
When staring tasks, I’d always get a PENDING status regardless of the queried task_id. The PENDING state is celery's default state for any task whose status is unknown or has no history.
The workaround: write the task_id to Redis (Celery's result backend) on task start, then only report status for task_id's that exist there. Also enable track_started=True by default, long-running tasks in chains show as PENDING until completion, which is pointless for monitoring.
def trigger_chain(task_chain):
future = task_chain.apply_async()
celery_app.backend.client.set(f"{future.id}", "started", ex=86400)
def get_task_result(task_id: str):
try:
task_result = AsyncResult(task_id, app=celery_app)
if task_result.status == "PENDING":
# Access the backend to check if the task key exists
backend = celery_app.backend
task_key, backend_client = (
backend.get_key_for_task(task_id), gettattr(backend, "client", None
)
# Check if the key exists in Redis
if backend_client and
if not ( backend_client.exists(key) or backend_client.exists(task_id)):
# Task doesn't exist in the result backend
return jsonify(
{
"error": "Task not found",
"task_id": task_id,
"message": "No task with this ID exists in the result backend",
}
), 404
# Valid task
response = {
"task_id": task_id,
"status": task_result.status,
}
Happy hackin’