TaskManager Class
The TaskManager distributed data structure tracks queues of clients that want to exclusively run a task.
It is still experimental and under development. Please do try it out, but expect breaking changes in the future.
Signature
export declare class TaskManager extends SharedObject<ITaskManagerEvents> implements ITaskManager
Extends: SharedObject<ITaskManagerEvents
Implements: ITaskManager
Remarks
### Creation
To create a TaskManager
, call the static create method:
const taskManager = TaskManager.create(this.runtime, id);
### Usage
To volunteer for a task, use the lockTask()
method. This returns a Promise that will resolve once the client has acquired exclusive rights to run the task, or reject if the client is removed from the queue without acquiring the rights.
taskManager.lockTask("NameOfTask")
.then(() => { doTheTask(); })
.catch((err) => { console.error(err); });
To release the rights to the task, use the abandon()
method. The next client in the queue will then get the rights to run the task.
taskManager.abandon("NameOfTask");
To inspect your state in the queue, you can use the queued()
and haveTaskLock()
methods.
if (taskManager.queued("NameOfTask")) {
console.log("This client is somewhere in the queue, potentially even having the lock");
}
if (taskManager.queued("NameOfTask")) {
console.log("This client currently has the rights to run the task");
}
### Eventing
TaskManager
is an EventEmitter
, and will emit events when a task is assigned to the client or released.
taskManager.on("assigned", (taskId: string) => {
console.log(`Client was assigned task: ${taskId}`);
});
taskManager.on("lost", (taskId: string) => {
console.log(`Client released task: ${taskId}`);
});
These can be useful if the logic to volunteer for a task is separated from the logic to perform the task and it's not convenient to pass the Promise around.
Constructors
Constructor | Description |
---|---|
(constructor)(id, runtime, attributes) | Constructs a new task manager. If the object is non-local an id and service interfaces will be provided |
Static Methods
Method | Return Type | Description |
---|---|---|
create(runtime, id) | TaskManager | Create a new TaskManager |
getFactory() | IChannelFactory | Get a factory for TaskManager to register with the data store. |
Methods
Method | Return Type | Description |
---|---|---|
_getTaskQueues() | Map<string, string[]> | |
abandon(taskId) | void | |
applyStashedOp() | void | |
haveTaskLock(taskId) | boolean | |
lockTask(taskId) | Promise<void> | |
queued(taskId) | boolean |
Constructor Details
(constructor)
Constructs a new task manager. If the object is non-local an id and service interfaces will be provided
Signature
constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes);
Parameters
Parameter | Type | Description |
---|---|---|
id | string | optional name of the task queue |
runtime | IFluidDataStoreRuntime | data store runtime the task queue belongs to |
attributes | IChannelAttributes |
Method Details
_getTaskQueues
Signature
_getTaskQueues(): Map<string, string[]>;
Returns
Return type: Map<string, string[]>
abandon
Signature
abandon(taskId: string): void;
Parameters
Parameter | Type | Description |
---|---|---|
taskId | string |
applyStashedOp
Signature
applyStashedOp(): void;
create
Create a new TaskManager
Signature
static create(runtime: IFluidDataStoreRuntime, id?: string): TaskManager;
Parameters
Parameter | Modifiers | Type | Description |
---|---|---|---|
runtime | IFluidDataStoreRuntime | data store runtime the new task queue belongs to | |
id | optional | string | optional name of the task queue |
Returns
newly create task queue (but not attached yet)
Return type: TaskManager
getFactory
Get a factory for TaskManager to register with the data store.
Signature
static getFactory(): IChannelFactory;
Returns
a factory that creates and load TaskManager
Return type: IChannelFactory
haveTaskLock
Signature
haveTaskLock(taskId: string): boolean;
Parameters
Parameter | Type | Description |
---|---|---|
taskId | string |
Returns
Return type: boolean
lockTask
Signature
lockTask(taskId: string): Promise<void>;
Parameters
Parameter | Type | Description |
---|---|---|
taskId | string |
Returns
Return type: Promise<void>
queued
Signature
queued(taskId: string): boolean;
Parameters
Parameter | Type | Description |
---|---|---|
taskId | string |
Returns
Return type: boolean