task-sharding
Version:
Shards tasks in a cluster using consistent hashing
115 lines (100 loc) • 4.2 kB
TypeScript
/**
* Configuration for the TaskSharding class.
* @typedef {object} TaskShardingConfig
* @property {number} [delay] The usual delay in milliseconds until the tasks are rebalanced across the nodes when the list of node changes.
* @property {number} [maxDelay] The maximal delay in milliseconds until the tasks are rebalanced across the nodes when the list of node changes.
* @property {Array<string>} [nodes] Initial list of node names that participate in the task sharding. This list should contain the 'selfNode' if present.
* @property {string} [selfNode] The name of this node.
*/
declare type TaskShardingConfig = {
delay: number;
maxDelay: number;
nodes: string[];
selfNode: string;
};
/**
* Shard tasks between nodes in a cluster, using consistent hashing.
* @export
* @class
* @extends EventEmitter
*/
declare class TaskSharding {
constructor(conf?: TaskShardingConfig);
/**
* Sets/replaces the list of nodes that participate in the task sharding.
* @param {string|Array<string>|object} nodes The new replacement node(s). String for one server. Array for multiple servers. Object for mapping between server names and vnode weight number.
* @memberof TaskSharding
*/
setNodes(nodes: string | string[] | any): void;
/**
* Adds one or more nodes to the list of nodes that participate in the task sharding.
* @param {string|Array<string>|object} node The new node(s) that should be added. String for one server. Array for multiple servers. Object for mapping between server names and vnode weight number.
* @memberof TaskSharding
*/
addNode(node: string | string[] | any): void;
/**
* Removes one or more nodes from the list of nodes that participate in the task sharding.
* @param {string|Array<string>|object} node The current node(s) that should be removed. String for one server. Array for multiple servers. Object for mapping between server names and vnode weight number.
* @memberof TaskSharding
*/
removeNode(node: string | string[] | any): void;
/**
* The (self) name of this node/instance. Undefined if not set so far.
* @member {string}
* @readonly
* @memberof TaskSharding
* @return {string}
*/
readonly selfNode: string;
/**
* Removes a task by its id.
* @param {string} id The task id that should be removed.
* @return {boolean} true if the task was removed (since it was present); else false.
* @memberof TaskSharding
*/
removeTask(id: string): boolean;
/**
* Adds a task by its id.
* @param {string} id The task id that should be added.
* @return {string} The owner node name.
* @memberof TaskSharding
*/
addTask(id: string): string;
/**
* Checks if this node is the owner for the given task (tasks not previously added will return false as well).
* @param {string} id The task id that should be checked.
* @return {boolean} true if and only if the selfNode is the owner for the given task; else false.
* @memberof TaskSharding
*/
isOwned(id: string): boolean;
/**
* Gets the owner node name for the task with the given id (task ids not previously added will return undefined).
* @param {string} id The task id of which the owner should be retrieved.
* @return {string} The owner node name of the task. 'undefined' if no such task was added previously.
* @memberof TaskSharding
*/
getOwner(id: string): string;
/**
* Event name when a task gets revoked to this node.
* @function on
* @instance
* @param {string} eventName The name of the event.
* @param {function} listener The callback function
* @memberof TaskSharding
*/
on(eventName: string, listener: any): void;
/**
* Event name when a task gets assigned to this node.
* @member {string}
* @static
* @memberof TaskSharding
*/
static TASK_ASSIGNED_EVENT: string;
/**
* Event name when a task gets revoked to this node.
* @member {string}
* @static
* @memberof TaskSharding
*/
static TASK_REVOKED_EVENT: string;
}