Skip to content

WorkersProvider

This file defines the WorkersProvider class, a provider for interacting with worker controllers and managing worker tasks in the @sodacore/core library. It integrates with the Workers module to dispatch tasks, retrieve worker instances, and monitor queue sizes.


Features

  • Task Dispatching: Sends method calls to worker controllers by UID.
  • Worker Retrieval: Gets all worker instances for a given controller.
  • Queue Monitoring: Provides queue size information per controller and in total.
  • Dependency Injection: Uses @Inject and @Provide decorators for DI integration.

Usage

typescript
import WorkersProvider from './provider/workers';

const workersProvider = new WorkersProvider();

// Dispatch a method call to a worker controller
await workersProvider.dispatch('worker-uid', 'methodName', [param1, param2]);

// Get all worker instances for a controller
const workers = workersProvider.getWorkers('worker-uid');

// Get queue sizes
const perController = workersProvider.getQueueSizePerController();
const totalQueue = workersProvider.getTotalQueueSize();

API

Methods

async dispatch(uid: string, method: string, params: unknown[] = []): Promise<any>

Dispatches a method call to the worker controller with the specified UID.

  • uid: The unique identifier for the worker controller.
  • method: The method name to call on the worker.
  • params: An array of parameters to pass to the method.
  • Returns: The result of the worker method call.

getWorkers(uid: string): any[]

Returns all worker instances for the specified controller UID.

  • uid: The unique identifier for the worker controller.
  • Returns: An array of worker instances, or an empty array if not found.

getQueueSizePerController(): Record<string, number>

Returns an object mapping each controller UID to its current queue size.

getTotalQueueSize(): number

Returns the total number of queued tasks across all worker controllers.


Notes

  • This provider is designed to be used within the Sodacore dependency injection system.
  • Useful for monitoring and interacting with worker pools and their task queues.

Released under the Apache-2.0 License.