Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphv committed Aug 13, 2024
1 parent 192d615 commit 2fb5436
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 61 deletions.
31 changes: 16 additions & 15 deletions src/GallifreyRulesEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { EOL } from 'os';
import colors from 'colors';
import { ascii } from './ascii';
import { loadAll } from 'js-yaml';
import { WithModuleNameType } from './base-interfaces/ModuleInterface';

export class GallifreyRulesEngine {
private readonly schemaLoader: SchemaLoader;
Expand Down Expand Up @@ -513,7 +514,7 @@ export class GallifreyRulesEngine {
private async runRules(
event: GallifreyEventTypeInternal<any>,
engineEventContext: EngineEventContext,
rulesInstances: RuleInterface<any>[],
rulesInstances: WithModuleNameType<RuleInterface<any>>[],
source: string,
pause?: () => () => void,
) {
Expand Down Expand Up @@ -626,14 +627,14 @@ export class GallifreyRulesEngine {
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);

const [actionInstance] = (await this.instancesFactory.getModulesInstances(
const [actionInstance] = await this.instancesFactory.getModulesInstances<ActionInterface<any, any>>(
engineEventContext,
configAccessor,
[moduleData],
engineEventContext.getJournalLogger(),
(measurementName: string) =>
AssertNotNull(this.providersContext.metrics).getPoint(`plugins.${measurementName}`),
)) as ActionInterface<any, any>[];
);
logger.debug(`Fetched action module instance: ${actionName}`);

const engineAction = new EngineAction(
Expand Down Expand Up @@ -693,14 +694,14 @@ export class GallifreyRulesEngine {
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);

const [dataObjectInstance] = (await this.instancesFactory.getModulesInstances(
const [dataObjectInstance] = await this.instancesFactory.getModulesInstances<DataObjectInterface<any, any>>(
engineEventContext,
configAccessor,
[moduleData],
engineEventContext.getJournalLogger(),
(measurementName: string) =>
AssertNotNull(this.providersContext.metrics).getPoint(`plugins.${measurementName}`),
)) as DataObjectInterface<any, any>[];
);
logger.debug(`Fetched data object module instance: ${dataObjectName}`);

const engineDataObject = new EngineDataObject(
Expand Down Expand Up @@ -737,7 +738,7 @@ export class GallifreyRulesEngine {
private async runRule(
engineEventContext: EngineEventContext,
event: GallifreyEventTypeInternal<any>,
ruleInstance: RuleInterface<any>,
ruleInstance: WithModuleNameType<RuleInterface<any>>,
engineRule: EngineRule,
source: string,
pause?: () => () => void,
Expand Down Expand Up @@ -801,7 +802,7 @@ export class GallifreyRulesEngine {
engine: EngineReactToFailure,
payload: any,
error: any,
ruleInstance: RuleInterface<any>,
ruleInstance: WithModuleNameType<RuleInterface<any>>,
) {
await AssertNotNull(this.providersContext.reactToFailure).reactToRuleFailure(
engine,
Expand Down Expand Up @@ -865,14 +866,14 @@ export class GallifreyRulesEngine {
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);

const filtersInstances = (await this.instancesFactory.getModulesInstances(
const filtersInstances = await this.instancesFactory.getModulesInstances<FilterInterface<any>>(
engineEventContext,
configAccessor,
filtersModules,
engineEventContext.getJournalLogger(),
(measurementName: string) =>
AssertNotNull(this.providersContext.metrics).getPoint(`plugins.${measurementName}`),
)) as FilterInterface<any>[];
);
logger.debug(`Fetched filtersInstances: ${filtersInstances.length}`);
return await this.runFilters(event, engineEventContext, filtersInstances);
}
Expand Down Expand Up @@ -904,14 +905,14 @@ export class GallifreyRulesEngine {
engineEventContext,
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);
const rulesInstances = (await this.instancesFactory.getModulesInstances(
const rulesInstances = await this.instancesFactory.getModulesInstances<RuleInterface<any>>(
engineEventContext,
configAccessor,
rulesModules,
engineEventContext.getJournalLogger(),
(measurementName: string) =>
AssertNotNull(this.providersContext.metrics).getPoint(`plugins.${measurementName}`),
)) as RuleInterface<any>[];
);
logger.debug(`Fetched rulesInstances: ${rulesInstances.length}`);
//*/ isRuleEnabled?
await this.runRules(event, engineEventContext, rulesInstances, source, pause);
Expand All @@ -920,7 +921,7 @@ export class GallifreyRulesEngine {
private async runFilters(
event: GallifreyEventTypeInternal<any>,
engineEventContext: EngineEventContext,
filtersInstances: FilterInterface<any>[],
filtersInstances: WithModuleNameType<FilterInterface<any>>[],
): Promise<boolean> {
for (const filterInstance of filtersInstances) {
const configAccessor = await TypeAssertNotNull(
Expand Down Expand Up @@ -953,7 +954,7 @@ export class GallifreyRulesEngine {
private async canContinueFilter(
engineEventContext: EngineEventContext,
event: GallifreyEventTypeInternal<any>,
filterInstance: FilterInterface<any>,
filterInstance: WithModuleNameType<FilterInterface<any>>,
engineFilter: EngineFilter,
) {
try {
Expand Down Expand Up @@ -1166,14 +1167,14 @@ export class GallifreyRulesEngine {
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);

const [asyncActionInstance] = (await this.instancesFactory.getModulesInstances(
const [asyncActionInstance] = await this.instancesFactory.getModulesInstances<AsyncActionInterface<any, any>>(
engineEventContext,
configAccessor,
[moduleData],
engineEventContext.getJournalLogger(),
(measurementName: string) =>
AssertNotNull(this.providersContext.metrics).getPoint(`plugins.${measurementName}`),
)) as AsyncActionInterface<any, any>[];
);
logger.debug(`Fetched async action module instance: ${asyncActionName}`);

const engineAction = new EngineAction(
Expand Down
3 changes: 2 additions & 1 deletion src/SafeJournalLoggerWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import JournalLoggerInterface from './interfaces/Providers/JournalLoggerInterface';
import { DontThrowJustLog } from './lib/Decorators';
import { GallifreyEventTypeInternal } from './lib/GallifreyEventTypeInternal';
import { WithModuleNameType } from './base-interfaces/ModuleInterface';

export default class SafeJournalLoggerWrapper implements JournalLoggerInterface {
constructor(private readonly journalLogger: JournalLoggerInterface) {}
constructor(private readonly journalLogger: WithModuleNameType<JournalLoggerInterface>) {}

@DontThrowJustLog
customLog(description: string, extra: any): void {
Expand Down
16 changes: 15 additions & 1 deletion src/base-interfaces/ModuleInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@
* author: Ralph Varjabedian
*/
import InitializableInterface from './InitializableInterface';
import EngineCriticalError from '../errors/EngineCriticalError';

export default interface ModuleInterface extends InitializableInterface {
/**
* Gets the module name, has to be unique across all modules
*/
getModuleName(): string;
getModuleName?(): string;

/**
* The optional namespaces that the module is compatible with.
* If skipped this means this module has no restrictions and can run with any namespace
*/
getModuleNamespaces?(): Promise<string[]>;
}

export type WithModuleNameType<T extends ModuleInterface> = T & Required<Pick<ModuleInterface, 'getModuleName'>>;

export function IsWithModuleNameType<Type extends ModuleInterface>(obj: Type): obj is WithModuleNameType<Type> {
return typeof obj.getModuleName === 'function';
}

export function WithModuleName<Type extends ModuleInterface>(obj: Type): WithModuleNameType<Type> {
if (!IsWithModuleNameType<Type>(obj)) {
throw new EngineCriticalError(`missing getModuleName`);
}
return obj;
}
Loading

0 comments on commit 2fb5436

Please sign in to comment.