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 support for concurrency option when running locally #107

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
46 changes: 11 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
randomUUID,
sanitizeFunctionArguments,
} from "./utils.js";
import { execLocally, setupFn } from "./local.js";

const withDelay = (dt: Date, delay: Duration): Date =>
new Date(dt.getTime() + parseDuration(delay)!);
Expand All @@ -31,39 +32,6 @@ function getHTTPClient(): HTTPClient | undefined {

export const deferEnabled = () => !!getEnv("DEFER_TOKEN");

async function execLocally(
id: string,
fn: any,
args: any
): Promise<client.FetchExecutionResponse> {
let state: client.ExecutionState = "succeed";
let originalResult: any;
try {
originalResult = await fn(...args);
} catch (error) {
const e = error as Error;
state = "failed";
originalResult = {
name: e.name,
message: e.message,
cause: e.cause,
stack: e.stack,
};
}

let result: any;
try {
result = JSON.parse(JSON.stringify(originalResult || ""));
} catch (error) {
const e = error as Error;
throw new DeferError(`cannot serialize function return: ${e.message}`);
}

const response = { id, state, result };
__database.set(id, response);

return response;
}

async function enqueue<F extends DeferableFunction>(
func: DeferredFunction<F>,
Expand Down Expand Up @@ -105,7 +73,7 @@ async function enqueue<F extends DeferableFunction>(

const id = randomUUID();
__database.set(id, { id: id, state: "started" });
execLocally(id, originalFunction, functionArguments);
execLocally(id, func, functionArguments)
return { id };
}

Expand Down Expand Up @@ -139,6 +107,8 @@ export type Concurrency = Range<0, 51>;
export type NextRouteString = `/api/${string}`;

export interface Manifest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Manifest type must not be extended for local-only features. I suggest creating a new type that extends this one.

id: string;
name: string;
version: number;
cron?: string;
retry?: RetryPolicy;
Expand Down Expand Up @@ -249,12 +219,16 @@ export function defer<F extends DeferableFunction>(

wrapped.__fn = fn;
wrapped.__metadata = {
id: randomUUID(),
name: fn.name,
version: INTERNAL_VERSION,
retry: parseRetryPolicy(config),
concurrency: config?.concurrency,
maxDuration: config?.maxDuration,
};

setupFn(wrapped.__metadata)

return wrapped;
}

Expand All @@ -271,6 +245,8 @@ defer.cron = function (

wrapped.__fn = fn;
wrapped.__metadata = {
id: randomUUID(),
name: fn.name,
version: INTERNAL_VERSION,
retry: parseRetryPolicy(config),
cron: cronExpr,
Expand Down Expand Up @@ -361,7 +337,7 @@ export function awaitResult<F extends DeferableFunction>(
} else {
const id = randomUUID();
__database.set(id, { id: id, state: "started" });
response = await execLocally(id, originalFunction, functionArguments);
response = await execLocally(id, fn, functionArguments);
}

if (response.state === "failed") {
Expand Down
76 changes: 76 additions & 0 deletions src/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ExecutionState, FetchExecutionResponse } from "./client.js";
import { DeferError } from "./errors.js";
import {
DeferredFunction,
__database,
DeferableFunction,
Manifest,
} from "./index";
import { Queue } from "./queue.js";

type Invocation<F extends DeferableFunction> = {
id: string;
func: DeferredFunction<F>
args: any
oncomplete: ((result: FetchExecutionResponse<any>) => void) | undefined
};
type FunctionConfiguration<F extends DeferableFunction> = Manifest & {
queue: Queue<Invocation<F>>;
};
const fns: Record<string, FunctionConfiguration<any>> = {};

export function setupFn<F extends DeferableFunction>(metadata: Manifest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps 'registerFunction' would be a more suitable name in this case.

fns[metadata.id] = {
...metadata,
queue: new Queue<Invocation<F>>(invoke, metadata.concurrency),
};
}

export function execLocally<F extends DeferableFunction>(
id: string,
func: DeferredFunction<F>,
args: any
): Promise<FetchExecutionResponse<any>> {
return new Promise(resolve => {
fns[func.__metadata.id]!.queue.push({
id,
func,
args,
oncomplete: resolve
});
})
}

async function invoke(invocation: Invocation<any>) {
__database.set(invocation.id, { id: invocation.id, state: "started" });
let state: ExecutionState = "succeed";
let originalResult: any;
try {
originalResult = await invocation.func.__fn(...invocation.args);
} catch (error) {
const e = error as Error;
state = "failed";
originalResult = {
name: e.name,
message: e.message,
cause: e.cause,
stack: e.stack,
};

console.error('Error in deferred function ' + invocation.func.__fn.name + ' (invocation ' + invocation.id + '):\n', e)
}

let result: any;
try {
result = JSON.parse(JSON.stringify(originalResult || ""));
} catch (error) {
const e = error as Error;
throw new DeferError(`cannot serialize function return: ${e.message}`);
}

const response = { id: invocation.id, state, result }
__database.set(invocation.id, response);

invocation.oncomplete?.(response)

}
33 changes: 33 additions & 0 deletions src/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Concurrency } from ".";

export class Queue<T> extends Array<T> {
private running = 0;

constructor(public invoker: (item: T) => Promise<void>, public concurrency?: Concurrency) {
super();
Object.setPrototypeOf(this, Queue.prototype)
}

override push(...items: T[]): number {
const len = super.push(...items);
this.next();
return len;
}

private next() {
if (!this.concurrency || this.running < this.concurrency) {
this.dequeue();
}
}

private dequeue() {
const item = this.shift();
if (item) {
++this.running;
this.invoker(item).finally(() => {
--this.running;
this.next();
});
}
}
}
Loading