mirror of
https://github.com/unraid/api.git
synced 2026-02-07 08:28:56 -06:00
fix: publish registation event on key file update
This commit is contained in:
@@ -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());
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user