UNPKG

status-sharding

Version:

Welcome to Status Sharding! This package is designed to provide an efficient and flexible solution for sharding Discord bots, allowing you to scale your bot across multiple processes or workers.

80 lines (79 loc) 5.63 kB
import { ClusterEvents, ClusterKillOptions, EvalOptions, Serialized, Awaitable, ValidIfSerializable, SerializableInput, Serializable } from '../types'; import { BaseMessage, DataType } from '../other/message'; import { Worker as WorkerThread } from 'worker_threads'; import { RefClusterManager } from './clusterManager'; import { ClientRefType } from './clusterClient'; import { ChildProcess } from 'child_process'; import { Worker } from '../classes/worker'; import { Child } from '../classes/child'; import { Guild } from 'discord.js'; import EventEmitter from 'events'; /** A self-contained cluster created by the ClusterManager. */ export declare class Cluster<InternalManager extends RefClusterManager = RefClusterManager, InternalClient extends ClientRefType = ClientRefType> extends EventEmitter { manager: InternalManager; id: number; shardList: number[]; /** Represents whether the cluster is ready. */ ready: boolean; /** Exited. */ exited: boolean; /** Represents the child process/worker of the cluster. */ thread: null | Worker | Child; /** Represents the last time the cluster received a heartbeat. */ lastHeartbeatReceived?: number; /** Message processor that handles messages from the child process/worker/manager. */ private messageHandler?; /** Represents the environment data of the cluster. */ private envData; /** Creates an instance of Cluster. */ constructor(manager: InternalManager, id: number, shardList: number[]); /** Count of shards assigned to this cluster. */ get totalShards(): number; /** Count of clusters managed by the manager. */ get totalClusters(): number; /** Spawn function that spawns the cluster's child process/worker with proper event management. */ spawn(spawnTimeout?: number): Promise<ChildProcess | WorkerThread>; private _setupEventListeners; kill(options?: ClusterKillOptions): Promise<void>; /** Respawn function that respawns the cluster's child process/worker. */ respawn(delay?: number, timeout?: number): Promise<ChildProcess | WorkerThread>; /** Send function that sends a message to the cluster's child process/worker. */ send<T extends Serializable>(message: SerializableInput<T>): Promise<void>; /** Request function that sends a message to the cluster's child process/worker and waits for a response. */ request<T extends Serializable, O>(message: SerializableInput<T>, options?: { timeout?: number; }): Promise<Serialized<O>>; /** Broadcast function that sends a message to all clusters. */ broadcast<T extends Serializable>(message: SerializableInput<T>, sendSelf?: boolean): Promise<void>; /** Eval function that evaluates a script on the current cluster. */ eval<T, P extends object, C = Cluster<InternalManager, InternalClient>>(script: string | ((cluster: C, context: Serialized<P>) => Awaitable<T>), options?: Exclude<EvalOptions<P>, 'cluster'>): Promise<ValidIfSerializable<T>>; /** EvalOnClient function that evaluates a script on a specific cluster. */ evalOnClient<T, P extends object, C = InternalClient>(script: string | ((client: C, context: Serialized<P>) => Awaitable<T>), options?: EvalOptions<P>): Promise<ValidIfSerializable<T>>; /** EvalOnCluster function that evaluates a script on a specific cluster. */ evalOnGuild<T, P extends object, C = InternalClient>(guildId: string, script: string | ((client: C, context: Serialized<P>, guild: Guild | undefined) => Awaitable<T>), options?: EvalOptions<P>): Promise<ValidIfSerializable<T>>; /** Function that allows you to construct your own BaseMessage and send it to the cluster. */ _sendInstance<D extends DataType, A = Serializable, P extends object = object>(message: BaseMessage<D, A, P>): Promise<void>; /** Message handler function that handles messages from the cluster's child process/worker/manager. */ private _handleMessage; /** Exit handler function that handles the cluster's child process/worker exiting. */ private _handleExit; /** Error handler function that handles errors from the cluster's child process/worker/manager. */ private _handleError; /** Handle unexpected disconnection. */ private _handleDisconnect; /** Handle unexpected exit/crash. */ private _handleUnexpectedExit; } export type RefCluster = Cluster; export declare interface Cluster { /** Emit an event. */ emit: (<K extends keyof ClusterEvents>(event: K, ...args: ClusterEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof ClusterEvents>, ...args: unknown[]) => boolean); /** Remove an event listener. */ off: (<K extends keyof ClusterEvents>(event: K, listener: (...args: ClusterEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof ClusterEvents>, listener: (...args: unknown[]) => void) => this); /** Listen for an event. */ on: (<K extends keyof ClusterEvents>(event: K, listener: (...args: ClusterEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof ClusterEvents>, listener: (...args: unknown[]) => void) => this); /** Listen for an event once. */ once: (<K extends keyof ClusterEvents>(event: K, listener: (...args: ClusterEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof ClusterEvents>, listener: (...args: unknown[]) => void) => this); /** Remove all listeners for an event. */ removeAllListeners: (<K extends keyof ClusterEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof ClusterEvents>) => this); }