Skip to content

Commit

Permalink
Merge pull request #519 from Green-Software-Foundation/plugin-storage
Browse files Browse the repository at this point in the history
Plugin storage
  • Loading branch information
jmcook1186 authored Mar 11, 2024
2 parents 78ca638 + 6071fea commit 2193c24
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/config/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=fee
`Rejecting overriding of canonical parameter: ${param.name}.`,
INVALID_EXHAUST_PLUGIN: (pluginName: string) =>
`Invalid exhaust plugin: ${pluginName}.`,
UNKNOWN_PARAM: (name: string) =>
`Unknown parameter: ${name}. Using 'sum' aggregation method.`,
};
3 changes: 1 addition & 2 deletions src/lib/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ const computeNode = async (node: Node, params: Params): Promise<any> => {
while (pipelineCopy.length !== 0) {
const pluginName = pipelineCopy.shift() as string;

const plugin = params.plugins[pluginName];
const {execute, metadata} = plugin;
const {execute, metadata} = params.plugins.get(pluginName);
const nodeConfig = config && config[pluginName];

if (metadata.kind === 'execute') {
Expand Down
13 changes: 8 additions & 5 deletions src/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import pathLib = require('path');

import {ERRORS} from '../util/errors';
import {logger} from '../util/logger';
import {memoizedLog} from '../util/log-memoize';
import {pluginStorage} from '../util/plugin-storage';

import {CONFIG, STRINGS} from '../config';

import {PluginInterface} from '../types/interface';
import {PluginsStorage} from '../types/initialize';
import {GlobalPlugins, PluginOptions} from '../types/manifest';
import {PluginStorageInterface} from '../types/plugin-storage';

const {ModuleInitializationError, PluginCredentialError} = ERRORS;

Expand Down Expand Up @@ -52,7 +54,7 @@ const handModule = (method: string, path: string) => {
}

if (!path.includes(NATIVE_PLUGIN)) {
logger.warn(NOT_NATIVE_PLUGIN(path));
memoizedLog(logger.warn, NOT_NATIVE_PLUGIN(path));
}
}

Expand Down Expand Up @@ -85,11 +87,12 @@ const initPlugin = async (
*/
export const initalize = async (
plugins: GlobalPlugins
): Promise<PluginsStorage> => {
const storage: PluginsStorage = {};
): Promise<PluginStorageInterface> => {
const storage = pluginStorage();

for await (const pluginName of Object.keys(plugins)) {
storage[pluginName] = await initPlugin(plugins[pluginName]);
const plugin = await initPlugin(plugins[pluginName]);
storage.set(pluginName, plugin);
}

return storage;
Expand Down
5 changes: 4 additions & 1 deletion src/lib/parameterize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {logger} from '../util/logger';
import {STRINGS, PARAMETERS} from '../config';

import {ManifestParameter} from '../types/manifest';
import {memoizedLog} from '../util/log-memoize';

const {REJECTING_OVERRIDE} = STRINGS;
const {REJECTING_OVERRIDE, UNKNOWN_PARAM} = STRINGS;

/**
* Parameters manager. Provides get aggregation method and combine functionality.
Expand All @@ -20,6 +21,8 @@ const Parametrize = () => {
return PARAMETERS[unitName as keyof typeof PARAMETERS].aggregation;
}

memoizedLog(logger.warn, UNKNOWN_PARAM(unitName));

return 'sum';
};

Expand Down
6 changes: 3 additions & 3 deletions src/types/compute.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {PluginsStorage} from './initialize';
import {PluginParams} from './interface';
import {Context} from './manifest';
import {PluginStorageInterface} from './plugin-storage';

export type NodeConfig = {
[key: string]: Record<string, any>;
};

export type Params = {
plugins: PluginsStorage;
plugins: PluginStorageInterface;
context: Context;
pipeline?: string[];
config?: NodeConfig;
Expand All @@ -25,5 +25,5 @@ export type Node = {

export type ComputeParams = {
context: Context;
plugins: PluginsStorage;
plugins: PluginStorageInterface;
};
5 changes: 0 additions & 5 deletions src/types/initialize.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/types/plugin-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {pluginStorage} from '../util/plugin-storage';
import {PluginInterface} from './interface';

export type PluginStorage = {
[key: string]: PluginInterface;
};

export type PluginStorageInterface = ReturnType<typeof pluginStorage>;
20 changes: 20 additions & 0 deletions src/util/log-memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {LeveledLogMethod} from 'winston';

/**
* Keeps in memory logged messages. If called with redundant message, skips logging.
*/
const memoizedLogger = () => {
const memory: string[] = [];

return (logger: LeveledLogMethod, message: string) => {
if (memory.includes(message)) {
return;
}

memory.push(message);
logger(message);
};
};

/** Singleton pattern. */
export const memoizedLog = memoizedLogger();
38 changes: 38 additions & 0 deletions src/util/plugin-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {ERRORS} from '../util/errors';

import {PluginInterface} from '../types/interface';
import {PluginStorage} from '../types/plugin-storage';

const {PluginInitalizationError} = ERRORS;

/**
* Storage for maintaining plugins.
*/
export const pluginStorage = () => {
const storage: PluginStorage = {};

return {
/**
* Gets plugin by given `name`. If it's missing then throws error.
*/
get: (name: string) => {
const plugin = storage[name];

if (!plugin) {
throw new PluginInitalizationError(
`Not initalized plugin: ${name}. Check if ${name} is in 'manifest.initalize.plugins'.`
);
}

return plugin;
},
/**
* Saves given `plugin` with given `name`.
*/
set: function (name: string, plugin: PluginInterface) {
storage[name] = plugin;

return this;
},
};
};

0 comments on commit 2193c24

Please sign in to comment.