Skip to content

Commit

Permalink
update etcd service: handle end and error event
Browse files Browse the repository at this point in the history
  • Loading branch information
miaowing committed Jan 15, 2020
1 parent 5238b40 commit c99ca5b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/service/etcd-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class EtcdService implements IService, OnModuleInit, OnModuleDestroy {
}
}

private checkServiceWatcher() {
private checkServiceWatcher(immediate?: boolean) {
setTimeout(async () => {
if (this.watcherWrapper.watcher && !this.watcherWrapper.connected) {
try {
Expand All @@ -171,9 +171,11 @@ export class EtcdService implements IService, OnModuleInit, OnModuleDestroy {
this.logger.error('Service watcher created error.', e);
}

this.checkServiceWatcher();
this.logger.log('Service watcher recreate succeed.');

this.checkServiceWatcher(false);
}
}, 60000);
}, immediate ? 0 : 60000);
}

private async initServicesWatcher() {
Expand All @@ -190,6 +192,18 @@ export class EtcdService implements IService, OnModuleInit, OnModuleDestroy {
this.watcherWrapper.watcher.on('connecting', () => {
this.logger.log('Service watcher connecting...');
});
this.watcherWrapper.watcher.on('end', async () => {
this.logger.error('Service watcher unexpected end and will recreate soon');

this.watcherWrapper.connected = false;
this.checkServiceWatcher(true);
});
this.watcherWrapper.watcher.on('error', async e => {
this.logger.error('Service watcher occur unexpected error and will recreate soon', e.stack);

this.watcherWrapper.connected = false;
this.checkServiceWatcher(true);
});
this.watcherWrapper.watcher.on('data', (res) => {
res.events.forEach(evt => {
const key = evt.kv.key.toString();
Expand Down

0 comments on commit c99ca5b

Please sign in to comment.