backend: Add error handling for Redis connection in TaskSchedulerService and SystemEventService
This commit is contained in:
parent
67322f31d7
commit
8bf3c51125
@ -51,6 +51,7 @@ export class RedisService extends EventEmitter {
|
||||
|
||||
const onError = (error: Error) => {
|
||||
this.logger.error('Redis Error', error);
|
||||
this.emit('redisError', error);
|
||||
};
|
||||
|
||||
const onDisconnect = () => {
|
||||
@ -84,6 +85,10 @@ export class RedisService extends EventEmitter {
|
||||
this.on('redisConnected', callback);
|
||||
}
|
||||
|
||||
public onceError(callback: () => void) {
|
||||
this.once('redisError', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes a message to a specified Redis channel.
|
||||
*
|
||||
|
||||
@ -72,6 +72,10 @@ export class SystemEventService {
|
||||
this.redisService.onReady(callback);
|
||||
}
|
||||
|
||||
onceRedisError(callback: () => void) {
|
||||
this.redisService.onceError(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles incoming messages from Redis, parses them as system events,
|
||||
* and emits the corresponding event to all registered listeners.
|
||||
|
||||
@ -32,6 +32,14 @@ export class TaskSchedulerService {
|
||||
this.scheduleTask(task);
|
||||
});
|
||||
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;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user