-
Notifications
You must be signed in to change notification settings - Fork 7
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.