import { BackoffOptions } from 'exponential-backoff'; import { Instance } from '../instance/Instance'; /** * A cluster manages a number of instances and accepts tasks to be submitted to those instances. * It allows multiple async tasks to be submitted in parallel in a throttled way to preserve resource usage. */ export declare class Cluster { /** * The instances managed by this cluster mapped to whether they are free to pick up new tasks. */ protected instances: Set>; /** * How many instances the cluster can contain (and, conversely, how many tasks it can run in parallel). */ private maxInstances; /** * The function to call to create a new instance, when needed. */ private instanceCreator; /** * The cluster options. See {@link ClusterOptions} */ private clusterOptions?; /** * The cluster state. */ private state; /** * The instances whose creation is ongoing and the cluster is waiting on. */ private instancesBeingCreated; /** * Constructor. * * @param clusterSize how many instances the cluster can contain (and, conversely, how many tasks it can run in parallel). * @param instanceCreator the function to call to create a new instance, when needed. * @param defaultBackoffOptions the exponential-backoff options this cluster will use by default for retrying the acquisition of a free instance and for performing a graceful shutdown. */ constructor(clusterSize: number, instanceCreator: () => Instance | Promise>, clusterOptions?: ClusterOptions); private createInitialInstances; /** * Submits a task to the cluster. * The cluster will attempt to acquire a free instance and execute the task within the context of that instance. * * @param task the task to run. * @param backoffOptions the exponential-backoff options to retry acquiring a free instance. If not provided, the cluster will use its default ones. * @returns a promise that completes when the task is done. */ submit(task: (i: C) => Promise, backoffOptions?: ClusterBackoffOptions): Promise; /** * Attempts to gracefully shut down the cluster by waiting for any ongoing task to be completed before shutting down all instances. * After the configured retries are exhausted, the cluster will initiate a forceful shutdown (see {@link shutdownNow}) * * @param backoffOptions the exponential-backoff options to retry the graceful shutdown. If not provided, the cluster will use its default ones. * @returns a promise that completes when the cluster has been successfully shutdown. */ shutdown(backoffOptions?: ClusterBackoffOptions): Promise; /** * Immediately initiates a cluster shutdown, shutting down all managed instances, without waiting for ongoing tasks to be finished. * * @returns a promise that completes when the cluster has been successfully shutdown. */ shutdownNow(): Promise; /** * Performs the shutdown operations. * * @returns a promise that completes when the cluster has been successfully shutdown. */ protected performShutdown(): Promise; /** * Attempts to acquire a instance. If no instance is free and the cluster is not yet full, it will return a newly spawned instance. * * @param backoffOptions the exponential-backoff options to retry acquiring a free instance. If not provided, the cluster will use its default ones. * @returns a promise that resolves to the acquired instance. */ private acquire; private createNewInstance; private getNewInstancePromise; private getBackoffOptions; } /** * Options for the Cluster retries (instance acquisition & graceful shutdown). */ export type ClusterBackoffOptions = Omit; /** * Options for the Cluster behavior. */ export interface ClusterOptions { /** * The exponential-backoff options this cluster will use by default for retrying the acquisition of a free instance and for performing a graceful shutdown. */ defaultBackoffOptions?: ClusterBackoffOptions; /** * Whether to create instances eagerly or lazily. */ eagerInstances?: boolean; }