-
Notifications
You must be signed in to change notification settings - Fork 0
/
processinstance-delete.js
103 lines (86 loc) · 4.49 KB
/
processinstance-delete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module.exports = function (RED) {
function ProcessInstanceDelete(config) {
RED.nodes.createNode(this, config);
const node = this;
node.on('input', async function (msg) {
node.engine = RED.nodes.getNode(config.engine);
const client = node.engine ? node.engine.engineClient : null;
if (!client || !client.processInstances) {
node.error('No engine or processInstances API configured.');
return;
}
const timeToUse = msg.payload.duration || config.duration;
if (!timeToUse || isNaN(timeToUse) || timeToUse <= 0) {
node.error('Invalid duration: must be a positive number.');
return;
}
// Gültige Werte für time_type
const validTimeTypes = ['days', 'hours'];
const timeType = msg.payload.time_type
? msg.payload.time_type.toLowerCase()
: config.time_type?.toLowerCase();
// time_type validieren
if (!timeType || !validTimeTypes.includes(timeType)) {
node.error(`Invalid time_type provided: ${timeType}. Allowed values are 'days' or 'hours'.`);
return;
}
// Zeitmultiplikator berechnen
const multiplier = timeType === 'hours' ? 1 : 24;
node.log(`Time type: ${timeType}`);
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;
}
// Prüfung und Festlegung von batch_size
let batchSize = msg.payload.batch_size || config.batch_size || 1000;
if (isNaN(batchSize) || batchSize <= 0 || batchSize > 1000) {
node.error(`Invalid batch_size: ${batchSize}. Must be a positive number and not exceed 1000.`);
return;
}
batchSize = Math.min(batchSize, 1000); // Sicherstellen, dass der Wert 1000 nicht überschreitet
try {
msg.payload = { successfulDeletions: [], failedDeletions: [] };
let hasMoreResults = true;
let sumSuccessful = 0;
let sumFailed = 0;
while (hasMoreResults) {
const result = await client.processInstances.query(
{
processModelId: modelId,
finishedBefore: deletionDate.toISOString(),
state: ['finished', 'error', 'terminated'],
limit: batchSize,
},
{ includeXml: false }
);
const processInstances = result.processInstances || [];
if (processInstances.length === 0) {
node.log(`No more process instances to delete for Model-ID: ${modelId} with Date: ${deletionDate.toISOString()}`);
hasMoreResults = false;
continue;
}
const ids = processInstances.map((obj) => obj.processInstanceId);
try {
await client.processInstances.deleteProcessInstances(ids, true);
msg.payload.successfulDeletions.push(...ids);
sumSuccessful += ids.length;
} catch (deleteError) {
var message = JSON.stringify(deleteError);
sumFailed += ids.length;
ids.forEach((id) => {
msg.payload.failedDeletions.push({ id, error: message });
});
node.warn(`Failed to delete some process instances for Model-ID: ${modelId}. Error: ${message}`);
}
}
node.log(`Successfully deleted ${sumSuccessful} process instances and ${sumFailed} failed to delete process instances for Model-ID: ${modelId}.`);
node.send(msg);
} catch (queryError) {
node.error(`Failed to query process instances for Model-ID: ${modelId}. Error: ${queryError.message}`);
}
});
}
RED.nodes.registerType('processinstance-delete', ProcessInstanceDelete);
};