@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/node-red-contrib-processcube",
3
- "version": "1.6.0-develop-624ecd-m4somph0",
3
+ "version": "1.6.0-process-instances-delete-HOTFIX-de8a1e-m4v6cinx",
4
4
  "license": "MIT",
5
5
  "description": "Node-RED nodes for ProcessCube",
6
6
  "scripts": {
@@ -1,69 +1,70 @@
1
1
  module.exports = function (RED) {
2
2
  function ProcessInstanceDelete(config) {
3
3
  RED.nodes.createNode(this, config);
4
- var node = this;
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
- let timeMultiplier;
16
- if (msg.payload.time_unit) {
17
- timeMultiplier = msg.payload.time_unit == 'hours' ? 1 : 24;
18
- } else {
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 timeToUse = msg.payload.duration ? msg.payload.duration : config.duration;
23
- const modelId = msg.payload.processModelId
24
- ? msg.payload.processModelId != ''
25
- ? msg.payload.processModelId
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 batchSize = config.batch_size || 100; // Konfigurierbare Batchgröße, Standardwert 100
26
+ const deletionDate = new Date(Date.now() - timeToUse * multiplier * 60 * 60 * 1000);
32
27
 
33
- try {
34
- const result = await client.processInstances.query({
35
- processModelId: modelId
36
- }, { identity: node.engine.identity });
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
- let allInstances = result.processInstances.filter((instance) => instance.state != 'suspended' && instance.state != 'running');
34
+ const batchSize = config.batch_size || 100;
39
35
 
40
- const today = new Date();
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
- const oldTasks = allInstances.filter((instance) => {
43
- const finishedDate = new Date(instance.finishedAt);
44
- const diffInHours = (today - finishedDate) / (1000 * 60 * 60);
45
- return diffInHours > Number(timeToUse) * timeMultiplier;
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 = oldTasks.map((obj) => obj.processInstanceId);
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); // Erfolgreiche IDs hinzufügen
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 }); // Fehler protokollieren
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
+ };