@5minds/node-red-contrib-processcube 1.6.0-develop-624ecd-m4somph0 → 1.6.0-process-instances-delete-HOTFIX-de8a1e-m4v6cinx
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/package.json +1 -1
- package/processinstance-delete.js +40 -39
package/package.json
CHANGED
@@ -1,69 +1,70 @@
|
|
1
1
|
module.exports = function (RED) {
|
2
2
|
function ProcessInstanceDelete(config) {
|
3
3
|
RED.nodes.createNode(this, config);
|
4
|
-
|
4
|
+
const node = this;
|
5
5
|
|
6
6
|
node.on('input', async function (msg) {
|
7
7
|
node.engine = RED.nodes.getNode(config.engine);
|
8
|
-
const client = node.engine.engineClient;
|
8
|
+
const client = node.engine ? node.engine.engineClient : null;
|
9
9
|
|
10
|
-
if (!client) {
|
11
|
-
node.error('No engine configured.');
|
10
|
+
if (!client || !client.processInstances) {
|
11
|
+
node.error('No engine or processInstances API configured.');
|
12
12
|
return;
|
13
13
|
}
|
14
14
|
|
15
|
-
|
16
|
-
if (
|
17
|
-
|
18
|
-
|
19
|
-
timeMultiplier = config.time_unit == 'hours' ? 1 : 24;
|
15
|
+
const timeToUse = msg.payload.duration || config.duration;
|
16
|
+
if (!timeToUse || isNaN(timeToUse) || timeToUse <= 0) {
|
17
|
+
node.error('Invalid duration: must be a positive number.');
|
18
|
+
return;
|
20
19
|
}
|
21
20
|
|
22
|
-
const
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
: undefined
|
27
|
-
: config.modelid != ''
|
28
|
-
? config.modelid
|
29
|
-
: undefined;
|
21
|
+
const isHours = msg.payload.time_unit
|
22
|
+
? msg.payload.time_unit.toLowerCase() === 'hours'
|
23
|
+
: config.time_unit.toLowerCase() === 'hours';
|
24
|
+
const multiplier = isHours ? 1 : 24;
|
30
25
|
|
31
|
-
const
|
26
|
+
const deletionDate = new Date(Date.now() - timeToUse * multiplier * 60 * 60 * 1000);
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
const modelId = msg.payload.processModelId?.trim() || config.modelid?.trim();
|
29
|
+
if (!modelId) {
|
30
|
+
node.error('processModelId is not defined or empty.');
|
31
|
+
return;
|
32
|
+
}
|
37
33
|
|
38
|
-
|
34
|
+
const batchSize = config.batch_size || 100;
|
39
35
|
|
40
|
-
|
36
|
+
try {
|
37
|
+
const result = await client.processInstances.query(
|
38
|
+
{ processModelId: modelId,
|
39
|
+
finishedBefore: deletionDate,
|
40
|
+
state: ['finished', 'error', 'terminated'],
|
41
|
+
},
|
42
|
+
{ identity: node.engine.identity }
|
43
|
+
);
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
return
|
46
|
-
}
|
45
|
+
if (result.length === 0) {
|
46
|
+
node.log('No process instances to delete.');
|
47
|
+
node.send(msg);
|
48
|
+
return;
|
49
|
+
}
|
47
50
|
|
48
|
-
const ids =
|
51
|
+
const ids = result.map((obj) => obj.processInstanceId);
|
49
52
|
|
50
|
-
msg.payload = {
|
51
|
-
successfulDeletions: [],
|
52
|
-
failedDeletions: []
|
53
|
-
};
|
53
|
+
msg.payload = { successfulDeletions: [], failedDeletions: [] };
|
54
54
|
|
55
55
|
for (let i = 0; i < ids.length; i += batchSize) {
|
56
56
|
const batch = ids.slice(i, i + batchSize);
|
57
57
|
try {
|
58
|
-
await client.processInstances.deleteProcessInstances(batch, true, engine.identity);
|
59
|
-
msg.payload.successfulDeletions.push(...batch);
|
58
|
+
await client.processInstances.deleteProcessInstances(batch, true, node.engine.identity);
|
59
|
+
msg.payload.successfulDeletions.push(...batch);
|
60
60
|
} catch (deleteError) {
|
61
|
-
batch.forEach(id => {
|
62
|
-
msg.payload.failedDeletions.push({ id, error: deleteError.message });
|
61
|
+
batch.forEach((id) => {
|
62
|
+
msg.payload.failedDeletions.push({ id, error: deleteError.message });
|
63
63
|
});
|
64
64
|
node.warn(`Failed to delete process instances in batch: ${batch.join(', ')}. Error: ${deleteError.message}`);
|
65
65
|
}
|
66
66
|
}
|
67
|
+
|
67
68
|
node.send(msg);
|
68
69
|
} catch (queryError) {
|
69
70
|
node.error(`Failed to query process instances: ${queryError.message}`);
|
@@ -72,4 +73,4 @@ module.exports = function (RED) {
|
|
72
73
|
}
|
73
74
|
|
74
75
|
RED.nodes.registerType('processinstance-delete', ProcessInstanceDelete);
|
75
|
-
};
|
76
|
+
};
|