torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
81 lines • 3.16 kB
JavaScript
({
// Return type changed to be more general
const: tasksToProcessSet = new Set(),
const: notFoundKeys
} < keyof);
TTasks, string >> ;
[];
const notFoundIds = [];
let requestedByKeyCount = 0;
let requestedByIdCount = 0;
// Process tasks by key
if (filter?.tasksByKey && filter.tasksByKey.length > 0) {
requestedByKeyCount = filter.tasksByKey.length;
filter.tasksByKey.forEach((key) => {
const task = this.getTaskByKey(key);
if (task) {
tasksToProcessSet.add(task);
}
else {
notFoundKeys.push(key);
}
});
}
// Process tasks by ID
if (filter?.tasksById && filter.tasksById.length > 0) {
requestedByIdCount = filter.tasksById.length;
filter.tasksById.forEach((id) => {
const task = this.getTask(id); // Use getTask for ID-based retrieval
if (task) {
tasksToProcessSet.add(task);
}
else {
notFoundIds.push(id);
}
});
}
const tasksToProcessArray = Array.from(tasksToProcessSet);
// If neither filter is provided, or both are empty, get all tasks
if (requestedByKeyCount === 0 &&
requestedByIdCount === 0 &&
(!filter || ((filter.tasksByKey?.length ?? 0) === 0 && (filter.tasksById?.length ?? 0) === 0))) {
// Get all tasks if no specific filter is provided or if filters are empty
// Object.values(this.tasks) will correctly return Array<TTasks[keyof TTasks]>
// which is compatible with Array<Task<any, any, SchemaHandler>>
return Object.values(this.tasks);
}
// Logging for missing tasks
if (notFoundKeys.length > 0 || notFoundIds.length > 0) {
const missingDetails = {};
if (notFoundKeys.length > 0) {
missingDetails.missingKeys = notFoundKeys.map(String);
missingDetails.requestedByKey = requestedByKeyCount;
missingDetails.foundByKey = requestedByKeyCount - notFoundKeys.length;
}
if (notFoundIds.length > 0) {
missingDetails.missingIds = notFoundIds;
missingDetails.requestedById = requestedByIdCount;
missingDetails.foundById = requestedByIdCount - notFoundIds.length;
}
this.logger.warn({
...missingDetails,
totalFound: tasksToProcessArray.length,
context: actionContext,
}, `Some requested tasks for ${actionContext} workers were not found.`);
}
if (tasksToProcessArray.length === 0 && (requestedByKeyCount > 0 || requestedByIdCount > 0)) {
const filterCriteria = [];
if (filter?.tasksByKey?.length) {
filterCriteria.push(`keys: ${filter.tasksByKey.join(', ')}`);
}
if (filter?.tasksById?.length) {
filterCriteria.push(`IDs: ${filter.tasksById.join(', ')}`);
}
this.logger.info({ filter: filterCriteria.length > 0 ? filterCriteria.join('; ') : 'all specified', context: actionContext }, `No matching tasks found to process workers for.`);
}
else if (tasksToProcessArray.length === 0 && requestedByKeyCount === 0 && requestedByIdCount === 0) {
this.logger.info({ context: actionContext }, `No tasks available in the group to process workers for.`);
}
return tasksToProcessArray;
export {};
//# sourceMappingURL=filter-tasks.js.map