Prozess-Instanz neu starten
Diese Seite zeigt, wie Sie terminierte Prozess-Instanzen erneut starten können.
Grundlegendes Beispiel
import { Client } from '@processcube/client';
const client = new Client('http://localhost:8000');
async function restartInstance() {
const processInstanceId = 'instance-123';
console.log('Terminiere Prozess-Instanz');
await client.terminateProcessInstance(processInstanceId);
console.log('Starte Prozess-Instanz neu');
await client.retryProcessInstance(processInstanceId);
console.log('Prozess-Instanz wurde neu gestartet');
}
restartInstance().catch(console.error);retryProcessInstance() startet eine terminierte Prozess-Instanz von Anfang an neu. Alle bisherigen Ausführungen werden dabei überschrieben.
Verwendungszwecke
Fehlerhafte Prozesse neu starten
// Fehlerhafte Instanzen finden
const errorInstances = await client.getProcessInstances({
processKey: 'OrderProcess',
state: 'error',
});
for (const instance of errorInstances) {
try {
// Terminieren und neu starten
await client.terminateProcessInstance(instance.processInstanceId);
await client.retryProcessInstance(instance.processInstanceId);
console.log(`✓ Instanz ${instance.processInstanceId} neu gestartet`);
} catch (error) {
console.error(`✗ Fehler bei ${instance.processInstanceId}:`, error.message);
}
}Automatisches Retry bei Fehlern
async function autoRetry(processInstanceId: string, maxRetries: number = 3) {
let attempts = 0;
while (attempts < maxRetries) {
try {
// Starte Prozess neu
await client.retryProcessInstance(processInstanceId);
// Warte auf Abschluss
await new Promise((resolve) => setTimeout(resolve, 5000));
// Prüfe Status
const instances = await client.getProcessInstances({ processInstanceId });
const instance = instances[0];
if (instance.state === 'finished') {
console.log('Prozess erfolgreich abgeschlossen');
return true;
}
if (instance.state === 'error') {
attempts++;
console.log(`Versuch ${attempts}/${maxRetries} fehlgeschlagen`);
if (attempts < maxRetries) {
await client.terminateProcessInstance(processInstanceId);
}
}
} catch (error) {
console.error('Retry fehlgeschlagen:', error.message);
return false;
}
}
console.error(`Prozess nach ${maxRetries} Versuchen nicht erfolgreich`);
return false;
}Best Practices
Status vor Retry prüfen
async function safeRetry(processInstanceId: string) {
const instances = await client.getProcessInstances({ processInstanceId });
const instance = instances[0];
if (!instance) {
throw new Error('Prozess-Instanz nicht gefunden');
}
if (instance.state === 'running') {
console.log('Instanz läuft bereits');
return false;
}
if (instance.state === 'finished') {
console.log('Instanz ist bereits erfolgreich beendet');
return false;
}
// Retry nur bei terminated oder error
if (instance.state === 'terminated' || instance.state === 'error') {
await client.retryProcessInstance(processInstanceId);
console.log('Prozess neu gestartet');
return true;
}
return false;
}Retry mit Logging
async function retryWithLogging(processInstanceId: string) {
console.log(`[${new Date().toISOString()}] Starte Retry für ${processInstanceId}`);
try {
// Aktuellen Status loggen
const instances = await client.getProcessInstances({ processInstanceId });
const instance = instances[0];
console.log(`Aktueller Status: ${instance.state}`);
// Retry durchführen
await client.retryProcessInstance(processInstanceId);
console.log('Retry erfolgreich');
// Neuen Status prüfen
await new Promise((resolve) => setTimeout(resolve, 2000));
const newInstances = await client.getProcessInstances({ processInstanceId });
const newInstance = newInstances[0];
console.log(`Neuer Status: ${newInstance.state}`);
return true;
} catch (error) {
console.error('Retry fehlgeschlagen:', error.message);
return false;
}
}Batch-Retry
async function retryMultiple(processInstanceIds: string[]) {
const results = [];
for (const id of processInstanceIds) {
try {
await client.retryProcessInstance(id);
results.push({ id, success: true });
console.log(`✓ ${id}`);
} catch (error) {
results.push({ id, success: false, error: error.message });
console.error(`✗ ${id}: ${error.message}`);
}
}
const successCount = results.filter((r) => r.success).length;
console.log(`\nErgebnis: ${successCount}/${results.length} erfolgreich`);
return results;
}Fehlerbehandlung
try {
await client.retryProcessInstance(processInstanceId);
console.log('Retry erfolgreich');
} catch (error) {
if (error.code === 'INSTANCE_NOT_FOUND') {
console.error('Prozess-Instanz existiert nicht');
} else if (error.code === 'INVALID_STATE') {
console.error('Prozess-Instanz kann nicht neu gestartet werden');
} else {
console.error('Retry fehlgeschlagen:', error.message);
}
}Nächste Schritte
- Prozess-Instanzen abfragen - Status überwachen
- Prozess-Instanzen beenden - Laufende Prozesse stoppen
- Prozesse starten - Neue Instanzen erstellen