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) => { async (request, app, params) => {
const bulkMemory: BulkStorage = app.get('bulk'); const bulkMemory: BulkStorage = app.get('bulk');
const bulk = await bulkMemory.read(params.UID); const bulk = bulkMemory.read(params.UID);
if (typeof bulk === 'undefined') { if (typeof bulk === 'undefined') {
Logger.warn(`Bulk with ${params.UID} not found.`); Logger.warn(`Bulk with ${params.UID} not found.`);

View File

@@ -32,7 +32,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneRequest, SCBulkDoneResponse>
async (_request, app, params) => { async (_request, app, params) => {
const bulkMemory: BulkStorage = app.get('bulk'); const bulkMemory: BulkStorage = app.get('bulk');
const bulk = await bulkMemory.read(params.UID); const bulk = bulkMemory.read(params.UID);
if (typeof bulk === 'undefined') { if (typeof bulk === 'undefined') {
Logger.warn(`Bulk with ${params.UID} not found.`); 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 // send the request to the plugin (forward the body) and save the response
const pluginResponse = await got.post( const pluginResponse = await got.post(
plugin.route, plugin.route.replace(/^\//gi, ''),
{ {
prefixUrl: plugin.address, prefixUrl: plugin.address,
json: req.body, json: req.body,
timeout: configFile.backend.externalRequestTimeout, timeout: configFile.backend.externalRequestTimeout,
responseType: 'json',
}, },
); );
responseBody = JSON.parse(pluginResponse.body); responseBody = pluginResponse.body as object;
const responseValidation = validator.validate(responseBody, plugin.responseSchema); const responseValidation = validator.validate(responseBody, plugin.responseSchema);
if (responseValidation.errors.length > 0) { if (responseValidation.errors.length > 0) {
throw new SCValidationErrorResponse(responseValidation.errors, isTestEnvironment); throw new SCValidationErrorResponse(responseValidation.errors, isTestEnvironment);

View File

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

View File

@@ -21,7 +21,7 @@ import {
import {expect, use} from 'chai'; import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised'; import chaiAsPromised from 'chai-as-promised';
import {Request} from 'express'; import {Request} from 'express';
import got, { Options } from 'got'; import got, {Options} from 'got';
import nock from 'nock'; import nock from 'nock';
import sinon from 'sinon'; import sinon from 'sinon';
import {mockReq} from 'sinon-express-mock'; import {mockReq} from 'sinon-express-mock';
@@ -79,9 +79,9 @@ describe('Virtual plugin routes', async function () {
await virtualPluginRoute(req, plugin); await virtualPluginRoute(req, plugin);
expect(gotStub.args[0][0]).to.equal(plugin.route); expect(gotStub.args[0][0]).to.equal(plugin.route.substr(1));
expect(((gotStub.args[0] as any)[1] as Options).prefixUrl).to.equal(plugin.address); expect(((gotStub.args[0] as any)[1] as Options).prefixUrl).to.equal(plugin.address);
expect(((gotStub.args[0] as any)[1] as Options).body).to.equal(req.body); expect(((gotStub.args[0] as any)[1] as Options).json).to.equal(req.body);
}); });
it('should provide data from the plugin when its route is called', async function () { it('should provide data from the plugin when its route is called', async function () {

View File

@@ -81,14 +81,14 @@ describe('Bulk Storage', function () {
}); });
it('should throw an error if the bulk for deletion cannot be read', async function () { it('should throw an error if the bulk for deletion cannot be read', async function () {
sandbox.stub(BulkStorage.prototype, 'read').callsFake(async () => Promise.resolve(undefined)); sandbox.stub(BulkStorage.prototype, 'read').callsFake(() => undefined);
const bulkStorage = new BulkStorage(database); const bulkStorage = new BulkStorage(database);
return expect(bulkStorage.delete('123')).to.be.rejected; return expect(bulkStorage.delete('123')).to.be.rejected;
}); });
it('should delete a bulk', async function () { it('should delete a bulk', async function () {
const readStub = sandbox.stub(BulkStorage.prototype, 'read').callsFake(async () => Promise.resolve(bulk)); const readStub = sandbox.stub(BulkStorage.prototype, 'read').callsFake(() => bulk);
let caught: any; let caught: any;
sandbox.stub(NodeCache.prototype, 'del').callsFake(() => caught = 123); sandbox.stub(NodeCache.prototype, 'del').callsFake(() => caught = 123);
// force call // force call