|
| 1 | +# Recording and replaying renders |
| 2 | + |
| 3 | +`Liquid::TemplateRecorder` captures successful template renders so they can be |
| 4 | +replayed without the application's file system or Drop implementations. |
| 5 | +Recording does not wrap or replace assigns, so the recorded render has the same |
| 6 | +semantics as a normal render. |
| 7 | + |
| 8 | +```ruby |
| 9 | +Liquid::TemplateRecorder.record("render.json") do |
| 10 | + template = Liquid::Template.parse(source) |
| 11 | + template.render!(assigns) |
| 12 | +end |
| 13 | + |
| 14 | +replayer = Liquid::TemplateRecorder.replay_from("render.json", mode: :verify) |
| 15 | +replayer.render # raises if the output changed |
| 16 | +``` |
| 17 | + |
| 18 | +A recording contains the root template, every parsed partial, partial contents, |
| 19 | +plain Hash/Array values resolved by the template, properties actually read from `Liquid::Drop` objects, |
| 20 | +filter-call diagnostics, engine options, and the rendered output. Drop instance |
| 21 | +variables are never inspected. An unsupported Ruby object raises |
| 22 | +`Liquid::TemplateRecorder::SerializationError` rather than silently producing a |
| 23 | +recording that cannot be replayed. |
| 24 | + |
| 25 | +## Storage formats |
| 26 | + |
| 27 | +A `.json` destination is written atomically after the recording block succeeds. |
| 28 | +It contains a session with every render performed by the block. |
| 29 | + |
| 30 | +A `.jsonl` destination is append-only. Each successful top-level render is one |
| 31 | +compact, self-contained JSON line. This is the recommended format for production |
| 32 | +sampling: a process failure can lose at most the render being written, writers |
| 33 | +are serialized with `flock`, and a recording can be replayed by index. |
| 34 | + |
| 35 | +A destination may instead be any writer object responding to `write(record)`. The |
| 36 | +writer receives one self-contained recording Hash per successful render. Liquid |
| 37 | +does not own or close injected writers, so applications can publish records to |
| 38 | +Kafka, object storage, or another transport without coupling that transport to |
| 39 | +the recorder. |
| 40 | + |
| 41 | +```ruby |
| 42 | +Liquid::TemplateRecorder.record(kafka_writer) do |
| 43 | + template.render!(assigns) |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +```ruby |
| 48 | +Liquid::TemplateRecorder.replay_from("renders.jsonl") # last render |
| 49 | +Liquid::TemplateRecorder.replay_from("renders.jsonl", index: 0) # first render |
| 50 | +Liquid::TemplateRecorder.records("renders.jsonl") # inspect all |
| 51 | +``` |
| 52 | + |
| 53 | +Compression is intentionally separate from the schema. In particular, one |
| 54 | +long-lived compressed stream makes appending, recovery, and selecting a render |
| 55 | +harder. Compress rotated `.jsonl` files with the storage system of your choice; |
| 56 | +a future compressed writer can use one independent frame per record without a |
| 57 | +schema change. |
| 58 | + |
| 59 | +Recording sessions are thread-local. Nested sessions in the same thread are |
| 60 | +rejected. Existing application register names and the one-argument |
| 61 | +`FileSystem#read_template_file` API remain unchanged. |
| 62 | + |
| 63 | +## Replay modes |
| 64 | + |
| 65 | +* `:compute` runs filters normally. Pass application filters with |
| 66 | + `replayer.render(filters: MyFilters)`. |
| 67 | +* `:strict` returns each exact recorded filter result and rejects a changed |
| 68 | + filter sequence. This can replay application-specific or nondeterministic |
| 69 | + filters without loading their implementations. |
| 70 | +* `:verify` computes normally and raises when the final output differs. |
0 commit comments