Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use this for max 1 child using conditional? #240

Open
simkimsia opened this issue Apr 25, 2022 · 0 comments
Open

how to use this for max 1 child using conditional? #240

simkimsia opened this issue Apr 25, 2022 · 0 comments

Comments

@simkimsia
Copy link

simkimsia commented Apr 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant