Skip to content

Commit

Permalink
Merge pull request #110 from bcgov/bug/cache
Browse files Browse the repository at this point in the history
Await writing template to temp directory
  • Loading branch information
TimCsaky authored Jun 26, 2024
2 parents e899938 + 50803cc commit 562a81c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/cacheCleaner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { join } = require('path');

const log = require('./src/components/log')(module.filename);

const RATIO = 0.8; // Best practice is to keep the cache no more than 80% full
const RATIO = 0.7; // Best practice is to keep the cache no more than 70% full

const osTempDir = realpathSync(tmpdir());
const cacheDir = (() => {
Expand Down
22 changes: 13 additions & 9 deletions app/src/components/fileCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ class FileCache {
}
if (!name) {
result.errorType = 400;
result.errorMsg = 'Cannot move file; name parameter is required.';
result.errorMsg = 'Cannot move file; file name parameter is required.';
return result;
}

try {
// get a hash of the file from contents
result.hash = await this._getHash(source);
} catch (e) {
result.errorType = 500;
Expand All @@ -120,6 +121,7 @@ class FileCache {
}

const hashPath = this._getHashPath(result.hash);
// if file exists at temp file path
if (fs.existsSync(hashPath)) {
if (options.overwrite) {
fs.removeSync(hashPath);
Expand Down Expand Up @@ -158,28 +160,30 @@ class FileCache {
return result;
}

async write(content, name, contentEncodingType = 'base64', options = { overwrite: false }) {
async write(content, fileType, contentEncodingType = 'base64', options = { overwrite: false }) {
let result = { success: false, errorType: null, errorMsg: null, hash: null };

if (!content) {
result.errorType = 400;
result.errorMsg = 'Cannot write file; content parameter is required.';
return result;
}
if (!name) {
if (!fileType) {
result.errorType = 400;
result.errorMsg = 'Cannot write file; name parameter is required.';
result.errorMsg = 'Cannot write file; fileType parameter is required.';
return result;
}
const tmpFile = this._getTempFilePath();
fs.outputFileSync(tmpFile, content, { encoding: contentEncodingType });
// save template to temp directory
await fs.outputFileSync(tmpFile, content, { encoding: contentEncodingType });

// name may only be an extension, if that is the case, let's generate a name
let destFilename = path.extname(name) === '' ? path.format({
// move temp file to file cache
let destFilename = path.format({
name: uuidv4(),
ext: (name.startsWith('.') ? name : `.${name}`)
}) : name;
ext: fileType.replace(/\./g, '')
});
result = await this.move(tmpFile, destFilename, options);
log.info('Template cached', { function: 'fileCache.write' });
if (!result.success) {
result.errorMsg = `Error writing content to cache. ${result.errorMsg}`;
}
Expand Down
16 changes: 15 additions & 1 deletion app/src/routes/v2/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const log = require('../../components/log')(module.filename);

const fileCache = new FileCache();

/** Returns the rendered report from cache */
/**
* Upload a template to cache
*/
templateRouter.post('/', upload, async (req, res) => {
log.verbose('Template upload');

Expand All @@ -28,6 +30,9 @@ templateRouter.post('/', upload, async (req, res) => {
}
});

/**
* Render a document from a template provided in JSON body
*/
templateRouter.post('/render', middleware.validateTemplate, async (req, res) => {
log.verbose('Template upload and render');

Expand All @@ -52,12 +57,18 @@ templateRouter.post('/render', middleware.validateTemplate, async (req, res) =>
return await findAndRender(content.hash, req, res);
});

/**
* Render a document from a cached template
*/
templateRouter.post('/:uid/render', middleware.validateCarbone, async (req, res) => {
const hash = req.params.uid;
log.verbose('Template render', { hash: hash });
return await findAndRender(hash, req, res);
});

/**
* get a template from cache
*/
templateRouter.get('/:uid', async (req, res) => {
const hash = req.params.uid;
const download = req.query.download !== undefined;
Expand All @@ -66,6 +77,9 @@ templateRouter.get('/:uid', async (req, res) => {
return getFromCache(hash, hashHeaderName, download, false, res);
});

/**
* delete a template from cache
*/
templateRouter.delete('/:uid', async (req, res) => {
const hash = req.params.uid;
const download = req.query.download !== undefined;
Expand Down

0 comments on commit 562a81c

Please sign in to comment.