Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make apiPollingDelay an optionnal parameter of StepFunctions helper #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions src/helpers/stepFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import { StepFunctions as AWSStepFunctions } from "aws-sdk";
import { AWSClient } from "./general";

const API_POLLING_DELAY_MS = 1000;
const DEFAULT_API_POLLING_DELAY_MS = 1000;

type StepFunctionOptions = { apiPollingDelay?: number };

export default class StepFunctions {
stepFunctions: AWSStepFunctions | undefined;
allStateMachines: AWSStepFunctions.ListStateMachinesOutput | undefined;
apiPollingDelay = DEFAULT_API_POLLING_DELAY_MS;

async init(): Promise<void> {
async init(options: StepFunctionOptions = {}): Promise<void> {
this.stepFunctions = new AWSClient.StepFunctions();
this.allStateMachines = await this.stepFunctions
.listStateMachines()
.promise();
const { apiPollingDelay } = options;
if (apiPollingDelay !== undefined && apiPollingDelay > 0) {
this.apiPollingDelay = apiPollingDelay;
}
}

static async build(): Promise<StepFunctions> {
static async build(
options: StepFunctionOptions = {}
): Promise<StepFunctions> {
const stepFunction = new StepFunctions();
await stepFunction.init();
await stepFunction.init(options);

return stepFunction;
}

// eslint-disable-next-line max-params
async runExecution(
stateMachineName: string,
input: unknown
input: unknown,
options: StepFunctionOptions = {}
): Promise<AWSStepFunctions.DescribeExecutionOutput> {
const { apiPollingDelay } = options;
const executionApiPollingDelay =
apiPollingDelay !== undefined && apiPollingDelay > 0
? apiPollingDelay
: this.apiPollingDelay;

if (this.allStateMachines === undefined) {
throw new Error(
"The list of state machines is undefined. You might have forgotten to run build()."
Expand Down Expand Up @@ -63,7 +80,9 @@ export default class StepFunctions {
.promise();

// Wait before retrying to avoid throttle limits
await new Promise((resolve) => setTimeout(resolve, API_POLLING_DELAY_MS));
await new Promise((resolve) =>
setTimeout(resolve, executionApiPollingDelay)
);
}

return await this.stepFunctions
Expand Down