assemblyline.sh

Workpiece

A workpiece is a folder that holds a pointer.json. Stations pass it from bin to bin whole. See Workpieces.

The wp object

ctx.eachWorkpiece and ctx.seed call your function with wp:

MemberTypeWhat it is
idstringThe workpiece's id, its folder name
dirstringThe folder fn works in: the copy in doing/ for eachWorkpiece, the hidden output/.<id>/ for seed
readText(name)stringRead a file in the folder, as UTF-8
readJson(name)anyRead and parse a JSON file in the folder
writeText(name, text)Write a file in the folder
writeJson(name, value)Write value as JSON, indented two spaces
log(event, details)Append an event to the folder's log.jsonl. details is optional. Never throws: a failed write prints a warning.

name is relative to wp.dir. The read and write calls are synchronous and throw as fs.readFileSync and fs.writeFileSync do. For types: @param {import('@assemblyline/lib-factory').Workpiece} wp.

Files the lib writes in a workpiece

FileWrittenHolds
pointer.jsonby ctx.seed, when the workpiece is created{ workpiece_id, dedup_key, seeded_at, ...pointer }; dedup_key only when given. A station never writes it.
log.jsonlat every eventOne JSON line per event: { ts, station, wr, event, ...details }
error.jsonwhen fn throws{ failed_at, name, message, stack } and every other own property of the error, such as status or body
error.txtwhen fn throwsThe same, for reading: a long body is cut to 500 characters

The events the lib logs:

EventWhenDetails
workpiece_createdctx.seed created the workpiecededup_key, when given
conveyedthe conveyor moved it into a station's input/from; no wr
convey_failedthe conveyor couldn't move it; logged where it stayedfrom, error; no wr
station_startedeachWorkpiece copied it into doing/
station_completefn returnedsub_bin, when given
station_failedfn threw

StopError

import { StopError } from '@assemblyline/lib-factory';
throw new StopError('ledger_auth: the API key has expired');

Throw it from the function given to eachWorkpiece or seed when the station itself is broken, not the workpiece. The workpiece stays where it was, the batch stops, and the run fails as retryable: see Station context and Throw errors from a station.

listWorkpieces(bin_dir)

Returns the names of the workpieces directly in bin_dir, sorted: folders that hold a pointer.json and don't start with .. Returns [] when the folder doesn't exist. Sub-bins hold no pointer.json, so they aren't listed.

listWorkpieces(bin('IN1', 'failed'));   // ['inv-1001', 'inv-1004']

logEvent(dir, station, work_record_id, event, details)

Appends { ts, station, wr: work_record_id, event, ...details } to <dir>/log.jsonl, for a workpiece you hold outside fn. Inside fn, use wp.log(event, details). Like wp.log, it never throws.