backend: Add error handling for Redis connection in TaskSchedulerService and SystemEventService

This commit is contained in:
Carlos Santos 2025-04-08 11:18:57 +02:00
parent 67322f31d7
commit 8bf3c51125
3 changed files with 17 additions and 0 deletions

View File

@ -51,6 +51,7 @@ export class RedisService extends EventEmitter {
const onError = (error: Error) => { const onError = (error: Error) => {
this.logger.error('Redis Error', error); this.logger.error('Redis Error', error);
this.emit('redisError', error);
}; };
const onDisconnect = () => { const onDisconnect = () => {
@ -84,6 +85,10 @@ export class RedisService extends EventEmitter {
this.on('redisConnected', callback); this.on('redisConnected', callback);
} }
public onceError(callback: () => void) {
this.once('redisError', callback);
}
/** /**
* Publishes a message to a specified Redis channel. * Publishes a message to a specified Redis channel.
* *

View File

@ -72,6 +72,10 @@ export class SystemEventService {
this.redisService.onReady(callback); this.redisService.onReady(callback);
} }
onceRedisError(callback: () => void) {
this.redisService.onceError(callback);
}
/** /**
* Handles incoming messages from Redis, parses them as system events, * Handles incoming messages from Redis, parses them as system events,
* and emits the corresponding event to all registered listeners. * and emits the corresponding event to all registered listeners.

View File

@ -32,6 +32,14 @@ export class TaskSchedulerService {
this.scheduleTask(task); this.scheduleTask(task);
}); });
this.started = true; this.started = true;
this.systemEventService.onceRedisError(() => {
this.logger.debug('Redis shutdown detected. Cancelling all scheduled tasks...');
this.scheduledTasks.forEach((task, name) => {
this.cancelTask(name);
});
this.started = false;
});
}); });
} }