Worker tasks
Use @Task to declare work that runs in a worker thread. The framework creates, registers, and closes the worker manager automatically. Declare ordinary dependencies with @Provider and include the task requests in your module’s providers.
// jobs.module.ts
import {Module, Provider, Task, type Runnable, type RunContext} from '@heronjs/common';
@Provider()
class Arithmetic {
square(value: number): number {
return value * value;
}
}
@Provider()
class SquareHandler implements Runnable<Square, number> {
constructor(private readonly arithmetic: Arithmetic) {}
run({input: request, context}: {input: Square; context: RunContext}): number {
context.signal.throwIfAborted();
return this.arithmetic.square(request.value);
}
}
@Task({token: 'square', handler: SquareHandler})
export class Square {
constructor(readonly value: number) {}
}
@Module({providers: [Arithmetic, Square]})
export class JobsModule {}Register the exported module definition file before starting the application:
// main.ts
import {HeronJS} from '@heronjs/core';
import {JobsModule} from './jobs.module.js';
const app = await HeronJS.create({module: JobsModule});
app.threads(new URL('./jobs.module.js', import.meta.url));
await app.start(); // Use listen({port: 3000}) when the application also serves HTTP.
// Keep the application alive for its work, then await app.close() on shutdown.Keep task definitions separate from the executable application bootstrap: a worker imports the definition file and must not start the application again. Register sources before start() or listen(). Each task module receives a pool; task tokens must be unique within that module.
Request data must support structured cloning. Worker dependencies are independent instances, and task handlers use context.signal for cancellation. Existing @Service classes continue to run on the main thread. No manual manager construction or extra DI decorator is needed for task registration.
Dispatch a task
import {ThreadManager} from '@heronjs/common';
import {firstValueFrom} from 'rxjs';
import {Square} from './jobs.module.js';
// Resolve after app.start() or app.listen().
const threads = app.container.resolve(ThreadManager);
const result = await firstValueFrom(threads.run({request: new Square(12)}));
if (typeof result !== 'number') throw new TypeError('Expected a numeric result');run returns a cold Observable: each subscription submits a new job, and unsubscribing cancels it. Awaiting run directly does not execute the job; use firstValueFrom for async/await. Pass timeout and signal under options. Validate returned results at the worker boundary.
For explicit task dispatch, use threads.run({pool, task, input, options}). Supply input even when its value is undefined. Do not combine request with pool, task, or input; invalid calls report errors on subscription. run$ is removed in 4.6.0.
Worker handlers receive {input, context}; custom worker setup callbacks receive {container, config}. Handlers may return a value, Promise, or Observable.
Configure generated pools with @ThreadManager({taskDefaults: {minWorkers: 1, maxWorkers: 4, maxQueue: 64}}) on the root module, importing that decorator from @heronjs/core. The runtime manager class is imported from common or thread. Do not combine a fixed workers value with elastic limits. Applications with no tasks or manager decorator do not create a worker manager.
Bundled applications
Compile exported task definition files as independently importable ESM with the normal TypeScript build. Register their compiled output URLs with app.threads(...) before startup. If bundling the application, keep task definitions as separate ESM entries and preserve their exports. The custom thread build plugins are no longer available.