How to handle management command exception in django app?

695 views Asked by At

I'm working on a django app which is based on consuming an external ZeroMQ message queue - it listens to the queue all the time, processing the messages and saving the outcome in the database. The data will be in the future made accessible to the users by API.

Currently I'm running it by a custom management command listen, which runs an infinite loop and it works fine. However, whenever some kind of error appears (and they will sometimes appear, it's an external data source) the command dies.

What's the best approach to handle such errors? Things I'm considering:

  1. Bash script which will restart the command on failure
  2. Big Try... except Exception in the main command function (but this seems extremely wrong)
  3. Some custom exception handler - the problem is that the recommended handlers only handle errors in views/requests, and my command is not a view/request, it just runs.

Any advice would be useful, maybe it's a misuse of the management command?

1

There are 1 answers

0
Tarsis Azevedo On

I have a command that is a crawler and I am wrapping my handle method's code with a try/except clause to avoid stopping on errors. But I noticed that when Django triggers a database exception the command will stop, ignoring the try/except blocks. I didn't find out why yet.

I think that is the best option for commands that should run "forever" (on run for a long period of time).