torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
47 lines • 2.1 kB
JavaScript
export function filterGroups(server, filter, actionContext = 'processing') {
const groupsToProcessSet = new Set();
const notFoundKeys = [];
let requestedByIdCount = 0;
// Process groups by id
if (filter?.groupsById && filter.groupsById.length > 0) {
requestedByIdCount = filter.groupsById.length;
filter.groupsById.forEach((id) => {
const group = server.taskGroups[id];
if (group) {
groupsToProcessSet.add(group);
}
else {
notFoundKeys.push(id);
}
});
}
const groupsToProcessArray = Array.from(groupsToProcessSet);
// If no filter is provided, or filter is empty, get all groups
if (requestedByIdCount === 0 && (!filter || (filter.groupsById?.length ?? 0) === 0)) {
return Object.values(server.taskGroups);
}
// Logging for missing groups
if (notFoundKeys.length > 0) {
const missingDetails = {};
missingDetails.missingIds = notFoundKeys.map(String);
missingDetails.requestedById = requestedByIdCount;
missingDetails.foundById = requestedByIdCount - notFoundKeys.length;
server.logger.warn({
...missingDetails,
totalFound: groupsToProcessArray.length,
context: actionContext,
}, `Some requested groups for ${actionContext} were not found.`);
}
if (groupsToProcessArray.length === 0 && requestedByIdCount > 0) {
const filterCriteria = [];
if (filter?.groupsById?.length) {
filterCriteria.push(`ids: ${filter.groupsById.map(String).join(', ')}`);
}
server.logger.info({ filter: filterCriteria.length > 0 ? filterCriteria.join('; ') : 'all specified', context: actionContext }, `No matching groups found to ${actionContext}.`);
}
else if (groupsToProcessArray.length === 0 && requestedByIdCount === 0) {
server.logger.info({ context: actionContext }, `No groups available to ${actionContext}.`);
}
return groupsToProcessArray;
}
//# sourceMappingURL=filter-groups.js.map