Skip to content

About

Shared worker infrastructure for process engine workers

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

101 Commits

Folders and files

Repository files navigation

@fob/lib-worker

The worker framework for FinOpsBricks customer workers. Provides the polling loop, the defineStep() API, orchestrator HTTP integration, and bundled conveyor/PDF steps.

Install

npm install @fob/lib-worker@file:../../lib/lib-worker

Env Vars

Variable Required Default Purpose
ORCHESTRATOR_API_KEY Yes — Orchestrator API key (also identifies the org)
ORCHESTRATOR_API_SECRET Yes — Orchestrator API secret
WORKER_LOCATION Yes — Worker location identifier (sent as X-Location; orchestrator dispatches only stations whose line's location matches — lines own the location)
ORCHESTRATOR_URL No http://localhost:4005 Orchestrator server URL
POLL_INTERVAL_MS No 2000 Polling frequency (ms)
NODE_ENV No — When set to development, dev-mode behaviors apply (e.g. clearTemp becomes a no-op)

Path conventions are hard-coded relative to process.cwd():

  • Station bins: temp/stations/{station}/{type}/
  • Per-work-record scratch: temp/work_records/{work_record_id}/

Exports

Core Worker

Function Description
startWorker({ getHandler, validateOptions? }) Start the polling loop
validateEnv(options) Validate required env vars at startup
defineStep({ slug, name, inputSchema, outputSchema, execute }) Define a step handler with Zod validation
isStepDefinition(value) Type guard for defineStep output
getStepHandler(definition) Extract the executable handler from a step definition
createHandler(definition) Wrap a step definition into a handler function

Step Discovery

Function Description
discoverSteps(dir) Auto-discover step modules in a directory
createGetHandler(definitions) Build a getHandler from a definitions map

Station Triggering

Start alongside startWorker(). Each watcher triggers a watch_enabled station (from the worker's local .orchestrator/ files) when it has work and no run in flight.

Function Description
startBinWatcher() Every 10 s: trigger conveyor stations whose input or upstream source_bin holds a workpiece. Also starts the intake watcher, and warns at boot about stations nothing triggers
startIntakeWatcher() Every 10 s: trigger line-heads whose step 0 watch_path (an inbox folder or file) is non-empty. Started by startBinWatcher(); call it directly only if you don't use the bin-watcher

Orchestrator Integration

Function Description
attachDocument(work_record_id, title, content, step_slug) Attach a supporting markdown document
attachReport(work_record_id, content) Attach the final summary report
attachFile(work_record_id, title, buffer, filename, step_slug) Attach a binary file (Excel, PDF, etc.)
clearTemp(work_record_id) Clear temp files for a work record (no-op in dev mode)
findItemByExternalId(external_id) Look up an item by external ID
createItem(data) Create a new item
findOrCreateItem(external_id, data) Upsert by external ID
runProcess(process_slug, item) Trigger a process run

Template Rendering

Function Description
renderTemplate(template_name, data) Render an EJS template bundled with lib-worker
renderLocal(template_path, data) Render an EJS template from the worker's own templates dir
initTemplates(options) Initialize template loader (called by startWorker)

Config + Paths

Function Description
resolveConfig(step, work_record) Resolve a step config from process definition + runtime context
bin(station, ...segments) Resolve a station bin path (auto-creates)
workRecordDir(work_record_id) Per-work-record scratch directory (auto-creates)
workRecordFile(work_record_id, ...segments) Per-work-record file path (auto-creates parents)

File + Step Helpers

Function Description
moveWorkpieces({ source_dir, target_dir, batch_size }) Move workpieces (folders with a pointer.json) between bins
listWorkpieces(bin_dir) / isWorkpieceDir(dir) The workpiece test shared by the conveyor and the bin-watcher
processWorkpiece({ station, workpiece_id, work_record_id, body }) 5-bin doing-bin contract helper for workpiece-mode steps
cleanupOrphanedDoing() Clean up stuck doing/ entries
logEvent(wp, station, work_record_id, event) Append a {ts, station, wr, event} line to {wp}/log.jsonl
pooled(items, options, fn) Run an async function over items with bounded concurrency
stripFrontmatter(markdown) Strip YAML frontmatter
countWords(text) Word count helper

Bundled Steps

Slug Export Description
lib-worker:move_files moveFilesStep Conveyor: move workpieces between assembly-line bins (supports moves array)

Usage

import 'dotenv/config';
import { startWorker, createGetHandler, discoverSteps } from '@fob/lib-worker';

const definitions = await discoverSteps('./src/steps');
startWorker({ getHandler: createGetHandler(definitions) });
import { defineStep, attachDocument } from '@fob/lib-worker';
import { z } from 'zod';

export default defineStep({
  slug: 'EX1_fetch_data',
  name: 'Fetch data',
  inputSchema: z.object({ account_id: z.string() }),
  outputSchema: z.object({ row_count: z.number() }),
  async execute({ input, work_record, step }) {
    const data = await fetchSomething(input.account_id);
    await attachDocument(work_record.id, 'Raw data', JSON.stringify(data, null, 2), step.slug);
    return { row_count: data.length };
  },
});

See Also

  • FDE Handbook → step-patterns/ for step authoring patterns (defineStep, schemas, document attachment, error handling)
  • FDE Handbook → worker-internals/ for startWorker lifecycle, handler resolution, task structure
  • Platform Handbook → architecture/worker-internals/ for framework internals

About

Shared worker infrastructure for process engine workers

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages