From e9889f56557cbf7f7d80e5ab66fcd95763dbea19 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Fri, 30 Apr 2021 07:59:25 +0930 Subject: [PATCH] fix: ensure varState is watched for registration events --- app/core/watchers/index.ts | 4 +- app/core/watchers/key-file.ts | 76 ---------------- app/core/watchers/registration.ts | 91 +++++++++++++++++++ .../watchers/{states.ts => state-files.ts} | 0 4 files changed, 93 insertions(+), 78 deletions(-) delete mode 100644 app/core/watchers/key-file.ts create mode 100644 app/core/watchers/registration.ts rename app/core/watchers/{states.ts => state-files.ts} (100%) diff --git a/app/core/watchers/index.ts b/app/core/watchers/index.ts index 56284e23a..98a344137 100644 --- a/app/core/watchers/index.ts +++ b/app/core/watchers/index.ts @@ -1,4 +1,4 @@ -export * from './key-file'; +export * from './registration'; export * from './myservers'; export * from './plugins'; -export * from './states'; +export * from './state-files'; diff --git a/app/core/watchers/key-file.ts b/app/core/watchers/key-file.ts deleted file mode 100644 index f74a57254..000000000 --- a/app/core/watchers/key-file.ts +++ /dev/null @@ -1,76 +0,0 @@ -/*! - * Copyright 2021 Lime Technology Inc. All rights reserved. - * Written by: Alexis Tyler - */ - -import { dirname } from 'path'; -import chokidar from 'chokidar'; -import { coreLogger, logger } from '../log'; -import { varState } from '../states'; -import { pubsub } from '../pubsub'; -import { getKeyFile } from '../utils'; - -export const keyFile = () => { - const watchers: chokidar.FSWatcher[] = []; - - return { - start() { - // Watch the directory where the key file is meant to reside - // This is to ensure we get the key file even if it changes names - // For example if someone has a Basic.key and then it changes to Pro.key - const keyDirectory = dirname(varState.data.regFile); - - const watcher = chokidar.watch(keyDirectory, { - persistent: true, - ignoreInitial: true - }); - - coreLogger.debug('Loading watchers for %s', keyDirectory); - - // Key file has possibly changed - watcher.on('all', async function (event, fullPath) { - try { - // Ensure this is a key file - // @todo Check if varState is updated here if so for an exact match - // we can check if the path is varState.data.regFile - if (varState.data.regFile !== '' && fullPath !== varState.data.regFile) { - return; - } - - logger.debug('Checking "%s" for the key file.', varState.data.regFile); - - // Get key file - const keyFile = await getKeyFile(); - const registration = { - guid: varState.data.regGuid, - type: varState.data.regTy.toUpperCase(), - state: varState.data.regState, - keyFile: { - location: fullPath, - contents: keyFile - } - }; - - logger.debug('Publishing %s to registration', JSON.stringify(registration, null, 2)); - - // Publish event - // This will end up going to the graphql endpoint - await pubsub.publish('registration', { - registration - }).catch(error => { - coreLogger.error('Failed publishing to "registration" with %s', error); - }); - - // Log for debugging - coreLogger.debug('Registration file %s has emitted %s event.', fullPath, event); - } catch {} - }); - - // Save ref for cleanup - watchers.push(watcher); - }, - stop() { - watchers.forEach(async watcher => watcher.close()); - } - }; -}; diff --git a/app/core/watchers/registration.ts b/app/core/watchers/registration.ts new file mode 100644 index 000000000..b1eb13903 --- /dev/null +++ b/app/core/watchers/registration.ts @@ -0,0 +1,91 @@ +/*! + * Copyright 2021 Lime Technology Inc. All rights reserved. + * Written by: Alexis Tyler + */ + +import { dirname } from 'path'; +import chokidar from 'chokidar'; +import { coreLogger, logger } from '../log'; +import { varState } from '../states'; +import { pubsub } from '../pubsub'; +import { getKeyFile } from '../utils'; +import { bus } from '../bus'; + +const processChange = async function (fullPath: string) { + try { + // Ensure this is a key file + // @todo Check if varState is updated here if so for an exact match + // we can check if the path is varState.data.regFile + if (varState.data.regFile !== '' && fullPath !== varState.data.regFile) { + return; + } + + logger.debug('Checking "%s" for the key file.', varState.data.regFile); + + // Get key file + const keyFile = await getKeyFile(); + const registration = { + guid: varState.data.regGuid, + type: varState.data.regTy.toUpperCase(), + state: varState.data.regState, + keyFile: { + location: fullPath, + contents: keyFile + } + }; + + logger.debug('Publishing %s to registration', JSON.stringify(registration, null, 2)); + + // Publish event + // This will end up going to the graphql endpoint + await pubsub.publish('registration', { + registration + }).catch(error => { + coreLogger.error('Failed publishing to "registration" with %s', error); + }); + } catch {} +}; + +export const keyFile = () => { + const watchers: chokidar.FSWatcher[] = []; + + return { + start() { + // Watch the directory where the key file is meant to reside + // This is to ensure we get the key file even if it changes names + // For example if someone has a Basic.key and then it changes to Pro.key + const keyDirectory = dirname(varState.data.regFile); + + const watcher = chokidar.watch(keyDirectory, { + persistent: true, + ignoreInitial: true + }); + + coreLogger.debug('Loading watchers for %s', keyDirectory); + + // Key file has possibly changed + watcher.on('all', async (event, filePath) => { + // Log for debugging + coreLogger.debug('Registration file %s has emitted %s event.', filePath, event); + + // Process the changes + await processChange(filePath); + }); + + // Save ref for cleanup + watchers.push(watcher); + + // Update registration when regTy, regCheck, etc changes + bus.on('varstate', async data => { + // Log for debugging + coreLogger.debug('Var state updated, publishing registration event.'); + + // Process the changes + await processChange(data.regFile); + }); + }, + stop() { + watchers.forEach(async watcher => watcher.close()); + } + }; +}; diff --git a/app/core/watchers/states.ts b/app/core/watchers/state-files.ts similarity index 100% rename from app/core/watchers/states.ts rename to app/core/watchers/state-files.ts