bcquality/custom/knowledge/integration/prefer-business-events-over-handwritten-retry-loops.bad.al
Tharanga Chandrasekara 07140e2223 Add new action skills for AL testing and documentation
- Introduced `al-test-writer` to generate AL test codeunits for production objects based on TDD principles.
- Added `al-userguide-test-writer` to create test codeunits from user guide steps, mapping actions and assertions.
- Implemented `bc-extension-test-guide` to generate a comprehensive TEST_GUIDE.md for Business Central extensions, covering various categories.
- Created `bc-webclient-runner` to automate UI testing of the Business Central web client, capturing screenshots and asserting UI states.
- Developed `page-scripting-e2e` to produce a recording plan for Page Scripting, ensuring a structured approach to browser-level testing.
2026-06-07 12:26:47 +12:00

35 lines
1.7 KiB
AL

// Anti-pattern: a hand-written HTTP retry loop for a fire-and-forget notification. It
// reimplements platform retry, backoff, and durability (usually less correctly), couples
// delivery to BC staying up for the life of the loop, and runs inline on the caller's thread.
codeunit 50162 "Shipment Notifier Bad"
{
procedure NotifyShipmentReleased(DocumentNo: Code[20])
var
Client: HttpClient;
Content: HttpContent;
Response: HttpResponseMessage;
Attempt: Integer;
begin
Content.WriteFrom(BuildJson(DocumentNo));
// BAD: a hand-rolled retry loop for a one-way notification an external business event
// would carry. Everything in this loop is something the platform already does for free.
for Attempt := 1 to 5 do begin
if Client.Post('https://wms.contoso.com/api/events', Content, Response) then
// BAD: status classification by hand. A real implementation must distinguish
// 408/429/5xx (retry) from 4xx (give up), and this one does not even try.
if Response.IsSuccessStatusCode() then
exit;
// BAD: hand-rolled backoff. The platform's external-event delivery already retries
// with backoff for up to ~36 hours and persists the state across restarts.
Sleep(Attempt * 2000);
end;
// BAD: the loop lives entirely in this session. If the WMS is DOWN for the whole window
// the notification is lost; and if BC RESTARTS mid-loop, the retry state is gone and the
// notification is silently lost with no record that it was ever attempted. Nobody is
// alerted, so the gap is found only when downstream is discovered to be out of sync.
end;
}