Skip to content

Commit

Permalink
Merge pull request #951 from mountaindude/942
Browse files Browse the repository at this point in the history
942
  • Loading branch information
mountaindude authored Jan 29, 2024
2 parents c07a2e1 + 6c909da commit adeb0e9
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 147 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
- name: Build binaries
run: |
pwd
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${DIST_FILE_NAME}" -t node18-macos-x64 ./build.cjs --config package.json --compress GZip
chmod +x "${DIST_FILE_NAME}"
Expand Down Expand Up @@ -257,7 +257,7 @@ jobs:
- name: Build binaries
run: |
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${env:DIST_FILE_NAME}.exe" -t node18-win-x64 ./build.cjs --config package.json --compress GZip
# Sign the executable
Expand Down Expand Up @@ -358,7 +358,7 @@ jobs:
- name: Build binaries
run: |
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${DIST_FILE_NAME}" -t node18-linux-x64 ./build.cjs --config package.json --compress GZip
chmod +x ${DIST_FILE_NAME}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/insiders-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
include:
- os: winsrv-2016
build: |
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${env:DIST_FILE_NAME}.exe" -t node18-win-x64 ./build.cjs --config package.json --compress GZip
dir
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
artifact_insider: butler--win-x64--${{ github.sha }}.zip
- os: mac-build1
build: |
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${DIST_FILE_NAME}" -t node18-macos-x64 ./build.cjs --config package.json --compress GZip
chmod +x "${DIST_FILE_NAME}"
Expand Down Expand Up @@ -140,7 +140,7 @@ jobs:
artifact_insider: butler--macos-x64--${{ github.sha }}.zip
- os: ubuntu-latest
build: |
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify
./node_modules/.bin/esbuild src/butler.js --bundle --external:axios --external:xdg-open --external:enigma.js --outfile=build.cjs --format=cjs --platform=node --target=node18 --minify --inject:./src/lib/import-meta-url.js --define:import.meta.url=import_meta_url
pkg --output "./${DIST_FILE_NAME}" -t node18-linux-x64 ./build.cjs --config package.json --compress GZip
chmod +x ${DIST_FILE_NAME}
Expand Down
10 changes: 5 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"NODE_ENV": "production"
},
"args": [
// "--configfile",
// "config/production.yaml",
"--configfile",
"config/production.yaml",
// "config/config-gen-api-docs.yaml",
// "--new-relic-account-name",
// "'First NR account'",
Expand All @@ -26,9 +26,9 @@
// "--new-relic-api-key",
// "APIKEY",

"-c",
"./config/config-gen-api-docs.yaml",
"--no-qs-connection"
// "-c",
// "./config/config-gen-api-docs.yaml",
// "--no-qs-connection"
],
"outFiles": ["${workspaceFolder}/**/*.js"]
}
Expand Down
44 changes: 27 additions & 17 deletions src/butler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@
// Add dependencies
import dgram from 'dgram';

// Load code from sub modules

// Load globals dynamically/async to ensure singleton pattern works
const globalModule = await import('./globals.js');
const globals = globalModule.default;
const start = async () => {
// Load code from sub modules
// Load globals dynamically/async to ensure singleton pattern works
const globals = (await import('./globals.js')).default;

const setupServiceMonitorTimer = (await import('./lib/service_monitor.js')).default;
const setupServiceMonitorTimer = (await import('./lib/service_monitor.js')).default;

// The build function creates a new instance of the App class and returns it.
const build = (await import('./app.js')).default;
// The build function creates a new instance of the App class and returns it.
const build = (await import('./app.js')).default;

const udpInitTaskErrorServer = (await import('./udp/udp_handlers.js')).default;
const mqttInitHandlers = (await import('./lib/mqtt_handlers.js')).default;
const udpInitTaskErrorServer = (await import('./udp/udp_handlers.js')).default;
const mqttInitHandlers = (await import('./lib/mqtt_handlers.js')).default;

import {
configFileStructureAssert,
configFileYamlAssert,
configFileNewRelicAssert,
configFileInfluxDbAssert,
} from './lib/assert/assert_config_file.js';
const { configFileStructureAssert, configFileYamlAssert, configFileNewRelicAssert, configFileInfluxDbAssert } = await import(
'./lib/assert/assert_config_file.js'
);

const start = async () => {
// Verify correct structure of config file
configFileStructureAssert(globals.config, globals.logger);

Expand All @@ -42,6 +37,21 @@ const start = async () => {
configFileInfluxDbAssert(globals.config, globals.configQRS, globals.logger);
}

// Ensure that initialisation of globals is complete
// Sleep 5 seconds otherwise to llow globals to be initialised

function sleepLocal(ms) {
// eslint-disable-next-line no-promise-executor-return
return new Promise((resolve) => setTimeout(resolve, ms));
}

if (!globals.initialised) {
globals.logger.info('START: Sleeping 5 seconds to allow globals to be initialised.');
await sleepLocal(5000);
} else {
globals.logger.info('START: Globals initialised, all good.');
}

const apps = await build({});

const { restServer } = apps;
Expand Down
21 changes: 19 additions & 2 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import si from 'systeminformation';
import isUncPath from 'is-unc-path';
import winston from 'winston';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';

// Add dependencies
import { Command, Option } from 'commander';
Expand All @@ -23,10 +24,17 @@ class Settings {
}

// Get app version from package.json file
const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const { version } = loadJSON('../package.json');
const filename = `./package.json`;
const a = upath.resolve(filename)
const b = readFileSync(a);
const { version } = JSON.parse(b);

// const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
// const { version } = loadJSON('../package.json');
this.appVersion = version;

console.log(`appVersion: ${this.appVersion}`);

// Command line parameters
const program = new Command();
program
Expand Down Expand Up @@ -419,6 +427,9 @@ class Settings {

// this.mqttClient,

// Indicate that we have finished initialising
this.initialised = true;

// eslint-disable-next-line no-constructor-return
return instance;
})();
Expand Down Expand Up @@ -640,6 +651,12 @@ class Settings {
const dbName = this.config.get('Butler.influxDb.dbName');
const enableInfluxdb = this.config.get('Butler.influxDb.enable');

// Ensure that InfluxDB has been created
if(this.influx === undefined) {
this.logger.error('CONFIG: InfluxDB not initialized! Possible race condition during startup of Butler. Exiting.');
process.exit(1);
}

if (enableInfluxdb) {
this.influx
.getDatabaseNames()
Expand Down
1 change: 1 addition & 0 deletions src/lib/import-meta-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var import_meta_url = require('url').pathToFileURL(__filename);
30 changes: 25 additions & 5 deletions src/routes/sense_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import httpErrors from 'http-errors';
import enigma from 'enigma.js';
import SenseUtilities from 'enigma.js/sense-utilities.js';
import WebSocket from 'ws';
import { createRequire } from "module";
// import { createRequire } from "module";
import { readFile } from 'fs/promises';
import upath from 'upath';


// Load global variables and functions
import globals from '../globals.js';
Expand All @@ -15,10 +18,27 @@ import apiPutAppReload from '../api/sense_app.js';

async function handlerPutAppReload(request, reply) {
try {
const schemaFile = `../../node_modules/enigma.js/schemas/${globals.configEngine.engineVersion}.json`;
const require = createRequire(import.meta.url);
// eslint-disable-next-line import/no-dynamic-require
const qixSchema = require(schemaFile);
const schemaFile = `./node_modules/enigma.js/schemas/${globals.configEngine.engineVersion}.json`;
// const require = createRequire(import.meta.url);
// // eslint-disable-next-line import/no-dynamic-require
// const qixSchema = require(schemaFile);

// Convert schemaFile to absolute path using path
const a = upath.resolve(schemaFile)
const b = await readFile(schemaFile);
const qixSchema = JSON.parse(b);


// const qixSchema = JSON.parse(
// await readFile(
// // new URL(schemaFile, import.meta.url);
// new URL(schemaFile, import.meta.url);
// )
// );

// const { createRequire } = require('node:module');
// const qixSchema = createRequire(schemaFile);


logRESTCall(request);

Expand Down
90 changes: 37 additions & 53 deletions src/routes/sense_app_dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ import serializeApp from 'serializeapp';
import httpErrors from 'http-errors';
import enigma from 'enigma.js';
import WebSocket from 'ws';
import { createRequire } from "module";
// import { createRequire } from "module";
import { readFile } from 'fs/promises';
import upath from 'upath';


import globals from '../globals.js';
import { logRESTCall } from '../lib/log_rest_call.js';
import { apiGetSenseAppDump, apiGetAppDump } from '../api/sense_app_dump.js';

// Set up enigma.js configuration

function handlerGetSenseAppDump(request, reply) {
async function handlerGetSenseAppDump(request, reply) {
try {
const schemaFile = `../../node_modules/enigma.js/schemas/${globals.configEngine.engineVersion}.json`;
const require = createRequire(import.meta.url);
// eslint-disable-next-line import/no-dynamic-require
const qixSchema = require(schemaFile);
const schemaFile = `./node_modules/enigma.js/schemas/${globals.configEngine.engineVersion}.json`;


// const require = createRequire(import.meta.url);
// // eslint-disable-next-line import/no-dynamic-require
// const qixSchema = require(schemaFile);


// Convert schemaFile to absolute path using path
const a = upath.resolve(schemaFile)
const b = await readFile(a);
const qixSchema = JSON.parse(b);


// const { createRequire } = require('node:module');
// const qixSchema = createRequire(schemaFile);

logRESTCall(request);

Expand All @@ -42,53 +57,22 @@ function handlerGetSenseAppDump(request, reply) {
};

const session = enigma.create(configEnigma);
session
.open()
.then((global) => {
// We can now interact with the global object, for example get the document list.
// Please refer to the Engine API documentation for available methods.

global
.openDoc(request.params.appId, '', '', '', true)
.then((app) => serializeApp(app))
.then((data) => {
reply.type('application/json; charset=utf-8').code(200).send(JSON.stringify(data));

// Close connection to Sense server
try {
session.close();
} catch (err) {
globals.logger.error(`APPDUMP: Error closing connection to Sense engine: ${JSON.stringify(err, null, 2)}`);
reply.send(httpErrors(500, 'Failed closing connection to Sense server'));
}
})
.catch((error) => {
globals.logger.error(`APPDUMP: Error while opening doc during app dump: ${JSON.stringify(error, null, 2)}`);

try {
session.close();
} catch (err) {
globals.logger.error(`APPDUMP: Error closing connection to Sense engine: ${JSON.stringify(err, null, 2)}`);
reply.send(httpErrors(500, 'Error closing connection to Sense server'));
}

reply.send(httpErrors(422, 'Failed to open session to Sense engine'));
});
})
.catch((error) => {
globals.logger.error(
`APPDUMP: Error while opening session to Sense engine during app dump: ${JSON.stringify(error, null, 2)}`
);

try {
session.close();
} catch (err) {
globals.logger.error(`APPDUMP: Error closing connection to Sense engine: ${JSON.stringify(err, null, 2)}`);
reply.send(httpErrors(500, 'Error closing connection to Sense server'));
}

reply.send(httpErrors(422, 'Failed to open session to Sense engine'));
});
const global = await session.open();

// We can now interact with the global object, for example get the document list.
// Please refer to the Engine API documentation for available methods.
const app = await global.openDoc(request.params.appId, '', '', '', true);
const data = await serializeApp(app);

reply.type('application/json; charset=utf-8').code(200).send(JSON.stringify(data));

// Close connection to Sense server
try {
await session.close();
} catch (err) {
globals.logger.error(`APPDUMP: Error closing connection to Sense engine: ${JSON.stringify(err, null, 2)}`);
reply.send(httpErrors(500, 'Failed closing connection to Sense server'));
}
}
} catch (err) {
globals.logger.error(`APPDUMP: Failed dumping app: ${request.params.appId}, error is: ${JSON.stringify(err, null, 2)}`);
Expand Down
Loading

0 comments on commit adeb0e9

Please sign in to comment.