Thursday, 9 June 2022

How to capture the celery warning(s) from logs?

I am using celery in conjunction with fastapi & the results are being logged in a file called celery.log via the command

celery worker --app=app.celery_worker.celery --loglevel=info --logfile=app/logs/celery.log

When the code is triggered, the logs are written in the celery.log file as follows:

[2022-05-20 11:38:35,148: INFO/MainProcess] Received task: kwept_calculation[bd80737a-92cd-4fea-8a68-c010d5ab3ed3]  
[2022-05-20 11:38:46,249: WARNING/ForkPoolWorker-7] System mit Neuinstallation(en) nicht betreibbar (z.B. wegen nicht deckbarer Wärmenachfrage)
[2022-05-20 11:38:53,401: WARNING/ForkPoolWorker-7] System ohne Neuinstallation(en) nicht betreibbar (z.B. wegen nicht deckbarer Wärmenachfrage)
[2022-05-20 11:38:53,402: ERROR/ForkPoolWorker-7] Task kwept_calculation[bd80737a-92cd-4fea-8a68-c010d5ab3ed3] raised unexpected: UnboundLocalError("local variable 'kpis_df' referenced before assignment")
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 412, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 704, in __protected_call__
    return self.run(*args, **kwargs)
  File "/kwept/app/celery_worker.py", line 77, in calculation
    created_at_ts
  File "/kwept/app/services/planning_calc.py", line 571, in plan_calc
    kpis_df.reset_index(drop=False, inplace=True)
UnboundLocalError: local variable 'kpis_df' referenced before assignment

To get the info on the task, I do

from celery.result import AsyncResult
task_result = AsyncResult(task_id) # in the above case bd80737a-92cd-4fea-8a68-c010d5ab3ed3
task_info = task_result.info

When I do that, the error from the above task_info is captured

local variable 'kpis_df' referenced before assignment

Is there a way to capture the warning messages? In the above example, the warnings are:

System mit Neuinstallation(en) nicht betreibbar (z.B. wegen nicht deckbarer Wärmenachfrage)

and

System ohne Neuinstallation(en) nicht betreibbar (z.B. wegen nicht deckbarer Wärmenachfrage)



from How to capture the celery warning(s) from logs?

No comments:

Post a Comment