Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/asyncflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import { fileURLToPath } from "url";
import AdmZip from "adm-zip";
import { sendToLambda } from "./sendLambda";
import { initDirectories, initCallbacks } from "./initialize";
import {
triggerDirectoryJob,
TriggerAsyncflowJobOptions,
LambdaResponse,
} from "./trigger";

type JSONPrimitive = string | number | boolean | null;

Expand Down Expand Up @@ -98,6 +103,13 @@ export class Asyncflow {
return asyncflowInstance;
}

triggerDirectoryJob<T>(
jobName: string,
options?: TriggerAsyncflowJobOptions<T>,
) {
triggerDirectoryJob(jobName, options);
}

async addJob<F extends (...args: any[]) => any>(
fun: SerializableFunction<F>,
): Promise<(...args: Parameters<F>) => Promise<ReturnType<F>>> {
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from "./initialize";
export * from "./trigger";
export * from "./asyncflow";
18 changes: 11 additions & 7 deletions src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { isEnvironmentValid } from "./utils/credentials";
import { lambdaClient } from "./awsClients";
import { initDirectories } from "./initialize";

interface TriggerAsyncflowJobOptions<T> {
export interface TriggerAsyncflowJobOptions<T> {
callback?: (a: LambdaResponse<T> | null) => void;
onrejected?: () => void;
onrejected?: (err: any) => void;
payload?: Record<string, any>;
}

interface LambdaResponse<T> {
export interface LambdaResponse<T> {
statusCode: number;
body: T;
}
Expand Down Expand Up @@ -39,24 +39,28 @@ function callback<T>(
}
}

export function triggerJob<T>(
export function triggerDirectoryJob<T>(
jobName: string,
options?: TriggerAsyncflowJobOptions<T>,
) {
if (!isEnvironmentValid()) return;

if (NODE_ENV !== "production") initDirectories();

const lambdaName = "ASYNCFLOW-DIR-" + jobName;

const command = new InvokeCommand({
FunctionName: jobName,
FunctionName: lambdaName,
InvocationType: "RequestResponse",
Payload: options?.payload ? JSON.stringify(options.payload) : undefined,
});

lambdaClient
.send(command)
.then((res) => callback(res, options))
.catch(() =>
options?.onrejected ? options.onrejected() : defaultOnrejected(jobName),
.catch((err) =>
options?.onrejected
? options.onrejected(err)
: defaultOnrejected(jobName),
);
}
Loading