Add file access checks option with pause node

This commit is contained in:
HaveAGitGat 2023-10-17 07:07:01 +01:00
parent 759a873c30
commit 6b1157763f
2 changed files with 256 additions and 14 deletions

View file

@ -1,11 +1,14 @@
import { promises as fs } from 'fs';
import {
IpluginDetails,
IpluginInputArgs,
IpluginOutputArgs,
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
import { getFileAbosluteDir } from '../../../../FlowHelpers/1.0.0/fileUtils';
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
const details = ():IpluginDetails => ({
const details = (): IpluginDetails => ({
name: 'Input File',
description: 'Start the flow with an input file',
style: {
@ -17,7 +20,34 @@ const details = ():IpluginDetails => ({
requiresVersion: '2.11.01',
sidebarPosition: -1,
icon: '',
inputs: [],
inputs: [
{
name: 'fileAccessChecks',
type: 'boolean',
defaultValue: 'false',
inputUI: {
type: 'dropdown',
options: [
'false',
'true',
],
},
tooltip: 'Will check if input file and cache are readable and writable',
},
{
name: 'pauseNodeIfAccessChecksFail',
type: 'boolean',
defaultValue: 'false',
inputUI: {
type: 'dropdown',
options: [
'false',
'true',
],
},
tooltip: 'This will pause the node if the file access checks fail',
},
],
outputs: [
{
number: 1,
@ -27,11 +57,70 @@ const details = ():IpluginDetails => ({
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const plugin = (args:IpluginInputArgs):IpluginOutputArgs => {
const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
const lib = require('../../../../../methods/lib')();
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
args.inputs = lib.loadDefaultValues(args.inputs, details);
const orignalFolder = getFileAbosluteDir(args.originalLibraryFile._id);
const { fileAccessChecks, pauseNodeIfAccessChecksFail } = args.inputs;
const nodeID = process.argv[8];
const { serverIP, serverPort } = args.deps.configVars.config;
const url = `http://${serverIP}:${serverPort}/api/v2/update-node`;
const pauseNode = async () => {
args.jobLog('Pausing node');
const requestConfig = {
method: 'post',
url,
headers: {},
data: {
data: {
nodeID,
nodeUpdates: {
nodePaused: true,
},
},
},
};
await args.deps.axios(requestConfig);
args.jobLog('Node paused');
};
const checkReadWrite = async (location: string) => {
try {
await fs.access(location, fs.constants.R_OK);
} catch (err) {
args.jobLog(JSON.stringify(err));
if (pauseNodeIfAccessChecksFail) {
await pauseNode();
}
throw new Error(`Location not readable:${location}`);
}
try {
await fs.access(location, fs.constants.W_OK);
} catch (err) {
args.jobLog(JSON.stringify(err));
if (pauseNodeIfAccessChecksFail) {
await pauseNode();
}
throw new Error(`Location not writeable:${location}`);
}
};
if (fileAccessChecks) {
args.jobLog('Checking file access');
await checkReadWrite(orignalFolder);
await checkReadWrite(args.librarySettings.cache);
} else {
args.jobLog('Skipping file access checks');
}
return {
outputFileObj: args.inputFileObj,
outputNumber: 1,