Skip to main content
NetScriptdocs Search docs ⌘K v0.0.9
Browse documentation
View as Markdown

@netscript/plugin-workers

The deployable NetScript background-workers plugin: background jobs, task execution, workflow orchestration, a Workers API service, CLI commands, a ./scaffold adapter entrypoint, durable streams and Aspire process wiring. It binds the host plugin system to the worker primitives in @netscript/plugin-workers-core.

Sub-path exports

Export Entrypoint Purpose
@netscript/plugin-workers ./mod.ts Plugin manifest and inspection helper.
@netscript/plugin-workers/adapter-cli ./cli.ts Executable plugin-adapter CLI entrypoint and its shared CLI protocol types.
@netscript/plugin-workers/aspire ./src/aspire/mod.ts Aspire AppHost contribution and resource ports.
@netscript/plugin-workers/cli ./src/cli/composition/main.ts Host-mounted workers CLI command group.
@netscript/plugin-workers/contracts ./contracts/v1/mod.ts Versioned oRPC worker service contracts and schemas.
@netscript/plugin-workers/doctor ./doctor.ts Static workers adapter used by plugin doctor.
@netscript/plugin-workers/jobs/health-check.ts ./jobs/health-check.ts Health-check job definition and its context, handler, and result contracts.
@netscript/plugin-workers/runtime ./bin/runtime.ts Generated job-registry loading and registration plus worker, scheduler, and combined-process start helpers.
@netscript/plugin-workers/scaffold ./scaffold.ts Plugin scaffold entrypoint types, logger, context, and result contracts.
@netscript/plugin-workers/service ./services/mod.ts Inert caller-policy factory.
@netscript/plugin-workers/services ./services/src/main.ts Workers API service entrypoint.
@netscript/plugin-workers/streams ./streams/mod.ts Browser-facing StreamDB factory and entity schemas.
@netscript/plugin-workers/streams/server ./streams/server.ts Server-side durable stream producer wiring.
@netscript/plugin-workers/worker ./worker/mod.ts Worker and Scheduler runtime processes.

Plugin manifest (@netscript/plugin-workers)

The plugin manifest consumed by the NetScript host. Inspect it with inspectPlugin from @netscript/plugin.

Symbol Kind Description
workersPlugin const Plugin manifest for NetScript background workers.

@netscript/plugin-workers/aspire

Symbol Kind Description
WorkersAspireContribution class Aspire contribution for the NetScript workers plugin.
AspireNSPluginContribution class Base class plugins extend to contribute Aspire resources to an AppHost.
AspireBuilder interface Port implemented by adapters that emit Aspire AppHost resources.
AspireResource interface Resource descriptor returned by Aspire builder ports.
AspireResourceKind type alias Aspire resource kinds produced by plugin contributions.
CacheSpec interface Cache resource spec consumed by AspireBuilder.
ContainerSpec interface Container resource spec consumed by AspireBuilder.
ContributionContext interface Context passed to plugin Aspire contributions during AppHost composition.
DatabaseSpec interface Database resource spec consumed by AspireBuilder.
DenoBackgroundSpec interface Deno background process spec consumed by AspireBuilder.
DenoServiceSpec interface Deno service resource spec consumed by AspireBuilder.
EnvSource type alias Source for an environment variable value in an AppHost composition.

@netscript/plugin-workers/cli

Symbol Kind Description
WorkersCli class CLI command group for @netscript/plugin-workers.
PluginCli class Abstract base class for plugin-owned CLI command groups.
PluginCliArgs interface Command arguments passed to plugin CLI handlers.
PluginCliCommand interface A mounted CLI command handler.
PluginCliResult interface Result returned by plugin CLI handlers.
CliCommand class Stub-only base contract for CLI command implementations.
WorkersCommand class Stub-only contract for workers CLI commands.
WorkersCommandDefinition interface Definition supplied to worker CLI commands.
AddJobCommand class Create a worker job definition file.
AddTaskCommand class Create a worker task definition file.
CompileRegistryCommand class Compile the static worker registry used by compiled runtimes.
ConfigEditCommand class Edit a worker runtime configuration topic.
ConfigPublishCommand class Publish a worker runtime configuration topic.
DisableCommand class Disable a worker job.
EnableCommand class Enable a worker job.
ListJobsCommand class List configured worker jobs.
ListTasksCommand class List configured worker tasks.
LogsCommand class Show logs for a worker execution.
RunJobCommand class Run a configured worker job.
WorkersCliCommand class Base class for concrete workers CLI commands.
WORKERS_CLI_COMMANDS const Worker CLI commands exposed through the plugin CLI subpath.
WorkersCliBackend interface Backend invoked by concrete command classes.
WorkersCliCategory type alias Worker CLI command category used for grouped help output.
WorkersCliCommandDefinition interface Workers command definition mounted by the plugin CLI.
WorkersCliCommandName type alias Worker CLI command identifier.
WorkersCliFlagDefinition interface Flag metadata shown by host CLI help renderers.
workersCli const Default CLI instance used by the host CLI walker.

@netscript/plugin-workers/contracts

Versioned oRPC service contracts plus the request/response schemas and types used to generate worker service clients.

Symbol Kind Description
ExecutionRecord type alias Runtime execution record.
JobContext type alias Context supplied to runtime job handlers.
JobDefinition type alias Runtime job definition.
JobHandler type alias Function that executes a runtime job.
JobMessage type alias Message enqueued to trigger a job execution.
JobResult type alias Result returned by runtime job handlers.
RegisterJobInput type alias Input for registering a job definition.
RegisterTaskInput type alias Input for registering a task definition.
RuntimePermissions type alias Runtime permission bag accepted by task and job execution.
RuntimePermissionValue type alias Runtime permission value accepted by task and job execution.
TaskDefinition type alias Runtime task definition.
TaskExecutionOptions type alias Options supplied when executing a task.
TaskMessage type alias Message enqueued to trigger a task execution.
TaskResult type alias Result returned by task execution.
ExecutionFiltersSchema const Schema for list-executions filters.
ExecutionRecordResponseSchema const Schema for worker execution record responses.
JobCreateInputSchema const Schema for creating worker job definitions.
JobDefinitionResponseSchema const Schema for worker job definition responses.
JobFiltersSchema const Schema for list-jobs filters.
JobTriggerInputSchema const Schema for triggering a worker job by id.
JobUpdateInputSchema const Schema for updating worker job definitions.
JobUpdateWithIdSchema const Schema for updating a worker job definition by id.
SSEEventSchema const Schema for server-sent event payloads emitted by the workers service.
TaskDefinitionResponseSchema const Schema for worker task definition responses.
TaskFiltersSchema const Schema for list-tasks filters.
workersContract const Worker service contract definition for client generation.
workersContractV1 const Context-bindable worker service contract definition.
ContractSchema interface Package-owned structural schema surface for worker contracts.
ContractSchemaResult type alias Result returned by contract schema validation.
ExecutionRecordResponse type alias Public response returned for worker execution records.
JobDefinitionResponse type alias Public response returned for worker job definitions.
JobTriggerInput type alias Input accepted by the trigger-job procedure.
JobTriggerOutput type alias Output returned by the trigger-job procedure.
SSEEvent type alias Server-sent event payload emitted by the workers service.
TaskDefinitionResponse type alias Public response returned for worker task definitions.
TaskTriggerInput type alias Input accepted by the trigger-task procedure.
TaskTriggerOutput type alias Output returned by the trigger-task procedure.
WorkersContract type alias Explicit public contract shape for worker service clients.
WorkersContractV1 type alias Context-binding contract wrapper for the v1 worker contract.
WorkersRouter type alias Structural worker router returned after binding a context.

Trigger procedures (RPC routes)

workersContract exposes two enqueue procedures. A generated typed client (createServiceClient<typeof workersContract>({ contract: workersContract, serviceName: 'workers-api', routerName: 'workers' })) reaches them over /api/rpc/*; the same routes answer REST callers under /api/v1/workers/.... For each, the {id} path segment names the target: a request-body id is optional and ignored, and a missing id returns a typed VALIDATION_ERROR (HTTP 422) through the validationFailed contract helper before any KV write.

Procedure Route Input (JobTriggerInput / TaskTriggerInput) Output
triggerJob POST /jobs/{id}/trigger { id, payload?, priority?, delay?, correlationId?, traceparent?, tracestate? } { jobId, triggered }
triggerTask POST /tasks/{id}/trigger { id, payload?, priority?, delay?, correlationId? } { taskId, triggered }

See the background jobs guide for the typed-client walkthrough.

@netscript/plugin-workers/service

Symbol Kind Signature Description
startWorkersService function startWorkersService(context: PluginServiceContext, options: WorkersServiceOptions): Promise<RunningService> Start the existing Workers API with explicit native auth, CORS and listener options.
WorkersServiceOptions interface auth: ServiceAuthPolicy; cors?: CorsOptions; serve?: ServeOptions Caller-selected policy and listener configuration.

Import is inert; policy validation precedes factory I/O. serve.port, including zero, wins over context.env.PORT and process PORT. The returned native handle drains its listener without closing host-owned global KV. See Host workers and streams services for typed composition, discovery, credential ownership and the legacy adoption boundary.

The public signature also re-exports the native PluginServiceContext from @netscript/plugin/sdk; CorsOptions, RunningService, RunningServiceAddress, ServeOptions, ServiceApp, ServiceEnvironment, ServiceTlsOptions from @netscript/service; and AuthenticatorPort, AuthnOptions, AuthnRequest, AuthnResult, AuthorizerPort, AuthzDecision, AuthzOptions, AuthzRequest, Principal, ServiceAuthPolicy, ServiceGuardedAuthPolicy, ServicePublicAuthPolicy from @netscript/service/auth. These retain their native identity; application code can import those contracts directly from their owning modules.

@netscript/plugin-workers/services

Symbol Kind Description
PluginServiceContext interface Context supplied to a plugin service at runtime.
default function Starts the Workers API service using host-provided infrastructure.

@netscript/plugin-workers/streams

Browser-facing StreamDB factory and the entity schemas mirrored from worker execution state.

Symbol Kind Description
createWorkersStreamDB function Create a TanStack DB-backed StreamDB for worker execution and job entities.
WorkerExecution type alias Worker execution entity stored in the durable stream.
WorkerJob type alias Worker job entity stored in the durable stream.
WorkersStreamDB type alias Browser-facing StreamDB surface returned by the workers stream factory.
WorkerExecutionSchema const Stream entity schema for worker executions.
WorkerJobSchema const Stream entity schema for worker jobs.
workersStreamSchema const Stream schema definition for worker executions and jobs.
StreamSchemaDefinition type alias Structural stream schema definition map.
WorkersStreamDefinition type alias Durable stream definition for worker execution and job entities.
WorkersStreamSchema type alias Package-owned structural workers stream schema surface.
WorkerStreamCollectionDefinition interface Package-owned structural stream collection definition.
WorkerStreamEntitySchema interface Package-owned structural schema surface for worker stream entities.
WorkerStreamStandardSchema interface Standard Schema compatible public schema surface for stream entities.

@netscript/plugin-workers/streams/server

Server-side wiring that mirrors execution-state changes into the durable stream producer.

Symbol Kind Description
createStreamMutationHook function Create an ExecutionState mutation hook that publishes to the durable stream.
emitJobToStream function Emit a job entity to the durable stream.
getWorkersStreamProducer function Get (or create) the singleton workers execution stream producer.
ExecutionConcept type alias Execution concept discriminator.
ExecutionMutationHook type alias Hook invoked after execution state mutations.
ExecutionRecord type alias Worker execution record stored in KV.
ExecutionStatus type alias Execution status discriminator.
ExecutionTriggerType type alias Execution trigger discriminator.
WorkersStreamProducer type alias Durable stream producer type for the workers stream schema.
WorkerExecutionSchema const Stream entity schema for worker executions.
WorkerJobSchema const Stream entity schema for worker jobs.
workersStreamSchema const Stream schema definition for worker executions and jobs.
StreamSchemaDefinition type alias Structural stream schema definition map.
WorkerExecution type alias Worker execution entity stored in the durable stream.
WorkerJob type alias Worker job entity stored in the durable stream.
WorkersStreamDefinition type alias Durable stream definition for worker execution and job entities.
WorkersStreamSchema type alias Package-owned structural workers stream schema surface.
WorkerStreamCollectionDefinition interface Package-owned structural stream collection definition.
WorkerStreamEntitySchema interface Package-owned structural schema surface for worker stream entities.
WorkerStreamStandardSchema interface Standard Schema compatible public schema surface for stream entities.

@netscript/plugin-workers/worker

The long-running runtime processes: the Worker job/task consumer and the cron Scheduler.

Symbol Kind Description
ScheduledJobInfo interface Scheduled job info.
Scheduler class Scheduler process that loads scheduled jobs and dispatches cron ticks.
SchedulerOptions interface Scheduler configuration options.
JobContext type alias Context supplied to runtime job handlers.
JobDefinition type alias Runtime job definition.
JobHandler type alias Function that executes a runtime job.
JobResult type alias Result returned by runtime job handlers.
RuntimePermissions type alias Runtime permission bag accepted by task and job execution.
RuntimePermissionValue type alias Runtime permission value accepted by task and job execution.
StaticJobRegistry type alias Registry of statically imported runtime job handlers.
TaskDefinition type alias Runtime task definition.
TaskExecutionOptions type alias Options supplied when executing a task.
WorkerCronJob interface Cron job handle used by the workers scheduler.
WorkerCronScheduler interface Cron scheduler surface consumed by the workers scheduler.
WorkerSchedulerExecutionState interface Execution-state surface consumed by the workers scheduler.
WorkerSchedulerJobRegistry interface Job registry surface consumed by the workers scheduler.
Worker class Worker process that consumes queued jobs and tasks for one runtime instance.
QueueTriggerConfig interface Configuration for a queue that triggers a job when messages arrive.
WorkerCompleteExecutionOptions type alias Options for completing a worker execution record.
WorkerCreateExecutionOptions type alias Options for creating a worker execution record.
WorkerExecutionRecord interface Execution record returned by the worker execution-state port.
WorkerExecutionState interface Execution-state surface consumed by the worker process.
WorkerHealthStatus interface Health snapshot for a worker runtime.
WorkerJobRegistry interface Job registry surface consumed by the worker process.
WorkerOptions interface Worker configuration options.
WorkerPayloadSchema interface Structural validation schema accepted by worker queue triggers.
WorkerTaskExecutor interface Task executor surface consumed by the worker process.
WorkerTaskRegistry interface Task registry surface consumed by the worker process.
WorkerTaskResult type alias Task execution result surface consumed by the worker process.
WorkerPoolOptions type alias Options for the plugin-layer in-process job runner pool.

Core package

@netscript/plugin-workers-core is canonical for worker definition builders, runtime composition, registries, executors, streams, configuration and testing exports.

Examples

Resume a workflow idempotently

Define a workflow with defineWorkflow and run it step by step with WorkflowExecutor, both from @netscript/plugin-workers-core/workflow. After a step fails, resume the execution from the failed step; completed steps are skipped.

TypeScript
import { assertEquals } from "@std/assert";
import {
  defineWorkflow,
  WorkflowExecutor,
  MemoryWorkflowStateStore,
} from "@netscript/plugin-workers-core/workflow";
import type { TaskId } from "@netscript/plugin-workers-core/workflow";

Deno.test("WorkflowExecutor infrastructure-free testing and idempotent resume", async () => {
  const workflow = defineWorkflow("data-processing-pipeline")
    .taskStep("download", { taskId: "download-file" as TaskId })
    .taskStep("transform", { taskId: "transform-data" as TaskId })
    .build();

  const stateStore = new MemoryWorkflowStateStore();
  const executedSteps: string[] = [];
  let shouldFailTransform = true;

  // 1. Create a WorkflowExecutor with mock handlers for infrastructure-free testing
  const executor = new WorkflowExecutor({
    stateStore,
    runTaskStep: (step, _state) => {
      const taskId = step.taskId;
      executedSteps.push(taskId!);
      if (taskId === "transform-data" && shouldFailTransform) {
        return Promise.reject(new Error("Transform step failed!"));
      }
      return Promise.resolve({ success: true });
    },
  });

  // 2. Execute the workflow (it will fail on the transform step)
  const firstRunState = await executor.execute(workflow, {
    executionId: "pipeline-run-1",
  });

  assertEquals(firstRunState.status, "failed");
  assertEquals(firstRunState.currentStepIndex, 2); // Failed step is recorded, advancing cursor
  assertEquals(executedSteps, ["download-file", "transform-data"]);

  // 3. Reset the failure flag
  shouldFailTransform = false;
  executedSteps.length = 0; // Clear execution log

  // 4. Simulate operator intervention: reset failed step to replay/resume it
  const savedState = await stateStore.findState(workflow.id, "pipeline-run-1");
  if (savedState) {
    const resultsCopy = { ...savedState.results };
    delete resultsCopy.transform; // Remove the failed step result

    await stateStore.saveState({
      ...savedState,
      status: "pending",
      currentStepIndex: 1, // Set cursor back to the transform step index
      results: resultsCopy,
    });
  }

  // 5. Resume the workflow using the executor
  const resumedState = await executor.execute(workflow, {
    executionId: "pipeline-run-1",
  });

  assertEquals(resumedState.status, "completed");
  assertEquals(resumedState.currentStepIndex, 2); // Completed all 2 steps
  
  // Idempotency check: "download" step is skipped on resume, only "transform" is run
  assertEquals(executedSteps, ["transform-data"]);
});
Search all documentation

Type a term to search the documentation.