Search code examples
djangorecursiondjango-celery

Should I use django-celery to update my DB recursively?


I am already thinking for few days what the best way to do it.

I am selling an item I am manufacturing however the item I am selling can be composed from other items and those other items can be composed from other items and so on.

After the sale order is created I want to trigger process creating internal sales orders of all the items required to build this item so it should be some kind of recursion .

Final items that cannot be produced have a flag is_production=False so i know i dont have to go deeper

For this I was thinking to create a method in view.py that will be triggered by confirmation of my Sales order. But I afraid that since i can not predict complexity of product manufactured it will take forever and will be costly for performance . As well there is a risk of never ending loops. so may be doing it in the view is not a great idea and I am thinking on alternative and by investigation I did it should be implemented with something like django-celery

Question since I never used django celery before and I want to confirm my understanding: Is django-celery is the best and only option to solve my requirement?


Solution

  • First, about the application design issue: as you said, "doing it in the view is not a great idea"... in general you must avoid running processes that require much time on the request-response cycle, this is bad for the UX and could end in the request being aborted because a timeout on some layer (think in database transaction timeout, application container timeout, reverse proxy timeout, etc).

    My recommendation: use something (Celery or any other solution) to off-load the process that creates internal sales orders, and any other long-running process (like sending emails).

    Second: which framework to use? There are so many options... you'll need to research that :-)

    Third: never ending loops. You'll have to fix that, regardeless of the design to use.