assemblyline.sh

Hold a workpiece until someone decides

A waiting station checks each workpiece in its input/ against a decision made outside the line. Decided work goes on; undecided work stays where it is until the next run. For why waiting gets its own station, see Waiting gets its own station.

In this example, IN1 files invoices above the approval limit into IN1/output/needs_approval/, and a station before the wait has already created an approval request for each and saved it as approval-request.json. INW waits for the answer; IN2 posts what was approved.

The handler

src/stations/INW__await_approval/station.js:

import { defineStation, attachReport, attachWorkpieces, bin, listWorkpieces, processWorkpiece, renderLocal } from '@assemblyline/lib-factory';
import { z } from 'zod';
import fs from 'node:fs';
import path from 'node:path';
import { getDecision } from '../../utils/approvals.js'; // your client: 'approved' | 'rejected' | null

// Per invoice: read its approval. Approved or rejected → output/approved or output/rejected;
// undecided → held in INW/input.
export default defineStation({
  code: 'INW',
  configSchema: z.object({ batch_size: z.number().default(200) }),

  execute: async (config, { work_record }) => {
    const workpieces = {};
    const results = [];

    for (const workpiece_id of listWorkpieces(bin('INW', 'input')).slice(0, config.batch_size)) {
      const request_file = path.join(bin('INW', 'input'), workpiece_id, 'approval-request.json');
      const request = fs.existsSync(request_file) ? JSON.parse(fs.readFileSync(request_file, 'utf8')) : null;

      let decision = null;
      if (request) {
        try {
          decision = await getDecision(request.id);
        } catch (err) {
          // The approvals tool is down: hold, and say so in the report. Nothing is wrong with the invoice.
          results.push({ workpiece_id, outcome: 'held', detail: `approvals unreachable: ${err.message}` });
          continue;
        }
        if (!decision) {
          // Not decided yet: leave it in input/ without calling processWorkpiece.
          results.push({ workpiece_id, outcome: 'held' });
          continue;
        }
      }

      const run = await processWorkpiece({
        station: 'INW',
        workpiece_id,
        work_record_id: work_record.id,
        body: async (wp_doing) => {
          if (!request) throw new Error('no_request: approval-request.json is missing');
          fs.writeFileSync(path.join(wp_doing, 'decision.json'), JSON.stringify({ decision, seen_at: new Date().toISOString() }, null, 2));
          return decision;
        },
      });

      if (run.status === 'failed') {
        workpieces[workpiece_id] = 'failed';
        results.push({ workpiece_id, outcome: 'failed', detail: run.error.message });
        continue;
      }

      // processWorkpiece put it in output/; file it into the lane for its decision.
      const lane = bin('INW', 'output', decision);
      fs.mkdirSync(lane, { recursive: true });
      fs.rmSync(path.join(lane, workpiece_id), { recursive: true, force: true });
      fs.renameSync(path.join(bin('INW', 'output'), workpiece_id), path.join(lane, workpiece_id));

      workpieces[workpiece_id] = 'done';
      results.push({ workpiece_id, outcome: decision });
    }

    // Held workpieces haven't been processed, so they aren't reported.
    await attachWorkpieces(work_record.id, workpieces);
    await attachReport(work_record.id, renderLocal(import.meta.url, './report.md', { results }));

    const count = (outcome) => results.filter((r) => r.outcome === outcome).length;
    return { released_count: count('approved') + count('rejected'), held_count: count('held'), failed_count: count('failed') };
  },
});
  • Holding is doing nothing. An undecided workpiece is never passed to processWorkpiece, so it stays in input/ untouched.
  • Releasing is the normal path. processWorkpiece moves the workpiece to output/, and the station moves it on into output/approved/ or output/rejected/.
  • A broken workpiece still fails. A missing request file is thrown inside the body, so the workpiece goes to failed/ with its error: see Find out why a workpiece failed.
  • The report shows what is waiting. Count the held workpieces in the report, and name the ones held because the outside tool was down.

The station file

Run the waiting station on a schedule and turn its watch off:

{
  "short_code": "INW",
  "line": "IN",
  "name": "await_approval",
  "conveyor": { "from": "IN1/output/needs_approval" },
  "config": { "batch_size": 200 },
  "dependencies": ["IN1"],
  "schedule_enabled": true,
  "schedule_cron": "30 * * * *",
  "watch_enabled": false
}

Bin-watch starts a run whenever a station's own input/ holds a workpiece. A waiting station always holds some, so with watch_enabled it would run every 10 seconds. See Start stations.

The posting station pulls only the approved lane:

{ "short_code": "IN2", "conveyor": { "from": "INW/output/approved" } }