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

Introduce the invoke function to resonate and the context. #132

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions lib/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export enum ErrorCodes {
STORE_ALREADY_EXISTS = 45,
STORE_INVALID_STATE = 46,
STORE_ENCODER = 47,

// error in user function
USER = 60,
}

export class ResonateError extends Error {
Expand Down
7 changes: 7 additions & 0 deletions lib/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ export type Options = {

export type PartialOptions = Partial<Options> & { __resonate: true };

/**
* A subset of configuration options for overriding when invocating a top level function.
*/
export type InvocationOverrides = Partial<
Pick<Options, "eidFn" | "idempotencyKeyFn" | "retryPolicy" | "tags" | "timeout" | "version">
> & { __resonate: true };

export function isOptions(o: unknown): o is PartialOptions {
return typeof o === "object" && o !== null && (o as PartialOptions).__resonate === true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/promises/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class DurablePromise<T> {
id: string,
timeout: number,
opts: Partial<CreateOptions> = {},
) {
): Promise<DurablePromise<T>> {
const storedPromise = await store.create(
id,
opts.idempotencyKey,
Expand Down
23 changes: 23 additions & 0 deletions lib/core/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ export function retryIterator<T extends { retryPolicy: RetryPolicy; attempt: num
};
}

export async function runWithRetry<T>(func: () => Promise<T>, retryPolicy: RetryPolicy, timeout: number) {
let error;

const ctx = { attempt: 0, retryPolicy, timeout };

// invoke the function according to the retry policy
for (const delay of retryIterator(ctx)) {
await new Promise((resolve) => setTimeout(resolve, delay));

try {
return await func();
} catch (e) {
error = e;

// bump the attempt count
ctx.attempt++;
}
}

// if all attempts fail throw the last error
throw error;
}

function retryDefaults(retryPolicy: RetryPolicy): {
initialDelay: number;
backoffFactor: number;
Expand Down
29 changes: 29 additions & 0 deletions lib/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,32 @@ export function mergeObjects<T extends object, U extends object>(obj1: T, obj2:
return acc;
}, {} as any);
}

/**
* Creates a promise that resolves after a specified delay.
*
* @param ms - The delay in milliseconds.
* @returns A promise that resolves after the specified delay.
*
* @example
* // Basic usage
* await sleep(1000); // Pauses execution for 1 second
*
* @example
* // Using in an async function
* async function example() {
* console.log('Start');
* await sleep(2000);
* console.log('2 seconds later');
* }
*
* @example
* // Using with .then()
* sleep(3000).then(() => console.log('3 seconds have passed'));
*/
export async function sleep(ms: number): Promise<void> {
if (ms < 0) {
throw new Error("ms should be a positive integer");
}
return new Promise((resolve) => setTimeout(resolve, ms));
}
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export * from "./resonate";
// async
export * from "./async";

// invok
export * as invok from "./resonate_invok";

// errors
export * from "./core/errors";

Expand Down
Loading
Loading