1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'cancellation_token.dart'; |
4 | | | import 'channel.dart'; |
5 | | | import 'squadron_exception.dart'; |
6 | | | import 'worker_exception.dart'; |
7 | | | import 'worker_service.dart'; |
8 | | | import 'worker_stat.dart'; |
9 | | |
|
10 | | | /// Base worker class. |
11 | | | /// |
12 | | | /// This base class takes care of creating the [Channel] and firing up the worker. |
13 | | | /// Typically, derived classes should add proxy methods sending [WorkerRequest]s to the worker. |
14 | | | abstract class Worker implements WorkerService { |
15 | | | /// Creates a [Worker] with the specified entrypoint. |
16 | | 2 | Worker(this._entryPoint, {String? id, this.args = const []}) { |
17 | | 4 | this.id = id ?? hashCode.toString(); |
18 | | 1 | } |
19 | | |
|
20 | | | /// The [Worker]'s entry point. |
21 | | | /// Typically, a top-level function in native world or a JavaScript Uri in browser world. |
22 | | | final dynamic _entryPoint; |
23 | | |
|
24 | | | /// The [Worker]'s start arguments. |
25 | | | final List args; |
26 | | |
|
27 | | | /// The [Worker] id. |
28 | | 1 | late final String id; |
29 | | |
|
30 | | | /// Start timestamp (in microseconds since Epoch). |
31 | | | int? _started; |
32 | | |
|
33 | | | /// Stopped timestamp (in microseconds since Epoch). |
34 | | | int? _stopped; |
35 | | |
|
36 | | | /// Current workload. |
37 | | 3 | int get workload => _workload; |
38 | | | int _workload = 0; |
39 | | |
|
40 | | | /// Maximum acceptable workload. |
41 | | 3 | int get maxWorkload => _maxWorkload; |
42 | | | int _maxWorkload = 0; |
43 | | |
|
44 | | | /// Total processed workload. |
45 | | 3 | int get totalWorkload => _totalWorkload; |
46 | | | int _totalWorkload = 0; |
47 | | |
|
48 | | | /// Total errors. |
49 | | 3 | int get totalErrors => _totalErrors; |
50 | | | int _totalErrors = 0; |
51 | | |
|
52 | | | /// Up time. |
53 | | 3 | Duration get upTime => (_started == null) |
54 | | 1 | ? Duration.zero |
55 | | 2 | : Duration( |
56 | | | microseconds: |
57 | | 6 | (_stopped ?? DateTime.now().microsecondsSinceEpoch) - _started!); |
58 | | |
|
59 | | | /// Idle time. |
60 | | 5 | Duration get idleTime => (_workload > 0 || _idle == null) |
61 | | 1 | ? Duration.zero |
62 | | 6 | : Duration(microseconds: DateTime.now().microsecondsSinceEpoch - _idle!); |
63 | | | int? _idle; |
64 | | |
|
65 | | | /// Indicates if the [Worker] has been stopped. |
66 | | 3 | bool get isStopped => _stopped != null; |
67 | | |
|
68 | | | /// [Worker] status. |
69 | | 2 | String get status { |
70 | | 2 | if (isStopped) { |
71 | | 1 | return 'STOPPED'; |
72 | | 3 | } else if (_workload == 0) { |
73 | | 1 | return 'IDLE'; |
74 | | | } else { |
75 | | 0 | return 'WORKING($_workload)'; |
76 | | | } |
77 | | 1 | } |
78 | | |
|
79 | | | /// [Worker] statistics. |
80 | | 7 | WorkerStat get stats => WorkerStat(runtimeType, id, isStopped, status, |
81 | | 7 | workload, maxWorkload, totalWorkload, totalErrors, upTime, idleTime); |
82 | | |
|
83 | | | /// [Channel] to communicate with the worker. |
84 | | 2 | Channel? get channel => _channel; |
85 | | | Channel? _channel; |
86 | | 1 | Future<Channel>? _channelRequest; |
87 | | |
|
88 | | 0 | static void _noop() {} |
89 | | |
|
90 | | 2 | SquadronCallback _canceller(CancellationToken? token) => |
91 | | 4 | (token == null) ? _noop : () => _channel?.notifyCancellation(token); |
92 | | |
|
93 | | | /// Sends a workload to the worker. |
94 | | 2 | Future<T> send<T>(int command, |
95 | | | [List args = const [], CancellationToken? token]) async { |
96 | | | // update stats |
97 | | 3 | _workload++; |
98 | | 3 | if (_workload > _maxWorkload) { |
99 | | 2 | _maxWorkload = _workload; |
100 | | | } |
101 | | |
|
102 | | | // ensure the worker is up and running |
103 | | | Channel channel; |
104 | | 2 | if (_channel != null) { |
105 | | 1 | channel = _channel!; |
106 | | | } else { |
107 | | 3 | channel = await start(); |
108 | | | } |
109 | | |
|
110 | | 2 | final canceller = _canceller(token); |
111 | | 1 | SquadronException? error = token?.exception; |
112 | | 1 | if (error == null) { |
113 | | 1 | try { |
114 | | | // check token |
115 | | 0 | token?.addListener(canceller); |
116 | | 0 | token?.start(); |
117 | | |
|
118 | | | // send request and return response |
119 | | 3 | return await channel.sendRequest<T>(command, args, token: token); |
120 | | 1 | } on CancelledException catch (e) { |
121 | | 0 | error = (token?.exception ?? e).withWorkerId(id).withCommand(command); |
122 | | 1 | } catch (e, st) { |
123 | | 2 | error = SquadronException.from( |
124 | | 1 | error: e, stackTrace: st, workerId: id, command: command); |
125 | | | } finally { |
126 | | | // update stats |
127 | | 2 | _workload--; |
128 | | 2 | _totalWorkload++; |
129 | | 0 | token?.removeListener(canceller); |
130 | | 3 | _idle = DateTime.now().microsecondsSinceEpoch; |
131 | | | } |
132 | | | } |
133 | | | // an error occured: update stats and throw exception |
134 | | 2 | _totalErrors++; |
135 | | 1 | throw error; |
136 | | 1 | } |
137 | | |
|
138 | | | /// Sends a streaming workload to the worker. |
139 | | 2 | Stream<T> stream<T>(int command, |
140 | | | [List args = const [], CancellationToken? token]) async* { |
141 | | | // update stats |
142 | | 3 | _workload++; |
143 | | 3 | if (_workload > _maxWorkload) { |
144 | | 2 | _maxWorkload = _workload; |
145 | | | } |
146 | | |
|
147 | | | // ensure the worker is up and running |
148 | | | Channel channel; |
149 | | 2 | if (_channel != null) { |
150 | | 1 | channel = _channel!; |
151 | | | } else { |
152 | | 3 | channel = await start(); |
153 | | | } |
154 | | |
|
155 | | 2 | final canceller = _canceller(token); |
156 | | 1 | SquadronException? error = token?.exception; |
157 | | 1 | if (error == null) { |
158 | | 1 | try { |
159 | | | // check token |
160 | | 2 | token?.addListener(canceller); |
161 | | 2 | token?.start(); |
162 | | |
|
163 | | | // send request and stream response items |
164 | | | final result = |
165 | | 2 | channel.sendStreamingRequest<T>(command, args, token: token); |
166 | | 3 | await for (var res in result) { |
167 | | | // check token |
168 | | 0 | if (error != null) break; |
169 | | 2 | yield res; |
170 | | 1 | error = token?.exception; |
171 | | | } |
172 | | 1 | return; |
173 | | 1 | } on CancelledException catch (e) { |
174 | | 5 | error = (token?.exception ?? e).withWorkerId(id).withCommand(command); |
175 | | 0 | } catch (e, st) { |
176 | | 0 | error = SquadronException.from( |
177 | | 0 | error: e, stackTrace: st, workerId: id, command: command); |
178 | | | } finally { |
179 | | | // update stats |
180 | | 2 | _workload--; |
181 | | 2 | _totalWorkload++; |
182 | | 2 | token?.removeListener(canceller); |
183 | | 3 | _idle = DateTime.now().microsecondsSinceEpoch; |
184 | | | } |
185 | | | } |
186 | | | // an error occured: update stats and throw exception |
187 | | 2 | _totalErrors++; |
188 | | 1 | throw error; |
189 | | 1 | } |
190 | | |
|
191 | | | /// Creates a [Channel] and starts the worker using the [_entryPoint]. |
192 | | 2 | Future<Channel> start() async { |
193 | | 2 | if (_stopped != null) { |
194 | | 3 | throw WorkerException('worker is stopped', workerId: id); |
195 | | | } |
196 | | 2 | if (_channel == null) { |
197 | | 5 | _channelRequest ??= Channel.open(_entryPoint, args); |
198 | | 3 | final channel = await _channelRequest!; |
199 | | 1 | if (_channel == null) { |
200 | | 3 | _started = DateTime.now().microsecondsSinceEpoch; |
201 | | 2 | _idle = _started; |
202 | | 1 | _channel = channel; |
203 | | | } |
204 | | | } |
205 | | 2 | return _channel!; |
206 | | 1 | } |
207 | | |
|
208 | | | /// Stops this worker. |
209 | | 2 | void stop() { |
210 | | 2 | if (_stopped == null) { |
211 | | 4 | _stopped = DateTime.now().microsecondsSinceEpoch; |
212 | | 2 | _channelRequest = null; |
213 | | 3 | _channel?.close(); |
214 | | 2 | _channel = null; |
215 | | | } |
216 | | 1 | } |
217 | | |
|
218 | | | /// Workers do not need an [operations] map. |
219 | | | @override |
220 | | 1 | final Map<int, CommandHandler> operations = WorkerService.noOperations; |
221 | | | } |