Skip to content

Commit 9207eda

Browse files
committed
[threads] background workers refactorized
1 parent 0323723 commit 9207eda

File tree

1 file changed

+103
-18
lines changed

1 file changed

+103
-18
lines changed

Quick.Threads.pas

+103-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Author : Kike Pérez
88
Version : 1.5
99
Created : 09/03/2018
10-
Modified : 08/02/2021
10+
Modified : 08/03/2021
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -595,12 +595,29 @@ TBackgroundWorkers = class
595595
private
596596
fWorkerPool : TWorkerPool;
597597
fConcurrentWorkers : Integer;
598-
fWorkerTask : IWorkTask;
598+
fWorkerInitProc : TTaskProc;
599+
fWorkerExecuteProc : TTaskProc;
600+
fWorkerRetryProc : TTaskRetryProc;
601+
fWorkerExceptionProc : TTaskExceptionProc;
602+
fWorkerTerminateProc : TTaskProc;
603+
fMaxRetries : Integer;
604+
fFaultPolicy : TFaultPolicy;
605+
procedure SetRetryPolicy(aMaxRetries, aWaitTimeBetweenRetriesMS : Integer; aWaitTimeMultiplierFactor: Double);
599606
public
600-
constructor Create(aConcurrentWorkers : Integer);
607+
constructor Create(aConcurrentWorkers : Integer; aWorkerProc : TTaskProc);
601608
destructor Destroy; override;
602609
property ConcurrentWorkers : Integer read fConcurrentWorkers;
603-
function OnExecute(aWorkerProc : TTaskProc) : IWorkTask;
610+
function OnInitialize(aTaskProc : TTaskProc) : TBackgroundWorkers;
611+
function OnException(aTaskProc : TTaskExceptionProc) : TBackgroundWorkers;
612+
function OnRetry(aTaskProc : TTaskRetryProc) : TBackgroundWorkers;
613+
function OnTerminated(aTaskProc : TTaskProc) : TBackgroundWorkers;
614+
function Retry(aMaxRetries : Integer) : TBackgroundWorkers;
615+
function RetryForever : TBackgroundWorkers;
616+
function WaitAndRetry(aMaxRetries, aWaitTimeBetweenRetriesMS : Integer) : TBackgroundWorkers; overload;
617+
function WaitAndRetry(aWaitTimeArray : TArray<Integer>) : TBackgroundWorkers; overload;
618+
function WaitAndRetry(aMaxRetries, aWaitTimeBetweenRetriesMS : Integer; aWaitTimeMultiplierFactor : Double) : TBackgroundWorkers; overload;
619+
function WaitAndRetryForever(aWaitTimeBetweenRetriesMS : Integer) : TBackgroundWorkers; overload;
620+
function WaitAndRetryForever(aWaitTimeBetweenRetriesMS : Integer; aWaitTimeMultiplierFactor : Double) : TBackgroundWorkers; overload;
604621
procedure Start;
605622
procedure Stop;
606623
end;
@@ -1616,13 +1633,13 @@ procedure TSimpleWorker.Execute;
16161633
end;
16171634
finally
16181635
fStatus := TWorkerStatus.wsIdle;
1619-
if fRunOnce then Terminate;
16201636
try
16211637
if TTask(fCurrentTask).TerminateWithSync then Synchronize(TerminateTask)
16221638
else fCurrentTask.DoTerminate;
16231639
except
16241640
on E : Exception do if fCurrentTask <> nil then fCurrentTask.DoException(E)
16251641
end;
1642+
if fRunOnce then Terminate;
16261643
end;
16271644
end;
16281645
fStatus := TWorkerStatus.wsSuspended
@@ -2331,9 +2348,11 @@ destructor TParamValue.Destroy;
23312348

23322349
{ TBackgroundWorkers }
23332350

2334-
constructor TBackgroundWorkers.Create(aConcurrentWorkers: Integer);
2351+
constructor TBackgroundWorkers.Create(aConcurrentWorkers : Integer; aWorkerProc : TTaskProc);
23352352
begin
23362353
fConcurrentWorkers := aConcurrentWorkers;
2354+
fWorkerExecuteProc := aWorkerProc;
2355+
fWorkerPool := TWorkerPool.Create(True);
23372356
end;
23382357

23392358
destructor TBackgroundWorkers.Destroy;
@@ -2342,25 +2361,23 @@ destructor TBackgroundWorkers.Destroy;
23422361
inherited;
23432362
end;
23442363

2345-
function TBackgroundWorkers.OnExecute(aWorkerProc: TTaskProc): IWorkTask;
2346-
begin
2347-
// fWorkerTask := TWorkTask.Create([],False,procedure(task : ITask)
2348-
// begin
2349-
// aWorkerProc;
2350-
// end);
2351-
fWorkerTask := TWorkTask.Create([],False,aWorkerProc);
2352-
fWorkerTask.Run;
2353-
Result := fWorkerTask;
2354-
end;
2355-
23562364
procedure TBackgroundWorkers.Start;
23572365
var
23582366
i : Integer;
23592367
worker : TWorker;
2368+
task : IWorkTask;
23602369
begin
23612370
for i := 1 to fConcurrentWorkers do
23622371
begin
2363-
worker := TSimpleWorker.Create(fWorkerTask,False);
2372+
task := TWorkTask.Create([],False,fWorkerExecuteProc)
2373+
.OnInitialize(fWorkerInitProc)
2374+
.OnRetry(fWorkerRetryProc)
2375+
.OnException(fWorkerExceptionProc)
2376+
.OnTerminated(fWorkerTerminateProc);
2377+
task.NumWorker := i;
2378+
task.Run;
2379+
worker := TSimpleWorker.Create(task,False);
2380+
fWorkerPool.Add(worker);
23642381
worker.Start;
23652382
end;
23662383
end;
@@ -2377,4 +2394,72 @@ procedure TBackgroundWorkers.Stop;
23772394
end;
23782395
end;
23792396

2397+
function TBackgroundWorkers.OnException(aTaskProc: TTaskExceptionProc): TBackgroundWorkers;
2398+
begin
2399+
Result := Self;
2400+
fWorkerExceptionProc := aTaskProc;
2401+
end;
2402+
2403+
function TBackgroundWorkers.OnInitialize(aTaskProc: TTaskProc): TBackgroundWorkers;
2404+
begin
2405+
Result := Self;
2406+
fWorkerInitProc := aTaskProc;
2407+
end;
2408+
2409+
function TBackgroundWorkers.OnRetry(aTaskProc: TTaskRetryProc): TBackgroundWorkers;
2410+
begin
2411+
Result := Self;
2412+
fWorkerRetryProc := aTaskProc;
2413+
end;
2414+
2415+
function TBackgroundWorkers.OnTerminated(aTaskProc: TTaskProc): TBackgroundWorkers;
2416+
begin
2417+
Result := Self;
2418+
fWorkerTerminateProc := aTaskProc;
2419+
end;
2420+
2421+
function TBackgroundWorkers.Retry(aMaxRetries: Integer): TBackgroundWorkers;
2422+
begin
2423+
Result := Self;
2424+
SetRetryPolicy(aMaxRetries,0,1);
2425+
end;
2426+
2427+
function TBackgroundWorkers.RetryForever: TBackgroundWorkers;
2428+
begin
2429+
Result := Self;
2430+
SetRetryPolicy(-1,0,1);
2431+
end;
2432+
2433+
procedure TBackgroundWorkers.SetRetryPolicy(aMaxRetries, aWaitTimeBetweenRetriesMS: Integer; aWaitTimeMultiplierFactor: Double);
2434+
begin
2435+
fFaultPolicy.MaxRetries := aMaxRetries;
2436+
fFaultPolicy.WaitTimeBetweenRetries := aWaitTimeBetweenRetriesMS;
2437+
fFaultPolicy.WaitTimeMultiplierFactor := aWaitTimeMultiplierFactor;
2438+
end;
2439+
2440+
function TBackgroundWorkers.WaitAndRetry(aMaxRetries, aWaitTimeBetweenRetriesMS: Integer; aWaitTimeMultiplierFactor: Double): TBackgroundWorkers;
2441+
begin
2442+
2443+
end;
2444+
2445+
function TBackgroundWorkers.WaitAndRetry(aWaitTimeArray: TArray<Integer>): TBackgroundWorkers;
2446+
begin
2447+
2448+
end;
2449+
2450+
function TBackgroundWorkers.WaitAndRetry(aMaxRetries, aWaitTimeBetweenRetriesMS: Integer): TBackgroundWorkers;
2451+
begin
2452+
2453+
end;
2454+
2455+
function TBackgroundWorkers.WaitAndRetryForever(aWaitTimeBetweenRetriesMS: Integer): TBackgroundWorkers;
2456+
begin
2457+
2458+
end;
2459+
2460+
function TBackgroundWorkers.WaitAndRetryForever(aWaitTimeBetweenRetriesMS: Integer; aWaitTimeMultiplierFactor: Double): TBackgroundWorkers;
2461+
begin
2462+
2463+
end;
2464+
23802465
end.

0 commit comments

Comments
 (0)