refactor: remove legacy callbacks from bulk storage

This commit is contained in:
Rainer Killinger
2021-04-26 18:32:31 +02:00
parent 7424ad9831
commit 334f5a7507
6 changed files with 18 additions and 18 deletions

View File

@@ -32,7 +32,7 @@ export const bulkAddRouter = createRoute<SCBulkAddRequest, SCBulkAddResponse>(
async (request, app, params) => {
const bulkMemory: BulkStorage = app.get('bulk');
const bulk = await bulkMemory.read(params.UID);
const bulk = bulkMemory.read(params.UID);
if (typeof bulk === 'undefined') {
Logger.warn(`Bulk with ${params.UID} not found.`);

View File

@@ -32,7 +32,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneRequest, SCBulkDoneResponse>
async (_request, app, params) => {
const bulkMemory: BulkStorage = app.get('bulk');
const bulk = await bulkMemory.read(params.UID);
const bulk = bulkMemory.read(params.UID);
if (typeof bulk === 'undefined') {
Logger.warn(`Bulk with ${params.UID} not found.`);

View File

@@ -39,14 +39,15 @@ export async function virtualPluginRoute(req: Request, plugin: SCPluginMetaData)
}
// send the request to the plugin (forward the body) and save the response
const pluginResponse = await got.post(
plugin.route,
plugin.route.replace(/^\//gi, ''),
{
prefixUrl: plugin.address,
json: req.body,
timeout: configFile.backend.externalRequestTimeout,
responseType: 'json',
},
);
responseBody = JSON.parse(pluginResponse.body);
responseBody = pluginResponse.body as object;
const responseValidation = validator.validate(responseBody, plugin.responseSchema);
if (responseValidation.errors.length > 0) {
throw new SCValidationErrorResponse(responseValidation.errors, isTestEnvironment);

View File

@@ -17,7 +17,6 @@ import {SCBulkRequest, SCThingType} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import moment from 'moment';
import NodeCache from 'node-cache';
import {promisify} from 'util';
import {v4} from 'uuid';
import {Database} from './database';
@@ -125,14 +124,14 @@ export class BulkStorage {
* @param bulk the bulk process to save
* @returns the bulk process that was saved
*/
private async save(bulk: Bulk): Promise<Bulk> {
private save(bulk: Bulk): Bulk {
const expirationInSeconds = moment(bulk.expiration)
// tslint:disable-next-line: no-magic-numbers
.diff(moment.now()) / 1000;
Logger.info('Bulk expires in ', expirationInSeconds, 'seconds');
// save the item in the cache with it's expected expiration
await promisify<string, Bulk, number, boolean>(this.cache.set)(bulk.uid, bulk, expirationInSeconds);
this.cache.set(bulk.uid, bulk, expirationInSeconds);
return bulk;
}
@@ -147,7 +146,7 @@ export class BulkStorage {
bulk.source = bulkRequest.source;
bulk.type = bulkRequest.type;
await this.save(bulk);
this.save(bulk);
// tell the database that the bulk was created
await this.database.bulkCreated(bulk);
@@ -161,14 +160,14 @@ export class BulkStorage {
* @returns a promise that contains the deleted bulk process
*/
public async delete(uid: string): Promise<Bulk> {
const bulk = await this.read(uid);
const bulk = this.read(uid);
if (typeof bulk === 'undefined') {
throw new Error(`Bulk that should be deleted was not found. UID was "${uid}"`);
}
// delete the bulk process from the cache
await promisify<string>(this.cache.del)(uid);
this.cache.del(uid);
// tell the database to handle the expiration of the bulk
await this.database.bulkExpired(bulk);
@@ -183,7 +182,7 @@ export class BulkStorage {
*/
public async markAsDone(bulk: Bulk): Promise<void> {
bulk.state = 'done';
await this.save(bulk);
this.save(bulk);
// tell the database that this is the new bulk
await this.database.bulkUpdated(bulk);
@@ -196,8 +195,8 @@ export class BulkStorage {
* @param uid uid of the bulk process
* @returns a promise that contains a bulk
*/
public async read(uid: string): Promise<Bulk | undefined> {
return promisify<string, Bulk | undefined>(this.cache.get)(uid);
public read(uid: string): Bulk | undefined {
return this.cache.get(uid);
}
}