@langchain/langgraph
Version:
64 lines (63 loc) • 2.01 kB
JavaScript
//#region src/pregel/runtime.ts
/**
* Run-scoped control surface for cooperative draining.
*
* Intended for a single graph run. Create a fresh {@link RunControl} per run;
* reusing a control after {@link RunControl#requestDrain} leaves it drained.
*
* Safe to use from any concurrent context: the drain request is represented
* by a single field write, so no synchronization is needed for this signal.
* If more mutable state is added here, add synchronization.
*
* The intended use is hooking SIGTERM (or any external supervisor signal) to
* {@link RunControl#requestDrain} so an in-flight graph run can stop cleanly
* at the next superstep boundary and be resumed later from the saved
* checkpoint.
*
* @example
* ```typescript
* import { RunControl, GraphDrained } from "@langchain/langgraph";
*
* const control = new RunControl();
*
* // In a signal handler, supervisor, etc.:
* // control.requestDrain("sigterm");
*
* try {
* const result = await graph.invoke(input, { ...config, control });
* if (control.drainRequested) {
* // finished naturally on the same tick where drain was requested
* }
* } catch (e) {
* if (e instanceof GraphDrained) {
* // checkpoint saved; resume later with the same config
* } else {
* throw e;
* }
* }
* ```
*/
var RunControl = class {
#drainReason = void 0;
/**
* Request that the current run drain cooperatively, stopping at the next
* superstep boundary. Does not cancel work that is already running.
*
* @param reason - A short description of why the drain was requested.
* Surfaced on the resulting {@link GraphDrained} error.
*/
requestDrain(reason = "shutdown") {
this.#drainReason = reason;
}
/** Whether a drain has been requested for this run. */
get drainRequested() {
return this.#drainReason !== void 0;
}
/** The reason passed to {@link RunControl#requestDrain}, if any. */
get drainReason() {
return this.#drainReason;
}
};
//#endregion
exports.RunControl = RunControl;
//# sourceMappingURL=runtime.cjs.map