Skip to content

Commit ee6206b

Browse files
committed
wip: fixing up errors
1 parent a8185b6 commit ee6206b

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

src/WorkerPool.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,24 @@ class WorkerPool {
7171
});
7272
}
7373

74+
// TODO: we need to handle a worker that doesn't respond at all. Timeout should terminate and avoid re-creating
7475
protected addWorker() {
7576
const worker = this.workerFactory();
7677
let workerError: Error;
7778
let initializing = true;
78-
const messageHandler = (result: WorkerResultInternal) => {
79+
const messageHandler = (result: WorkerResultInternal | 'initialized') => {
7980
if (initializing) {
80-
// @ts-ignore: ignoring type here
8181
if (result !== 'initialized') {
82-
throw Error('TMP IMP failed to initialize properly');
82+
throw new errors.ErrorWorkerInitializationFailed();
8383
}
8484
initializing = false;
8585
this.freeWorkers.push(worker);
8686
this.$workerFreed.next();
8787
return;
8888
}
89+
if (result === 'initialized') {
90+
throw new errors.ErrorWorkersUndefinedBehaviour();
91+
}
8992
if (result.error != null) {
9093
worker[taskInfoSymbol].done(undefined, result.error);
9194
} else {
@@ -115,9 +118,8 @@ class WorkerPool {
115118
) {
116119
// If the worker errored then we want to check if it was a failure to load error
117120
if (this.workers.size === 0 && this.terminatedError == null) {
118-
this.terminatedError = new Error(
119-
'TMP IMP Workers failed to load modules',
120-
);
121+
this.terminatedError =
122+
new errors.ErrorWorkerPoolWorkerCreationFailed();
121123
this.cleanUp(this.terminatedError);
122124
}
123125
return;
@@ -127,13 +129,6 @@ class WorkerPool {
127129
worker.once('online', async () => {
128130
worker.postMessage({ type: 'initialize', data: undefined });
129131
});
130-
131-
// TODO: debugging
132-
// worker.on('message', (...args) => console.log('DEBUG message: ', args));
133-
// worker.on('messageerror', (...args) => console.log('DEBUG messageerror: ', args));
134-
// worker.on('error', (...args) => console.log('DEBUG error: ', args));
135-
// worker.on('exit', (...args) => console.log('DEBUG exit: ', args));
136-
// worker.on('online', (...args) => console.log('DEBUG online: ', args));
137132
this.workers.add(worker);
138133
this.$workerCreated.next();
139134
}
@@ -158,7 +153,7 @@ class WorkerPool {
158153

159154
public async terminate(force: boolean) {
160155
if (this.terminatedError != null) return;
161-
this.terminatedError = Error('TMP IMP terminating');
156+
this.terminatedError = new errors.ErrorWorkerPoolWorkerTerminated();
162157
// Prevent new tasks and wait for exising queue to drain
163158
if (!force) await this.settled();
164159
// Prevent terminations from creating new workers

src/errors.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
import { AbstractError } from '@matrixai/errors';
22

3-
class ErrorWorkerManager<T> extends AbstractError<T> {
3+
class ErrorWorkers<T> extends AbstractError<T> {
4+
static description = 'Workers domain error';
5+
}
6+
7+
class ErrorWorkersUndefinedBehaviour<T> extends ErrorWorkers<T> {
8+
static description = 'You should never see this error';
9+
}
10+
11+
class ErrorWorkerManager<T> extends ErrorWorkers<T> {
412
static description = 'WorkerManager error';
513
}
614

715
class ErrorWorkerManagerDestroyed<T> extends ErrorWorkerManager<T> {
816
static description = 'WorkerManager is destroyed';
917
}
1018

11-
class ErrorWorkerPool<T> extends AbstractError<T> {
19+
class ErrorWorkerPool<T> extends ErrorWorkers<T> {
1220
static description = 'WorkerPool error';
1321
}
1422

15-
class ErrorWorkerPoolInvalidWorkers<T> extends ErrorWorkerManager<T> {
23+
class ErrorWorkerPoolInvalidWorkers<T> extends ErrorWorkerPool<T> {
1624
static description = 'You must have at minimum 1 worker';
1725
}
1826

19-
class ErrorWorker<T> extends AbstractError<T> {
27+
class ErrorWorkerPoolWorkerCreationFailed<T> extends ErrorWorkerPool<T> {
28+
static description = 'Failed to create workers when creating a WorkerPool';
29+
}
30+
31+
class ErrorWorkerPoolWorkerTerminated<T> extends ErrorWorkerPool<T> {
32+
static description = 'Worker pool is terminated';
33+
}
34+
35+
class ErrorWorker<T> extends ErrorWorkers<T> {
2036
static description = 'Worker error';
2137
}
2238

23-
class ErrorWorkerHandlerMissing<T> extends ErrorWorkerManager<T> {
39+
class ErrorWorkerHandlerMissing<T> extends ErrorWorker<T> {
2440
static description = 'Worker has not exposed a handler of this name';
2541
}
2642

43+
class ErrorWorkerInitializationFailed<T> extends ErrorWorker<T> {
44+
static description =
45+
'Worker failed to initialize with an initialization message';
46+
}
47+
2748
export {
49+
ErrorWorkers,
50+
ErrorWorkersUndefinedBehaviour,
2851
ErrorWorkerManager,
2952
ErrorWorkerManagerDestroyed,
3053
ErrorWorkerPool,
3154
ErrorWorkerPoolInvalidWorkers,
55+
ErrorWorkerPoolWorkerCreationFailed,
56+
ErrorWorkerPoolWorkerTerminated,
3257
ErrorWorker,
3358
ErrorWorkerHandlerMissing,
59+
ErrorWorkerInitializationFailed,
3460
};

src/expose.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type {
22
WorkerTaskInformation,
33
WorkerManifest,
44
WorkerResultInternal,
5-
} from '#types.js';
5+
} from './types.js';
66
import { isMainThread, parentPort } from 'node:worker_threads';
7-
import * as workerErrors from '#errors.js';
7+
import * as workerErrors from './errors.js';
88

99
/**
1010
* Call inside a worker to set it up and expose the supported worker functions.

src/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This is an example worker script
22

3-
import type { WorkerManifest } from '#types.js';
3+
import type { WorkerManifest } from './types.js';
44
import { expose } from './expose.js';
55

66
const worker = {

0 commit comments

Comments
 (0)