fix(striker-ui-api): improve receive server screenshot from named pipe

main
Tsu-ba-me 2 years ago
parent eb561d6d39
commit 950a98558f
  1. 70
      striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts

@ -8,7 +8,15 @@ import SERVER_PATHS from '../../consts/SERVER_PATHS';
import { dbQuery, getLocalHostUUID, job } from '../../accessModule'; import { dbQuery, getLocalHostUUID, job } from '../../accessModule';
import { sanitize } from '../../sanitize'; import { sanitize } from '../../sanitize';
import { mkfifo, rm } from '../../shell'; import { mkfifo, rm, stderr, stdout } from '../../shell';
const rmfifo = (path: string) => {
try {
rm(path);
} catch (rmfifoError) {
stderr(`Failed to clean up named pipe; CAUSE: ${rmfifoError}`);
}
};
export const getServerDetail: RequestHandler = (request, response) => { export const getServerDetail: RequestHandler = (request, response) => {
const { serverUUID } = request.params; const { serverUUID } = request.params;
@ -17,7 +25,7 @@ export const getServerDetail: RequestHandler = (request, response) => {
const epoch = Date.now(); const epoch = Date.now();
const isScreenshot = sanitize(ss, 'boolean'); const isScreenshot = sanitize(ss, 'boolean');
console.log( stdout(
`serverUUID=[${serverUUID}],epoch=[${epoch}],isScreenshot=[${isScreenshot}]`, `serverUUID=[${serverUUID}],epoch=[${epoch}],isScreenshot=[${isScreenshot}]`,
); );
@ -27,7 +35,7 @@ export const getServerDetail: RequestHandler = (request, response) => {
`Server UUID must be a valid UUID; got [${serverUUID}]`, `Server UUID must be a valid UUID; got [${serverUUID}]`,
); );
} catch (assertError) { } catch (assertError) {
console.log( stderr(
`Failed to assert value when trying to get server detail; CAUSE: ${assertError}.`, `Failed to assert value when trying to get server detail; CAUSE: ${assertError}.`,
); );
@ -42,14 +50,14 @@ export const getServerDetail: RequestHandler = (request, response) => {
try { try {
requestHostUUID = getLocalHostUUID(); requestHostUUID = getLocalHostUUID();
} catch (subError) { } catch (subError) {
console.log(subError); stderr(String(subError));
response.status(500).send(); response.status(500).send();
return; return;
} }
console.log(`requestHostUUID=[${requestHostUUID}]`); stdout(`requestHostUUID=[${requestHostUUID}]`);
try { try {
[[serverHostUUID]] = dbQuery(` [[serverHostUUID]] = dbQuery(`
@ -57,14 +65,14 @@ export const getServerDetail: RequestHandler = (request, response) => {
FROM servers FROM servers
WHERE server_uuid = '${serverUUID}';`).stdout; WHERE server_uuid = '${serverUUID}';`).stdout;
} catch (queryError) { } catch (queryError) {
console.log(`Failed to get server host UUID; CAUSE: ${queryError}`); stderr(`Failed to get server host UUID; CAUSE: ${queryError}`);
response.status(500).send(); response.status(500).send();
return; return;
} }
console.log(`serverHostUUID=[${serverHostUUID}]`); stdout(`serverHostUUID=[${serverHostUUID}]`);
const imageFileName = `${serverUUID}_screenshot_${epoch}`; const imageFileName = `${serverUUID}_screenshot_${epoch}`;
const imageFilePath = path.join(SERVER_PATHS.tmp.self, imageFileName); const imageFilePath = path.join(SERVER_PATHS.tmp.self, imageFileName);
@ -79,40 +87,46 @@ export const getServerDetail: RequestHandler = (request, response) => {
let imageData = ''; let imageData = '';
namedPipeReadStream.once('close', () => { namedPipeReadStream.once('error', (readError) => {
console.log(`On close; removing named pipe at ${imageFilePath}.`); stderr(`Failed to read from named pipe; CAUSE: ${readError}`);
try {
rm(imageFilePath);
} catch (cleanPipeError) {
console.log(
`Failed to clean up named pipe; CAUSE: ${cleanPipeError}`,
);
}
}); });
namedPipeReadStream.once('end', () => { namedPipeReadStream.once('close', () => {
stdout(`On close; removing named pipe at ${imageFilePath}.`);
response.status(200).send({ screenshot: imageData }); response.status(200).send({ screenshot: imageData });
rmfifo(imageFilePath);
}); });
namedPipeReadStream.on('data', (data) => { namedPipeReadStream.on('data', (data) => {
const imageChunk = data.toString().trim(); const imageChunk = data.toString().trim();
const chunkLogLength = 10; const peekLength = 10;
console.log( stdout(
`${serverUUID} image chunk: ${imageChunk.substring( `${serverUUID} image chunk: ${
0, imageChunk.length > 0
chunkLogLength, ? `${imageChunk.substring(
)}...${imageChunk.substring(imageChunk.length - chunkLogLength - 1)}`, 0,
peekLength,
)}...${imageChunk.substring(
imageChunk.length - peekLength - 1,
)}`
: 'empty'
}`,
); );
imageData += imageChunk; imageData += imageChunk;
}); });
} catch (prepPipeError) { } catch (prepPipeError) {
console.log(`Failed to prepare named pipe; CAUSE: ${prepPipeError}`); stderr(
`Failed to prepare named pipe and/or receive image data; CAUSE: ${prepPipeError}`,
);
response.status(500).send(); response.status(500).send();
rmfifo(imageFilePath);
return; return;
} }
@ -136,9 +150,7 @@ out-file-id=${epoch}`,
job_host_uuid: serverHostUUID, job_host_uuid: serverHostUUID,
}); });
} catch (subError) { } catch (subError) {
console.log( stderr(`Failed to queue fetch server screenshot job; CAUSE: ${subError}`);
`Failed to queue fetch server screenshot job; CAUSE: ${subError}`,
);
response.status(500).send(); response.status(500).send();

Loading…
Cancel
Save