@5minds/node-red-contrib-processcube
Advanced tools
Comparing version 1.6.0-develop-624ecd-m4somph0 to 1.6.0-process-instances-delete-HOTFIX-4de3b9-m4v609ad
{ | ||
"name": "@5minds/node-red-contrib-processcube", | ||
"version": "1.6.0-develop-624ecd-m4somph0", | ||
"version": "1.6.0-process-instances-delete-HOTFIX-4de3b9-m4v609ad", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Node-RED nodes for ProcessCube", |
module.exports = function (RED) { | ||
function ProcessInstanceDelete(config) { | ||
RED.nodes.createNode(this, config); | ||
var node = this; | ||
const node = this; | ||
node.on('input', async function (msg) { | ||
node.engine = RED.nodes.getNode(config.engine); | ||
const client = node.engine.engineClient; | ||
const client = node.engine ? node.engine.engineClient : null; | ||
if (!client) { | ||
node.error('No engine configured.'); | ||
if (!client || !client.processInstances) { | ||
node.error('No engine or processInstances API configured.'); | ||
return; | ||
} | ||
let timeMultiplier; | ||
if (msg.payload.time_unit) { | ||
timeMultiplier = msg.payload.time_unit == 'hours' ? 1 : 24; | ||
} else { | ||
timeMultiplier = config.time_unit == 'hours' ? 1 : 24; | ||
const timeToUse = msg.payload.duration || config.duration; | ||
if (!timeToUse || isNaN(timeToUse) || timeToUse <= 0) { | ||
node.error('Invalid duration: must be a positive number.'); | ||
return; | ||
} | ||
const timeToUse = msg.payload.duration ? msg.payload.duration : config.duration; | ||
const modelId = msg.payload.processModelId | ||
? msg.payload.processModelId != '' | ||
? msg.payload.processModelId | ||
: undefined | ||
: config.modelid != '' | ||
? config.modelid | ||
: undefined; | ||
const isHours = msg.payload.time_unit | ||
? msg.payload.time_unit.toLowerCase() === 'hours' | ||
: config.time_unit.toLowerCase() === 'hours'; | ||
const multiplier = isHours ? 1 : 24; | ||
const batchSize = config.batch_size || 100; // Konfigurierbare Batchgröße, Standardwert 100 | ||
const deletionDate = new Date(Date.now() - timeToUse * multiplier * 60 * 60 * 1000); | ||
const modelId = msg.payload.processModelId?.trim() || config.modelid?.trim(); | ||
if (!modelId) { | ||
node.error('processModelId is not defined or empty.'); | ||
return; | ||
} | ||
const batchSize = config.batch_size || 100; | ||
try { | ||
const result = await client.processInstances.query({ | ||
processModelId: modelId | ||
}, { identity: node.engine.identity }); | ||
const result = await client.processInstances.query( | ||
{ processModelId: modelId, finishedBefore: deletionDate }, | ||
{ identity: node.engine.identity } | ||
); | ||
let allInstances = result.processInstances.filter((instance) => instance.state != 'suspended' && instance.state != 'running'); | ||
const allInstances = result.processInstances.filter( | ||
(instance) => instance.state !== 'suspended' && instance.state !== 'running' | ||
); | ||
const today = new Date(); | ||
if (allInstances.length === 0) { | ||
node.log('No process instances to delete.'); | ||
node.send(msg); | ||
return; | ||
} | ||
const oldTasks = allInstances.filter((instance) => { | ||
const finishedDate = new Date(instance.finishedAt); | ||
const diffInHours = (today - finishedDate) / (1000 * 60 * 60); | ||
return diffInHours > Number(timeToUse) * timeMultiplier; | ||
}); | ||
const ids = allInstances.map((obj) => obj.processInstanceId); | ||
const ids = oldTasks.map((obj) => obj.processInstanceId); | ||
msg.payload = { successfulDeletions: [], failedDeletions: [] }; | ||
msg.payload = { | ||
successfulDeletions: [], | ||
failedDeletions: [] | ||
}; | ||
for (let i = 0; i < ids.length; i += batchSize) { | ||
const batch = ids.slice(i, i + batchSize); | ||
try { | ||
await client.processInstances.deleteProcessInstances(batch, true, engine.identity); | ||
msg.payload.successfulDeletions.push(...batch); // Erfolgreiche IDs hinzufügen | ||
await client.processInstances.deleteProcessInstances(batch, true, node.engine.identity); | ||
msg.payload.successfulDeletions.push(...batch); | ||
} catch (deleteError) { | ||
batch.forEach(id => { | ||
msg.payload.failedDeletions.push({ id, error: deleteError.message }); // Fehler protokollieren | ||
batch.forEach((id) => { | ||
msg.payload.failedDeletions.push({ id, error: deleteError.message }); | ||
}); | ||
@@ -67,2 +68,3 @@ node.warn(`Failed to delete process instances in batch: ${batch.join(', ')}. Error: ${deleteError.message}`); | ||
} | ||
node.send(msg); | ||
@@ -76,2 +78,2 @@ } catch (queryError) { | ||
RED.nodes.registerType('processinstance-delete', ProcessInstanceDelete); | ||
}; | ||
}; |
515120
9862