mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-10-06 23:26:55 +01:00
fix(community/performance): correct httpclient and oncompanyopen good samples
httpclient-inside-write-transaction-holds-locks.good.al - Pass Customer.RecordId as the last argument to CreateTask so the task is bound to the single customer that was written, not to all customers. - Declare TableNo = Customer on the task codeunit so the platform loads the bound record into Rec automatically when OnRun executes. - Replace the FindSet loop over all customers with Rec."No.", preserving the one-customer scope of the original SyncCustomerLastName procedure. oncompanyopen-subscribers-must-not-do-io.good.al - Replace Session.GetCurrentClientType() with Session.CurrentClientType(), the correct platform method name. - Fix the idempotency check: TaskScheduler.TaskExists() requires a Guid, not a codeunit integer ID. Store the Guid returned by CreateTask in IsolatedStorage (DataScope::Company) under a fixed key; on the next login read it back as Text, Evaluate it to Guid, and pass that Guid to TaskExists so the type matches the method signature.
This commit is contained in:
parent
5f9328618d
commit
8373bc4717
2 changed files with 17 additions and 13 deletions
|
|
@ -5,22 +5,21 @@ codeunit 50100 "HttpClient Holds Locks Good"
|
||||||
begin
|
begin
|
||||||
Customer."Search Name" := Customer.Name;
|
Customer."Search Name" := Customer.Name;
|
||||||
Customer.Modify(false);
|
Customer.Modify(false);
|
||||||
TaskScheduler.CreateTask(Codeunit::"Customer Sync Task", 0, true, CompanyName(), CurrentDateTime());
|
// RecordId binds the task to this specific customer; the platform loads it into Rec on OnRun.
|
||||||
|
TaskScheduler.CreateTask(Codeunit::"Customer Sync Task", 0, true, CompanyName(), CurrentDateTime(), Customer.RecordId);
|
||||||
end;
|
end;
|
||||||
}
|
}
|
||||||
|
|
||||||
codeunit 50101 "Customer Sync Task"
|
codeunit 50101 "Customer Sync Task"
|
||||||
{
|
{
|
||||||
|
TableNo = Customer;
|
||||||
|
|
||||||
trigger OnRun()
|
trigger OnRun()
|
||||||
var
|
var
|
||||||
Client: HttpClient;
|
Client: HttpClient;
|
||||||
Customer: Record Customer;
|
|
||||||
Response: HttpResponseMessage;
|
Response: HttpResponseMessage;
|
||||||
begin
|
begin
|
||||||
// Separate session: no write-transaction lock is held during the HTTP call.
|
// Separate session: Rec is the single customer passed via RecordId; no write-transaction lock is held.
|
||||||
if Customer.FindSet() then
|
Client.Get(StrSubstNo('https://example.local/sync/%1', Rec."No."), Response);
|
||||||
repeat
|
|
||||||
Client.Get(StrSubstNo('https://example.local/sync/%1', Customer."No."), Response);
|
|
||||||
until Customer.Next() = 0;
|
|
||||||
end;
|
end;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,22 @@ codeunit 50100 "Login Subscriber IO Good"
|
||||||
{
|
{
|
||||||
[EventSubscriber(ObjectType::Codeunit, Codeunit::"System Initialization", OnAfterLogin, '', false, false)]
|
[EventSubscriber(ObjectType::Codeunit, Codeunit::"System Initialization", OnAfterLogin, '', false, false)]
|
||||||
local procedure OnAfterLogin()
|
local procedure OnAfterLogin()
|
||||||
|
var
|
||||||
|
TaskId: Guid;
|
||||||
|
StoredId: Text;
|
||||||
begin
|
begin
|
||||||
// Guard to interactive sessions only; background task sessions also raise OnAfterLogin.
|
// Guard to interactive sessions only; background task sessions also raise OnAfterLogin.
|
||||||
if not (Session.GetCurrentClientType() in [ClientType::Web, ClientType::Windows, ClientType::Desktop, ClientType::Tablet, ClientType::Phone]) then
|
if not (Session.CurrentClientType() in [ClientType::Web, ClientType::Windows, ClientType::Desktop, ClientType::Tablet, ClientType::Phone]) then
|
||||||
exit;
|
exit;
|
||||||
|
|
||||||
// Idempotent: skip if a task for this codeunit is already queued.
|
// Idempotent: TaskExists requires the GUID returned by CreateTask, stored across logins.
|
||||||
if TaskScheduler.TaskExists(Codeunit::"Login Subscriber IO Work") then
|
if IsolatedStorage.Get('LoginSyncTaskId', DataScope::Company, StoredId) then
|
||||||
exit;
|
if Evaluate(TaskId, StoredId) then
|
||||||
|
if TaskScheduler.TaskExists(TaskId) then
|
||||||
|
exit;
|
||||||
|
|
||||||
// Defer the I/O work; CreateTask is the only write allowed on this path.
|
TaskId := TaskScheduler.CreateTask(Codeunit::"Login Subscriber IO Work", 0, true, CompanyName(), CurrentDateTime() + 60000);
|
||||||
TaskScheduler.CreateTask(Codeunit::"Login Subscriber IO Work", 0, true, CompanyName(), CurrentDateTime() + 60000);
|
IsolatedStorage.Set('LoginSyncTaskId', Format(TaskId), DataScope::Company);
|
||||||
end;
|
end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue