fix: publish registation event on key file update

This commit is contained in:
Alexis
2021-09-07 16:22:24 +09:30
parent f60cb1a9f5
commit 6340967458

View File

@@ -3,10 +3,14 @@
* Written by: Alexis Tyler
*/
import chokidar from 'chokidar';
import { coreLogger, logger } from '../log';
import { pubsub } from '../pubsub';
import { getKeyFile, sleep } from '../utils';
import { getKeyFile } from '../utils';
import { bus } from '../bus';
import { varState } from '../states';
const fileWatchers: chokidar.FSWatcher[] = [];
export const keyFile = () => {
const listener = async (data: any) => {
@@ -36,13 +40,49 @@ export const keyFile = () => {
});
};
let timeout: NodeJS.Timeout;
return {
start() {
// Update registration when regTy, regCheck, etc changes
bus.on('var', listener);
// Update registration when key file is updated on disk
const watcher = chokidar.watch('/boot/config', {
persistent: true,
ignoreInitial: true,
ignored: (path: string) => !path.endsWith('.key')
});
// Key file has updated, updating registration
watcher.on('all', async () => {
// Reset timeout
clearTimeout(timeout);
// Get key file
const keyFile = varState.data.regFile ? await getKeyFile(varState.data.regFile) : '';
const registration = {
guid: varState.data.regGuid,
type: varState.data.regTy.toUpperCase(),
state: varState.data.regState,
keyFile: {
location: varState.data.regFile,
contents: keyFile
}
};
await pubsub.publish('registration', {
registration
}).catch(error => {
coreLogger.error('Failed publishing to "registration" with %s', error);
});
});
// Save ref for cleanup
fileWatchers.push(watcher);
},
stop() {
bus.removeListener('var', listener);
fileWatchers.forEach(async watcher => watcher.close());
}
};
};