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.
- package/backend/tasks/taskManager.js +10 -3
- package/backend/tasks/utils.js +18 -6
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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
|
};
|
package/backend/tasks/utils.js
CHANGED
|
@@ -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
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
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;
|