psdev-task-manager 1.1.1 → 1.1.4

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.
@@ -1,4 +1,4 @@
1
- const { withDuration } = require('psdev-utils');
1
+ const { withDuration, withSuccessRateLogs } = require('psdev-utils');
2
2
 
3
3
  const { TASK_STATUS, CRON_JOB_MAX_DURATION_SEC } = require('./consts');
4
4
  const { scheduleChildTasksAndUpdateParent } = require('./parentChildTasks/handler');
@@ -73,7 +73,11 @@ function taskManager() {
73
73
  if (saveResults) {
74
74
  await saveTaskResultToAdditionalCollection(task, TASK_STATUS.FAILED, { error });
75
75
  }
76
- throw new Error(errMsg);
76
+ const isFailedAfterAllRetries = amountOfRetries >= TASK_MAX_TRIES;
77
+ if (isFailedAfterAllRetries) {
78
+ //Only throw error if task is permanently failed, otherwise we have retry mechanism
79
+ throw new Error(errMsg);
80
+ }
77
81
  }
78
82
  },
79
83
  console
@@ -132,7 +136,10 @@ function taskManager() {
132
136
  return {
133
137
  schedule: insertNewTask,
134
138
  scheduleInBulk: bulkInsertTasks,
135
- processTask,
139
+ processTask : (...args) => {
140
+ const requestName = `processTask_${args[0].name}`; //To get success rate analysis per task
141
+ return withSuccessRateLogs(requestName, () => processTask(...args));
142
+ },
136
143
  runScheduledTasks: (...args) =>
137
144
  withDuration('runScheduledTasks', () => runScheduledTasks(...args), console),
138
145
  };
@@ -171,14 +171,26 @@ const filterScheduledTasksByStatus = (tasks, tasksConfig) => {
171
171
  console.log(
172
172
  `filterScheduledTasksByStatus: tasks count: ${tasks.length} tasksConfig: ${JSON.stringify(tasksConfig)}`
173
173
  );
174
- const filtered = tasks.filter(task => {
175
- if (task.status === TASK_STATUS.IN_PROGRESS) {
176
- //Only include parent tasks that are in progress
177
- console.log(`filterScheduledTasksByStatus: task: ${JSON.stringify(task)}`);
178
- console.log(`filterScheduledTasksByStatus: tasksConfig: ${JSON.stringify(tasksConfig)}`);
174
+ const shouldIncludeTask = (task, tasksConfig) => {
175
+ console.log(`filterScheduledTasksByStatus: task: ${JSON.stringify(task)}`);
176
+ console.log(
177
+ `filterScheduledTasksByStatus: taskConfig: ${JSON.stringify(taskConfig)}`
178
+ );
179
+ const { status } = task;
180
+ if (status === TASK_STATUS.IN_PROGRESS) {
181
+ //Only include parent tasks that are in progress, to continue running next children
179
182
  return isParentTask(task, tasksConfig[task.name]);
180
183
  }
181
- return [TASK_STATUS.PENDING, TASK_STATUS.FAILED].includes(task.status);
184
+ if (status === TASK_STATUS.FAILED) {
185
+ // Exclude if it's a parent task, if parent task failed, we don't retry it anymore
186
+ return !isParentTask(task, taskConfig);
187
+ }
188
+ // Include if status is PENDING
189
+ return status === TASK_STATUS.PENDING;
190
+ };
191
+ const filtered = tasks.filter(task => {
192
+ const taskConfig = tasksConfig[task.name];
193
+ return shouldIncludeTask(task, taskConfig);
182
194
  });
183
195
  console.log(`filterScheduledTasksByStatus: filtered count: ${filtered.length}`);
184
196
  return filtered;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "psdev-task-manager",
3
- "version": "1.1.1",
3
+ "version": "1.1.4",
4
4
  "description": "Task manager library",
5
5
  "keywords": [
6
6
  "task-manager"