Notifications
Notifications ermöglichen es, auf Ereignisse in der ProcessCube® Engine zu reagieren. Sie können sich auf verschiedene Events registrieren und werden benachrichtigt, wenn diese auftreten.
Grundlegendes Beispiel
import { Client } from '@processcube/client';
const client = new Client('http://localhost:8000');
// Auf Prozess-Deployment reagieren
client.onProcessDeployed((notification) => {
console.log(`Prozess deployed: ${notification.processDefinitionId}`);
});
// Auf Prozess-Start reagieren
client.onProcessStarted((notification) => {
console.log(`Prozess gestartet: ${notification.processInstanceId}`);
});
// Auf Prozess-Ende reagieren
client.onProcessEnded((notification) => {
console.log(`Prozess beendet: ${notification.processInstanceId}`);
});Notifications arbeiten event-basiert über WebSockets. Sie erhalten Benachrichtigungen in Echtzeit.
Prozess-Notifications
Deployment Events
// Prozess wurde deployed
client.onProcessDeployed((notification) => {
console.log(`Prozess deployed: ${notification.processDefinitionId}`);
console.log(`Prozessmodell-ID: ${notification.processModelId}`);
});
// Prozess wurde undeployed
client.onProcessUndeployed((notification) => {
console.log(`Prozess entfernt: ${notification.processDefinitionId}`);
});
// Prozess ist jetzt ausführbar
client.onProcessIsExecutableChanged((notification) => {
console.log(`Prozess ${notification.processModelId} ausführbar: ${notification.isExecutable}`);
});Lifecycle Events
// Prozess startet
client.onProcessStarting((notification) => {
console.log(`Prozess startet: ${notification.processInstanceId}`);
});
// Prozess wurde gestartet
client.onProcessStarted((notification) => {
console.log(`Prozess gestartet: ${notification.processInstanceId}`);
console.log(`Gestartet um: ${notification.timestamp}`);
});
// Prozess ist beendet
client.onProcessEnded((notification) => {
console.log(`Prozess beendet: ${notification.processInstanceId}`);
});
// Prozess wurde terminiert
client.onProcessTerminated((notification) => {
console.log(`Prozess terminiert: ${notification.processInstanceId}`);
});
// Prozess hat einen Fehler
client.onProcessError((notification) => {
console.log(`Prozess-Fehler: ${notification.processInstanceId}`);
console.error(`Fehler: ${notification.error}`);
});Metadata Events
// Prozess-Owner wurde geändert
client.onProcessOwnerChanged((notification) => {
console.log(`Neuer Owner: ${notification.ownerId}`);
console.log(`Prozess: ${notification.processDefinitionId}`);
});
// Prozess-Instanz-Metadata wurde geändert
client.onProcessInstanceMetadataChanged((notification) => {
console.log(`Metadata geändert: ${JSON.stringify(notification.changedMetadata)}`);
});
// Prozess-Instanzen wurden gelöscht
client.onProcessInstancesDeleted((notification) => {
console.log(`Gelöschte Instanzen: ${notification.processInstanceIds.join(', ')}`);
});User Task Notifications
// User Task wartet auf Bearbeitung
client.onUserTaskWaiting((notification) => {
console.log(`User Task wartet: ${notification.flowNodeInstanceId}`);
console.log(`Task-Name: ${notification.flowNodeId}`);
});
// User Task wurde abgeschlossen
client.onUserTaskFinished((notification) => {
console.log(`User Task abgeschlossen: ${notification.flowNodeInstanceId}`);
});
// User Task wurde reserviert
client.onUserTaskReserved((notification) => {
console.log(`User Task reserviert von: ${notification.userId}`);
});External Task Notifications
// External Task erstellt
client.onExternalTaskCreated((notification) => {
console.log(`External Task erstellt: ${notification.flowNodeInstanceId}`);
console.log(`Topic: ${notification.topic}`);
});
// External Task wurde gesperrt
client.onExternalTaskLocked((notification) => {
console.log(`External Task gesperrt: ${notification.flowNodeInstanceId}`);
console.log(`Gesperrt bis: ${notification.lockExpirationTime}`);
});
// External Task abgeschlossen
client.onExternalTaskFinished((notification) => {
console.log(`External Task fertig: ${notification.flowNodeInstanceId}`);
});Activity Notifications
// Activity erreicht
client.onActivityReached((notification) => {
console.log(`Activity erreicht: ${notification.flowNodeId}`);
console.log(`Typ: ${notification.flowNodeType}`);
});
// Activity beendet
client.onActivityFinished((notification) => {
console.log(`Activity beendet: ${notification.flowNodeId}`);
});Gateway Notifications
// Gateway erreicht
client.onGatewayReached((notification) => {
console.log(`Gateway: ${notification.flowNodeId}`);
console.log(`Typ: ${notification.gatewayType}`);
});
// Gateway beendet
client.onGatewayFinished((notification) => {
console.log(`Gateway durchlaufen: ${notification.flowNodeId}`);
});Event Notifications
Intermediate Events
// Intermediate Catch Event erreicht
client.onIntermediateCatchEventReached((notification) => {
console.log(`Warte auf Event: ${notification.flowNodeId}`);
});
// Intermediate Throw Event ausgelöst
client.onIntermediateThrowEventTriggered((notification) => {
console.log(`Event ausgelöst: ${notification.flowNodeId}`);
});Boundary Events
// Boundary Event ausgelöst
client.onBoundaryEventTriggered((notification) => {
console.log(`Boundary Event: ${notification.flowNodeId}`);
console.log(`Attached to: ${notification.attachedToActivity}`);
});Praktische Anwendungen
Monitoring Dashboard
class ProcessMonitor {
private stats = {
started: 0,
finished: 0,
errors: 0,
active: 0,
};
constructor(private client: Client) {
this.setupListeners();
}
private setupListeners() {
this.client.onProcessStarted(() => {
this.stats.started++;
this.stats.active++;
this.logStats();
});
this.client.onProcessEnded(() => {
this.stats.finished++;
this.stats.active--;
this.logStats();
});
this.client.onProcessError(() => {
this.stats.errors++;
this.stats.active--;
this.logStats();
});
}
private logStats() {
console.log('=== Prozess-Statistik ===');
console.log(`Gestartet: ${this.stats.started}`);
console.log(`Beendet: ${this.stats.finished}`);
console.log(`Fehler: ${this.stats.errors}`);
console.log(`Aktiv: ${this.stats.active}`);
}
}
const monitor = new ProcessMonitor(client);Automatische Benachrichtigungen
import * as nodemailer from 'nodemailer';
const transporter = nodemailer.createTransporter({
// SMTP-Konfiguration
});
// Bei Prozess-Fehlern E-Mail senden
client.onProcessError(async (notification) => {
await transporter.sendMail({
from: 'alerts@processcube.io',
to: 'admin@example.com',
subject: `Prozess-Fehler: ${notification.processInstanceId}`,
text: `Ein Prozess ist fehlgeschlagen:\n\nProzess-Instanz: ${notification.processInstanceId}\nZeitpunkt: ${notification.timestamp}`,
});
});Workflow-Automation
// Automatisch nächsten Schritt auslösen
client.onUserTaskFinished(async (notification) => {
console.log(`User Task abgeschlossen: ${notification.flowNodeInstanceId}`);
// Automatisch nächste Aktion triggern
if (notification.flowNodeId === 'ApprovalTask') {
await client.sendSignal('ApprovalCompleted', {
taskId: notification.flowNodeInstanceId,
timestamp: new Date(),
});
}
});Logging und Audit Trail
import * as fs from 'fs';
const auditLog = fs.createWriteStream('audit.log', { flags: 'a' });
function logEvent(eventType: string, notification: any) {
const logEntry = {
timestamp: new Date().toISOString(),
eventType,
data: notification,
};
auditLog.write(JSON.stringify(logEntry) + '\n');
}
client.onProcessStarted((n) => logEvent('process.started', n));
client.onProcessEnded((n) => logEvent('process.ended', n));
client.onUserTaskFinished((n) => logEvent('usertask.finished', n));Best Practices
Fehlerbehandlung
client.onProcessError((notification) => {
try {
handleProcessError(notification);
} catch (error) {
console.error('Fehler beim Verarbeiten der Notification:', error);
}
});Filtern nach Prozess
const targetProcessKey = 'OrderProcess';
client.onProcessStarted((notification) => {
// Nur für bestimmten Prozess reagieren
if (notification.processModelId === targetProcessKey) {
console.log('Order Process gestartet!');
}
});Connection Management
// Bei Verbindungsverlust
client.onConnectionLost(() => {
console.warn('Verbindung zur Engine verloren');
});
// Verbindung wiederhergestellt
client.onConnectionRestored(() => {
console.log('Verbindung wiederhergestellt');
});Nächste Schritte
- Prozesse starten - Prozesse starten und überwachen
- User Tasks - User Tasks mit Notifications kombinieren
- External Tasks - External Task Worker mit Notifications
Tipp: Kombinieren Sie Notifications mit External Tasks für vollautomatische Workflows.