Repository navigation
Expand file tree
/
Copy pathQuick.Core.Extensions.Hosting.BackgroundService.pas
More file actions
229 lines (199 loc) · 5.83 KB
/
Copy pathQuick.Core.Extensions.Hosting.BackgroundService.pas
File metadata and controls
229 lines (199 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{ **************************** }
{ Quick.Core Library }
{ IHostedService / Background }
{ **************************** }
{ Author: QuickCore Team }
{ **************************** }
unit Quick.Core.Extensions.Hosting.BackgroundService;
{$i QuickCore.inc}
interface
uses
System.SysUtils,
System.Classes,
System.TypInfo,
System.Generics.Collections,
Quick.Core.DependencyInjection;
type
{ Implement StartAsync/StopAsync to provide a hosted service that runs
alongside the application. Use AddHostedService<T> to register. }
IHostedService = interface
['{B3F1A6D2-7C8E-4A3F-9D1B-2E5C0F4A7B3D}']
procedure StartAsync;
procedure StopAsync;
end;
{ Manages the lifecycle of all registered IHostedService instances.
Call Initialize(ServiceCollection) once after Build, then StartAll/StopAll. }
IHostedServiceManager = interface
['{A4C2E8F1-3D7B-4E9A-8C6F-1B5A0D3E7C2F}']
procedure Initialize(aServiceCollection : TServiceCollection);
procedure StartAll;
procedure StopAll;
end;
THostedServiceManager = class(TInterfacedObject, IHostedServiceManager)
private
{ Pending type-infos registered before Build.
Class var so they are accumulated by AddHostedService calls. }
class var fPendingTypes : TList<PTypeInfo>;
class var fPendingNames : TList<string>;
fRunning : TList<IHostedService>;
public
constructor Create;
destructor Destroy; override;
class constructor ClassCreate;
class destructor ClassDestroy;
class procedure AddPendingType(aTypeInfo : PTypeInfo; const aName : string);
procedure Initialize(aServiceCollection : TServiceCollection);
procedure StartAll;
procedure StopAll;
end;
{ Abstract base. Override ExecuteAsync with your background logic.
ExecuteAsync runs on a background thread. Check CancelRequested in loops. }
TBackgroundService = class abstract(TInterfacedObject, IHostedService)
private type
TWorkerThread = class(TThread)
private
fOwner : TBackgroundService;
public
constructor Create(aOwner : TBackgroundService);
procedure Execute; override;
end;
private
fThread : TWorkerThread;
fCancelRequested : Boolean;
protected
procedure ExecuteAsync; virtual; abstract;
property CancelRequested : Boolean read fCancelRequested;
public
constructor Create;
destructor Destroy; override;
procedure StartAsync; virtual;
procedure StopAsync; virtual;
end;
THostedServiceExtension = class(TServiceCollectionExtension)
public
{ Register TImplementation as a singleton IHostedService that will be
started when the host starts and stopped when it stops. }
class function AddHostedService<TImplementation : class, constructor> : TServiceCollection;
end;
implementation
uses
System.Rtti;
{ THostedServiceManager }
class constructor THostedServiceManager.ClassCreate;
begin
fPendingTypes := TList<PTypeInfo>.Create;
fPendingNames := TList<string>.Create;
end;
class destructor THostedServiceManager.ClassDestroy;
begin
fPendingTypes.Free;
fPendingNames.Free;
end;
constructor THostedServiceManager.Create;
begin
fRunning := TList<IHostedService>.Create;
end;
destructor THostedServiceManager.Destroy;
begin
fRunning.Free;
inherited;
end;
class procedure THostedServiceManager.AddPendingType(aTypeInfo : PTypeInfo; const aName : string);
begin
fPendingTypes.Add(aTypeInfo);
fPendingNames.Add(aName);
end;
procedure THostedServiceManager.Initialize(aServiceCollection : TServiceCollection);
var
i : Integer;
svc : IHostedService;
value : TValue;
begin
fRunning.Clear;
for i := 0 to fPendingTypes.Count - 1 do
begin
value := aServiceCollection.Resolve(fPendingTypes[i], fPendingNames[i]);
if value.IsObjectInstance then
begin
if Supports(value.AsObject, IHostedService, svc) then
fRunning.Add(svc);
end
else if value.Kind = tkInterface then
begin
if value.AsInterface.QueryInterface(IHostedService, svc) = S_OK then
fRunning.Add(svc);
end;
end;
end;
procedure THostedServiceManager.StartAll;
var
svc : IHostedService;
begin
for svc in fRunning do
svc.StartAsync;
end;
procedure THostedServiceManager.StopAll;
var
svc : IHostedService;
begin
for svc in fRunning do
svc.StopAsync;
end;
{ TBackgroundService.TWorkerThread }
constructor TBackgroundService.TWorkerThread.Create(aOwner : TBackgroundService);
begin
inherited Create(True);
fOwner := aOwner;
FreeOnTerminate := False;
end;
procedure TBackgroundService.TWorkerThread.Execute;
begin
fOwner.ExecuteAsync;
end;
{ TBackgroundService }
constructor TBackgroundService.Create;
begin
fThread := nil;
fCancelRequested := False;
end;
destructor TBackgroundService.Destroy;
begin
if fThread <> nil then
begin
fCancelRequested := True;
fThread.WaitFor;
fThread.Free;
end;
inherited;
end;
procedure TBackgroundService.StartAsync;
begin
fCancelRequested := False;
fThread := TWorkerThread.Create(Self);
fThread.Start;
end;
procedure TBackgroundService.StopAsync;
begin
fCancelRequested := True;
if fThread <> nil then
begin
fThread.WaitFor;
FreeAndNil(fThread);
end;
end;
{ THostedServiceExtension }
class function THostedServiceExtension.AddHostedService<TImplementation> : TServiceCollection;
var
typeName : string;
begin
Result := ServiceCollection;
// Ensure the manager is registered
if not ServiceCollection.IsRegistered<IHostedServiceManager> then
ServiceCollection.AddSingleton<IHostedServiceManager, THostedServiceManager>;
// Register the concrete implementation as a singleton with a unique name
typeName := TImplementation.ClassName;
ServiceCollection.AddSingleton<TImplementation>(typeName);
// Record the type info so the manager can resolve it after Build
THostedServiceManager.AddPendingType(TypeInfo(TImplementation), typeName);
end;
end.