-
Notifications
You must be signed in to change notification settings - Fork 604
[ENG-401] fix: token queue should respect sub queues while calling serve next #3708
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
base: develop
Are you sure you want to change the base?
Changes from all commits
637e5ce
9d383a7
bc2eec6
14ac7ae
a7ace39
140b0b1
b21fa4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,17 +81,21 @@ def perform_create(self, instance): | |
| super().perform_create(instance) | ||
|
|
||
| def validate_data(self, instance, model_obj=None): | ||
| if ( | ||
| model_obj | ||
| and instance.sub_queue | ||
| and model_obj.sub_queue | ||
| and instance.sub_queue != model_obj.sub_queue.external_id | ||
| ): | ||
| existing_current = TokenSubQueue.objects.filter( | ||
| current_token=model_obj | ||
| ).exists() | ||
| if existing_current: | ||
| raise ValidationError("Sub Queue already has a current token") | ||
| if model_obj and instance.sub_queue: | ||
| if ( | ||
| model_obj.sub_queue | ||
| and instance.sub_queue != model_obj.sub_queue.external_id | ||
| ): | ||
| existing_current = TokenSubQueue.objects.filter( | ||
| current_token=model_obj | ||
| ).exists() | ||
| if existing_current: | ||
| raise ValidationError("Sub Queue already has a current token") | ||
| if ( | ||
| instance.sub_queue | ||
| and instance.status == TokenStatusOptions.IN_PROGRESS.value | ||
| ): | ||
| raise ValidationError("Use set_next endpoint to assign a token.") | ||
|
|
||
| return super().validate_data(instance, model_obj) | ||
|
|
||
|
|
@@ -100,18 +104,26 @@ def perform_update(self, instance): | |
| raise ValidationError("Sub Queue and Queue are not in the same facility") | ||
| with transaction.atomic(): | ||
| obj = self.get_object() | ||
| if obj.sub_queue and obj.sub_queue != instance.sub_queue: | ||
| if ( | ||
| instance.sub_queue | ||
| and obj.sub_queue.resource != instance.sub_queue.resource | ||
| ): | ||
| raise ValidationError( | ||
| "Sub Queue and Queue are not in the same resource" | ||
| ) | ||
| # Clear current token if the sub queue is changed | ||
| if obj.sub_queue.current_token == obj: | ||
| obj.sub_queue.current_token = None | ||
| obj.sub_queue.save(update_fields=["current_token", "modified_date"]) | ||
| if ( | ||
| obj.sub_queue | ||
| and obj.sub_queue != instance.sub_queue | ||
| and instance.sub_queue | ||
| and obj.sub_queue.resource != instance.sub_queue.resource | ||
| ): | ||
| raise ValidationError( | ||
| "Sub Queue and Queue are not in the same resource" | ||
| ) | ||
| # Clear current token if the sub queue is changed | ||
| if ( | ||
| obj.sub_queue | ||
| and obj.sub_queue.current_token == obj | ||
| and ( | ||
| obj.sub_queue != instance.sub_queue | ||
| or instance.status != TokenStatusOptions.IN_PROGRESS.value | ||
| ) | ||
| ): | ||
| obj.sub_queue.current_token = None | ||
| obj.sub_queue.save(update_fields=["current_token", "modified_date"]) | ||
| super().perform_update(instance) | ||
|
|
||
| def perform_destroy(self, instance): | ||
|
|
@@ -181,17 +193,22 @@ def get_queryset(self): | |
| @action(detail=True, methods=["POST"]) | ||
| def set_next(self, request, *args, **kwargs): | ||
| obj = self.get_object() | ||
| if obj.status != TokenStatusOptions.CREATED.value: | ||
| raise ValidationError("Token in serving state cannot be set next") | ||
| request_obj = SetCurrentTokenRequest(**request.data) | ||
| queue = obj.queue | ||
| self.authorize_update(None, obj) | ||
| with transaction.atomic(): | ||
| sub_queue = get_object_or_404( | ||
| TokenSubQueue, | ||
| external_id=request_obj.sub_queue, | ||
| resource=queue.resource, | ||
| ) | ||
| sub_queue = get_object_or_404( | ||
| TokenSubQueue, | ||
| external_id=request_obj.sub_queue, | ||
| resource=queue.resource, | ||
| ) | ||
| with Lock(f"token:set_next:{sub_queue.id}"), transaction.atomic(): | ||
| if sub_queue.current_token and sub_queue.current_token != obj: | ||
| raise ValidationError("Sub Queue already has a current token") | ||
| sub_queue.current_token = obj | ||
| sub_queue.save() | ||
| obj.status = TokenStatusOptions.IN_PROGRESS.value | ||
| obj.sub_queue = sub_queue | ||
| obj.save() | ||
|
Comment on lines
+201
to
213
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Re-fetch with Lock(f"token:set_next:{sub_queue.id}"), transaction.atomic():
sub_queue = TokenSubQueue.objects.select_for_update().get(id=sub_queue.id)
if sub_queue.current_token and sub_queue.current_token != obj:
raise ValidationError("Sub Queue already has a current token")
... |
||
| return Response(self.get_retrieve_pydantic_model().serialize(obj).to_json()) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sub_queueis nullThe new validation at lines 94–98 that blocks direct
IN_PROGRESSupdates lives insideif model_obj and instance.sub_queue:. A caller that sends{"status": "in_progress", "sub_queue": null}satisfies pydantic (sinceTokenUpdateSpec.sub_queue: UUID4 | Noneaccepts null) but makesinstance.sub_queuefalsy, so the entire guard is skipped. The token then transitions toIN_PROGRESSwithsub_queue = nullinperform_update— the old sub-queue'scurrent_tokengets cleared, but nothing sets a newcurrent_token, leaving the queue in an inconsistent state.The
IN_PROGRESSstatus check should be guarded onmodel_objalone, independent of whether a sub-queue is being provided in the same request.