@push.rocks/taskbuffer 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6130,136 +6130,109 @@ var AbstractDistributedCoordinator = class {
6130
6130
 
6131
6131
  // ts/taskbuffer.classes.taskmanager.ts
6132
6132
  var TaskManager = class {
6133
- constructor(optionsArg = {}) {
6133
+ constructor(options = {}) {
6134
6134
  this.randomId = isounique2.uni();
6135
6135
  this.taskMap = new dist_ts_exports5.ObjectMap();
6136
6136
  this.cronJobManager = new dist_ts_exports6.CronManager();
6137
6137
  this.options = {
6138
6138
  distributedCoordinator: null
6139
6139
  };
6140
- this.options = Object.assign(this.options, optionsArg);
6140
+ this.options = Object.assign(this.options, options);
6141
6141
  }
6142
- /**
6143
- * checks if a task is already present
6144
- * @param taskNameArg
6145
- */
6146
- getTaskByName(taskNameArg) {
6147
- return this.taskMap.findSync((itemArg) => {
6148
- return itemArg.name === taskNameArg;
6149
- });
6142
+ getTaskByName(taskName) {
6143
+ return this.taskMap.findSync((task) => task.name === taskName);
6150
6144
  }
6151
- /**
6152
- * adds a Task to the TaskManager
6153
- * @param taskArg
6154
- */
6155
- addTask(taskArg) {
6156
- if (!taskArg.name) {
6157
- throw new Error("taskArg needs a name to be added to taskManager");
6145
+ addTask(task) {
6146
+ if (!task.name) {
6147
+ throw new Error("Task must have a name to be added to taskManager");
6158
6148
  }
6159
- this.taskMap.add(taskArg);
6149
+ this.taskMap.add(task);
6160
6150
  }
6161
- /**
6162
- * adds and schedules a task at once
6163
- * @param taskArg
6164
- * @param cronStringArg
6165
- */
6166
- addAndScheduleTask(taskArg, cronStringArg) {
6167
- this.addTask(taskArg);
6168
- this.scheduleTaskByName(taskArg.name, cronStringArg);
6151
+ addAndScheduleTask(task, cronString) {
6152
+ this.addTask(task);
6153
+ this.scheduleTaskByName(task.name, cronString);
6169
6154
  }
6170
- /**
6171
- * triggers a task in the TaskManagerByName
6172
- * @param taskNameArg
6173
- */
6174
- triggerTaskByName(taskNameArg) {
6175
- const taskToTrigger = this.getTaskByName(taskNameArg);
6155
+ async triggerTaskByName(taskName) {
6156
+ const taskToTrigger = this.getTaskByName(taskName);
6176
6157
  if (!taskToTrigger) {
6177
- throw new Error(`There is no task with the name of ${taskNameArg}`);
6158
+ throw new Error(`No task with the name ${taskName} found.`);
6178
6159
  }
6179
6160
  return taskToTrigger.trigger();
6180
6161
  }
6181
6162
  async triggerTask(task) {
6182
6163
  return task.trigger();
6183
6164
  }
6184
- /**
6185
- * schedules the task by name
6186
- * @param taskNameArg
6187
- */
6188
- scheduleTaskByName(taskNameArg, cronStringArg) {
6189
- const taskToSchedule = this.getTaskByName(taskNameArg);
6165
+ scheduleTaskByName(taskName, cronString) {
6166
+ const taskToSchedule = this.getTaskByName(taskName);
6167
+ if (!taskToSchedule) {
6168
+ throw new Error(`No task with the name ${taskName} found.`);
6169
+ }
6170
+ this.handleTaskScheduling(taskToSchedule, cronString);
6171
+ }
6172
+ handleTaskScheduling(task, cronString) {
6190
6173
  const cronJob = this.cronJobManager.addCronjob(
6191
- cronStringArg,
6192
- async (triggerTimeArg) => {
6193
- console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`);
6194
- console.log(
6195
- `task >>${taskToSchedule.name}<< is ${taskToSchedule.buffered ? `buffered with max ${taskToSchedule.bufferMax} buffered calls` : `unbuffered`}`
6196
- );
6174
+ cronString,
6175
+ async (triggerTime) => {
6176
+ this.logTaskState(task);
6197
6177
  if (this.options.distributedCoordinator) {
6198
- console.log(`Found a distrubuted coordinator, performing distributed consultation.`);
6199
- const announcementResult = await this.options.distributedCoordinator.fireDistributedTaskRequest({
6200
- submitterRandomId: this.randomId,
6201
- status: "requesting",
6202
- taskExecutionParallel: 1,
6203
- taskExecutionTime: triggerTimeArg,
6204
- taskExecutionTimeout: taskToSchedule.timeout,
6205
- taskName: taskToSchedule.name,
6206
- taskVersion: taskToSchedule.version
6207
- });
6178
+ const announcementResult = await this.performDistributedConsultation(task, triggerTime);
6208
6179
  if (!announcementResult.shouldTrigger) {
6209
- console.log("distributed coordinator result: NOT EXECUTING");
6180
+ console.log("Distributed coordinator result: NOT EXECUTING");
6210
6181
  return;
6211
6182
  } else {
6212
- console.log("distributed coordinator result: CHOSEN AND EXECUTING");
6183
+ console.log("Distributed coordinator result: CHOSEN AND EXECUTING");
6213
6184
  }
6214
6185
  }
6215
- await taskToSchedule.trigger();
6186
+ await task.trigger();
6216
6187
  }
6217
6188
  );
6218
- taskToSchedule.cronJob = cronJob;
6189
+ task.cronJob = cronJob;
6190
+ }
6191
+ logTaskState(task) {
6192
+ console.log(`Taskbuffer schedule triggered task >>${task.name}<<`);
6193
+ const bufferState = task.buffered ? `buffered with max ${task.bufferMax} buffered calls` : `unbuffered`;
6194
+ console.log(`Task >>${task.name}<< is ${bufferState}`);
6195
+ }
6196
+ async performDistributedConsultation(task, triggerTime) {
6197
+ console.log("Found a distributed coordinator, performing consultation.");
6198
+ return this.options.distributedCoordinator.fireDistributedTaskRequest({
6199
+ submitterId: this.randomId,
6200
+ status: "requesting",
6201
+ taskExecutionParallel: 1,
6202
+ taskExecutionTime: triggerTime,
6203
+ taskExecutionTimeout: task.timeout,
6204
+ taskName: task.name,
6205
+ taskVersion: task.version
6206
+ });
6219
6207
  }
6220
- /**
6221
- * deschedules a task by name
6222
- * @param taskNameArg
6223
- */
6224
- descheduleTaskByName(taskNameArg) {
6225
- const taskToDeSchedule = this.getTaskByName(taskNameArg);
6226
- if (taskToDeSchedule.cronJob) {
6227
- this.cronJobManager.removeCronjob(taskToDeSchedule.cronJob);
6228
- taskToDeSchedule.cronJob = null;
6208
+ descheduleTaskByName(taskName) {
6209
+ const task = this.getTaskByName(taskName);
6210
+ if (task && task.cronJob) {
6211
+ this.cronJobManager.removeCronjob(task.cronJob);
6212
+ task.cronJob = null;
6229
6213
  }
6230
6214
  if (this.cronJobManager.cronjobs.isEmpty) {
6231
6215
  this.cronJobManager.stop();
6232
6216
  }
6233
6217
  }
6234
- /**
6235
- * deschedules a task
6236
- * @param task
6237
- */
6238
6218
  async descheduleTask(task) {
6239
6219
  await this.descheduleTaskByName(task.name);
6240
6220
  }
6241
- /**
6242
- * returns the schedule of a specific task
6243
- * @param taskNameArg
6244
- */
6245
- getScheduleForTaskName(taskNameArg) {
6246
- const task = this.getTaskByName(taskNameArg);
6247
- if (!task || !task.cronJob) {
6248
- return null;
6249
- }
6250
- return task.cronJob.cronExpression;
6221
+ getScheduleForTaskName(taskName) {
6222
+ const task = this.getTaskByName(taskName);
6223
+ return task && task.cronJob ? task.cronJob.cronExpression : null;
6251
6224
  }
6252
- /**
6253
- * starts the taskmanager
6254
- */
6255
- start() {
6225
+ async start() {
6226
+ if (this.options.distributedCoordinator) {
6227
+ await this.options.distributedCoordinator.start();
6228
+ }
6256
6229
  this.cronJobManager.start();
6257
6230
  }
6258
- /**
6259
- * stops the taskmanager
6260
- */
6261
- stop() {
6231
+ async stop() {
6262
6232
  this.cronJobManager.stop();
6233
+ if (this.options.distributedCoordinator) {
6234
+ await this.options.distributedCoordinator.stop();
6235
+ }
6263
6236
  }
6264
6237
  };
6265
6238