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

Add max concurrency action #112

Merged
merged 3 commits into from
Jan 8, 2024
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
5 changes: 5 additions & 0 deletions .changeset/poor-cars-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@defer/client": minor
---

add function configuration maxConcurrencyAction
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const INTERNAL_VERSION = 5;
export const INTERNAL_VERSION = 6;
export const RETRY_MAX_ATTEMPTS_PLACEHOLDER = 13;
19 changes: 6 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ export interface ExecutionMetadata {
[key: string]: string;
}

export interface DeferredFunctionOptions {
delay?: Duration | Date;
metadata?: ExecutionMetadata;
discardAfter?: Duration | Date;
}

// https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range/70307091#70307091
type Enumerate<
N extends number,
Expand Down Expand Up @@ -180,12 +174,6 @@ export interface RetryPolicy {
maxInterval: number;
}

export interface DeferOptions {
retry?: boolean | number | Partial<RetryPolicy>;
concurrency?: Concurrency;
maxDuration?: number;
}

export type DeferableFunction = (...args: any) => Promise<any>;

export interface ExecutionOptions {
Expand All @@ -205,6 +193,7 @@ export interface DeferredFunctionConfiguration {
retry?: boolean | number | Partial<RetryPolicy>;
concurrency?: Concurrency;
maxDuration?: number;
maxConcurrencyAction?: "keep" | "cancel";
}

function defaultRetryPolicy(): RetryPolicy {
Expand All @@ -217,7 +206,9 @@ function defaultRetryPolicy(): RetryPolicy {
};
}

function parseRetryPolicy(options?: DeferOptions): RetryPolicy {
function parseRetryPolicy(
options?: DeferredFunctionConfiguration
): RetryPolicy {
const retryPolicy: RetryPolicy = defaultRetryPolicy();
switch (typeof options?.retry) {
case "boolean": {
Expand Down Expand Up @@ -279,6 +270,7 @@ export function defer<F extends DeferableFunction>(
retry: parseRetryPolicy(config),
concurrency: config?.concurrency,
maxDuration: config?.maxDuration,
maxConcurrencyAction: config?.maxConcurrencyAction,
};

return wrapped;
Expand All @@ -302,6 +294,7 @@ defer.cron = function (
cron: cronExpr,
concurrency: config?.concurrency,
maxDuration: config?.maxDuration,
maxConcurrencyAction: config?.maxConcurrencyAction,
};

return wrapped;
Expand Down
Loading