-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spike: introduce generic workload interface
- Loading branch information
Showing
5 changed files
with
173 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// import { BrokerClientRequestWorkload } from '../broker-workload/clientRequests'; | ||
// import { BrokerWorkload } from '../broker-workload/websocketRequests'; | ||
|
||
export enum WorkloadType { | ||
remoteServer = 'remoteServer', | ||
localClient = 'localClient', | ||
} | ||
|
||
export interface RemoteServerWorkloadParams { | ||
connectionIdentifier: string; | ||
options: any; | ||
websocketConnectionHandler: any; | ||
} | ||
|
||
export interface LocalClientWorkloadParams { | ||
req: any; | ||
res: any; | ||
options: any; | ||
} | ||
|
||
export interface RemoteServerWorkloadRuntimeParams { | ||
payload: any; | ||
websocketHandler: any; | ||
} | ||
export interface LocalClientWorkloadRuntimeParams { | ||
makeRequestOverHttp?: boolean; | ||
} | ||
// export type WorkloadRuntimeParamType<T extends WorkloadType> = | ||
// T extends WorkloadType.remoteServer | ||
// ? RemoteServerWorkloadRuntimeParams | ||
// : LocalClientWorkloadRuntimeParams; | ||
type WorkloadRuntimeParamType< | ||
T extends WorkloadType.localClient | WorkloadType.remoteServer, | ||
> = T extends WorkloadType.remoteServer | ||
? RemoteServerWorkloadRuntimeParams | ||
: T extends WorkloadType.localClient | ||
? LocalClientWorkloadRuntimeParams | ||
: never; | ||
export type WorkloadRuntimeReturnType = Promise<void> | Promise<any>; | ||
|
||
interface WorkloadModule { | ||
default: new ( | ||
connectionIdentifier: string, | ||
options: any, | ||
websocketConnectionHandler: any, | ||
) => Workload<WorkloadType.remoteServer>; | ||
} | ||
|
||
export abstract class Workload< | ||
T extends WorkloadType.localClient | WorkloadType.remoteServer, | ||
> { | ||
type: WorkloadType; | ||
name: string; | ||
|
||
constructor(name: string, type: WorkloadType) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
abstract handler( | ||
data: WorkloadRuntimeParamType<T>, | ||
): WorkloadRuntimeReturnType; | ||
|
||
// abstract handler(makeRequestOverHttp: boolean): void; | ||
// abstract handler(payload: any, websocketHandler: any): void; | ||
|
||
private static async instantiateRemoteServerWorkload( | ||
name: string, | ||
path: string, | ||
params: RemoteServerWorkloadParams, | ||
): Promise<Workload<WorkloadType.remoteServer>> { | ||
const { connectionIdentifier, options, websocketConnectionHandler } = | ||
params; | ||
const importedModule = (await import(path)) as WorkloadModule; | ||
const WorkloadClass = importedModule[name]; | ||
return new WorkloadClass( | ||
connectionIdentifier, | ||
options, | ||
websocketConnectionHandler, | ||
); | ||
} | ||
private static async instantiateLocalClientWorkload( | ||
name: string, | ||
path: string, | ||
params: LocalClientWorkloadParams, | ||
): Promise<Workload<WorkloadType.localClient>> { | ||
const { req, res, options } = params; | ||
const importedModule = (await import(path)) as WorkloadModule; | ||
const WorkloadClass = importedModule[name]; | ||
return new WorkloadClass(req, res, options); | ||
} | ||
|
||
static async instantiate( | ||
name: string, | ||
path: string, | ||
type: WorkloadType.localClient | WorkloadType.remoteServer, | ||
params, | ||
): Promise< | ||
Workload<WorkloadType.localClient> | Workload<WorkloadType.remoteServer> | ||
> { | ||
switch (type) { | ||
case WorkloadType.remoteServer: | ||
return await this.instantiateRemoteServerWorkload(name, path, params); | ||
case WorkloadType.localClient: | ||
return await this.instantiateLocalClientWorkload(name, path, params); | ||
default: | ||
throw new Error(`Error loading workload - unknown type ${type}`); | ||
} | ||
} | ||
} |