django-concurrency icon indicating copy to clipboard operation
django-concurrency copied to clipboard

how to use this for max 1 child using conditional?

Open simkimsia opened this issue 3 years ago • 0 comments

I'm trying to understand if django-concurrency is a good solution for my problem.

I have two models:

class Order:
  status = CharField()
  
class PushToMiddleware:
   order = ForeignKey(Order)
   

the Order status has 3 values : READY_TO_PUSH, PUSHING, PUSHED, STUCK

My problem is I can only allow max 1 PushToMiddleware at any time when order is READY_TO_PUSH

My pseudocode in Gherkin syntax

GIVEN Order is READY_TO_PUSH WHEN I want to push THEN Order changes to PUSHING AND followed by create a PushToMiddleware child record AND some extra processing that includes an API Post call to a 3rd party AND depending on the result of the API, the PushToMiddleware will be updated accordingly AND if successful API call, the Order changes to PUSHED, else to STUCK

GIVEN Order is STUCK WHEN the user wants to repush THEN the Order changes to READY_TO_PUSH

In terms of state transitions:

START->READY_TO_PUSH-> PUSHING-> PUSHED->TERMINAL

START->READY_TO_PUSH-> PUSHING-> STUCK->READY_TO_PUSH (so this can be a loop)

I was wondering if i can include a conditionalversioned field on Order so that i can do a conditional create on PushToMiddleware in a single atomic transaction.

Atomic transaction when READY_TO_PUSH:

  1. update the Order status from READY_TO_PUSH to PUSHING
  2. create a new PushToMiddleware record

Atomic transaction when API call is successful

  1. update the Order status from PUSHING to PUSHED
  2. update the PushToMiddleware record with API call result

Atomic transaction when API call is failure

  1. update the Order status from PUSHING to STUCK
  2. update the PushToMiddleware record with API call result

My concern is the first atomic transaction because the business logic needs to ensure max 1 PushToMiddleware record when status is READY_TO_PUSH

simkimsia avatar Apr 25 '22 15:04 simkimsia