batch
Appends each input to a temporary file until a delivery condition is met, and then runs the specified command in a sub-shell.
The delivery conditions are:
- the batch reaches
sizeinputs - the eval is about to send an input to a different channel
- the eval is about to wait for an input's timestamp in
--realtimemode - the eval finishes, or is cancelled (e.g via ctrl-c)
Like exec and stream, the command can use shell features and reference environment variables in the local .env.
The command gets the path to the batch file via the RNGO_BATCH_FILE variable. It also receives the contents of that batch file via stdin.
The stdout and stderr of the command are recorded as ambient outputs.
Parameters
commandrequiredThe command to run in a sub-shell once per batch file.
sizeoptionalThe maximum number of inputs per batch. Defaults to 10000.
splitoptionalSet to effect to write a separate file for each effect, and run the command once per file. See Effect Splitting.
formatoptionalHow to format each input before appending it to the batch file. See the format reference.
Delivery
When interleaving inputs between a batch channel and one or more other channels, rngo will always deliver the current batch channel file prior to sending inputs to the other channels to ensure any references are in place.
This tends to result in small batch files, and so should be avoided - batch channels ideally operate to the exclusion of other channels.
Once a command succeeds, the batch file is deleted. If the command exits with a non-zero status, the eval fails and the file is kept so you can inspect it.
Effect Splitting
Bulk loaders like PostgreSQL COPY, MySQL LOAD DATA LOCAL INFILE, SQLite .import and DuckDB read_csv are much faster than batched inserts, but expect one CSV file per table.
With split: effect, each batch writes one file per effect, then runs the command once per file. Paired with the csv format, the command can then leverage a bulk loader.
The command gets these additional variables:
RNGO_BATCH_EFFECT- the effect keyRNGO_BATCH_TABLE- the effect'smetadata.table, otherwise the effect keyRNGO_BATCH_TABLE_QUOTED- the table name in double quotes, e.g."Posts"
Also, size still counts the batch as a whole, not per effect.
Load Order
When delivering the batch, rngo avoids foreign key violations by ensuring that an effect's file will only be loaded after any effects it references through a reference generator or an effect trigger.
For the same reason, rngo guarantees that inputs from a effect that references itself are loaded in generation order (even if input schema changes - see below).
Schema Splitting
If an effect generates inputs with multiple schemas (e.g. with a top-level select generator), inputs that share a schema will be grouped together.
For object inputs, the command can reference a batch's keys via the following environment variables:
RNGO_BATCH_COLUMNS- the file's columns, comma-separated and unquoted, e.g.id,createdAtRNGO_BATCH_COLUMNS_QUOTED- the columns, each in double quotes, e.g."id","createdAt"
This reduces the size of each batch file, though, so you should prefer effect outputs with a consistent schema.
Log
Each run of the command writes a batch record to the log's metadata table, including the batch number, effect and table (with split: effect), first and last input IDs, size, start and end times and exit status. A failed command writes its row before the eval stops.
This makes it easy to write a signal that checks every batch loaded:
type: sql
query: SELECT count(*) FROM metadata WHERE type = 'batch' AND (data ->> 'exit_status') IS NOT 0
expect: result == 0Examples
PostgreSQL `-f`
Load each batch of SQL inserts in a single transaction with psql.
type: batch
command: >-
psql -X -q -1
-v ON_ERROR_STOP=1
-f "$RNGO_BATCH_FILE"
"$DATABASE_URL"
format:
type: sqlPostgreSQL `\copy`
Bulk load one CSV file per effect with \copy.
type: batch
split: effect
command: >-
psql -X
-v ON_ERROR_STOP=1
"$DATABASE_URL"
-c "\copy $RNGO_BATCH_TABLE_QUOTED ($RNGO_BATCH_COLUMNS_QUOTED)
FROM pstdin WITH (FORMAT csv)"
format:
type: csvNote that, unlike the prior example, this example reads the batch file from stdin.