Skip to content

Commit 74ac9fa

Browse files
authored
Edit & Retry - Enhancing the web controller's response for a scenario when Edit & Retry is ignored (#5127)
1 parent 20c10e7 commit 74ac9fa

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/ServiceControl/MessageFailures/Api/EditFailedMessagesController.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,24 @@ public class EditFailedMessagesController(
2727

2828
[Route("edit/{failedMessageId:required:minlength(1)}")]
2929
[HttpPost]
30-
public async Task<IActionResult> Edit(string failedMessageId, [FromBody] EditMessageModel edit)
30+
public async Task<ActionResult<EditRetryResponse>> Edit(string failedMessageId, [FromBody] EditMessageModel edit)
3131
{
3232
if (!settings.AllowMessageEditing)
3333
{
3434
logger.LogInformation("Message edit-retry has not been enabled");
3535
return NotFound();
3636
}
3737

38+
//HINT: This validation is the first one because we want to minimize the chance of two users concurrently execute an edit-retry.
39+
var editManager = await store.CreateEditFailedMessageManager();
40+
var editId = await editManager.GetCurrentEditingMessageId(failedMessageId);
41+
if (editId != null)
42+
{
43+
logger.LogWarning("Cannot edit message {FailedMessageId} because it has already been edited", failedMessageId);
44+
// We return HTTP 200 even though the edit is being ignored. This is to keep the compatibility with older versions of ServicePulse that don't handle the payload.
45+
return Ok(new EditRetryResponse { EditIgnored = true });
46+
}
47+
3848
var failedMessage = await store.ErrorBy(failedMessageId);
3949

4050
if (failedMessage == null)
@@ -45,8 +55,8 @@ public async Task<IActionResult> Edit(string failedMessageId, [FromBody] EditMes
4555

4656
//WARN
4757
/*
48-
* failedMessage.ProcessingAttempts.Last() return the lat retry attempt.
49-
* In theory between teh time someone edits a failed message and retry it someone else
58+
* failedMessage.ProcessingAttempts.Last() return the last retry attempt.
59+
* In theory between the time someone edits a failed message and retry it someone else
5060
* could have retried the same message without editing. If this is the case "Last()" is
5161
* not anymore the same message.
5262
* Instead of using Last() it's probably better to select the processing attempt by looking for
@@ -74,7 +84,7 @@ await session.SendLocal(new EditAndSend
7484
NewHeaders = edit.MessageHeaders
7585
});
7686

77-
return Accepted();
87+
return Accepted(new EditRetryResponse { EditIgnored = false });
7888
}
7989

8090

@@ -137,4 +147,9 @@ public class EditMessageModel
137147

138148
public Dictionary<string, string> MessageHeaders { get; set; }
139149
}
150+
151+
public class EditRetryResponse
152+
{
153+
public bool EditIgnored { get; set; }
154+
}
140155
}

0 commit comments

Comments
 (0)