Skip to main content

Events and Listeners

Every infrastructure tool eventually gets a request it was not designed for. Post the plan to Slack. Write an audit record of who deployed what. Fail the build if anything is being destroyed on a Friday. The usual answer is a wrapper script that parses the tool's output, which breaks the next time the output changes.

Octo narrates its whole run as events. Everything you have read in a terminal is a listener subscribed to that stream, and the stream is public. Nothing in the framework has to change for you to build on it.

Listening​

import { Factory, OnEvent, TerraformFoldersWrittenEvent } from '@quadnix/octo';

export class ShopAuditListener {
@OnEvent(TerraformFoldersWrittenEvent)
onFoldersWritten (event: TerraformFoldersWrittenEvent): void {
console.error(`wrote ${event.payload.written.length} folders to ${event.payload.outputDir}`);
}
}

@Factory<ShopAuditListener>(ShopAuditListener)
export class ShopAuditListenerFactory {
private static instance: ShopAuditListener;

static async create (): Promise<ShopAuditListener> {
this.instance ??= new ShopAuditListener();
return this.instance;
}
}

A handler receives every event that is an instance of the type you named, which means subscribing to a base class catches all of its subclasses. @OnEvent(TransactionEvent) catches every transaction event there is, and @OnEvent(Event) catches everything Octo emits.

Handlers can be synchronous or async. The listener class is resolved through Octo's container, so it needs a @Factory alongside it.

The stream​

Events come in families, and you usually want a family rather than one event. ModuleEvent, ActionEvent and RulesEvaluatedEvent are emitted as they are; every other family is abstract, so you listen for it and Octo emits its members. The API lists every event and its payload.

FamilyEmitted when
CommandEventAn octo command starts, finishes, or fails. One command can run a lifecycle step, or none, as lock does.
ModeEventA lifecycle step starts, finishes, or fails. ModeStartedEvent, ModeFinishedEvent, ModeFailedEvent.
ModeResultEventA mode has a verdict. ValidateModeCompletedEvent carries the whole report; CommitModeCompletedEvent carries what was recorded; RunActionModeCompletedEvent what one external resource's action did, or why it did nothing.
TransactionEventThe graph is being diffed and actions are running. Model diffs, resource diffs, each action.
RegistrationEventA model, resource, anchor, overlay, or action registered itself at boot.
SerializationEventThe model or resource graph was turned into state, or read back from it.
StateEventState files are about to be written or were written, state was refreshed, and the lock was taken, refused, or released.
TerraformEventA Terraform command started or finished, each process started, printed something, or exited, and the folders were written.
RulesEvaluatedEventThe rules were checked, by generate before it writes anything or by check-rules, carrying what each rule found.
HookEventA hook callback ran.
ModuleEventA module's onInit() ran, carrying the module's ID and the inputs it ran with.
ActionEventAn action called this.log(), carrying its message, its metadata, and the node and diff the action ran for.

Two are worth knowing by name. ValidateModeCompletedEvent carries the full validate report, which is what you want for posting a summary onto a pull request. ModeFinishedEvent carries the mode and its duration, and is emitted with emitAndWait, so a listener that writes a file finishes before the process moves on.

Registering a listener​

From the definition file, for the two Octo ships:

settings:
listeners:
- type: JournalEventListener
- type: LoggingEventListener
level: info

LoggingEventListener is the terminal output. JournalEventListener is the journal, below. A new project lists both.

A listener of your own is registered at boot, which means driving Octo from TypeScript:

await octo.initialize(stateProvider, [{ type: ShopAuditListener }]);

There is no way to name your own listener class from octo.yaml today; the definition file's listeners block accepts the two built-in types only.

The journal​

The terminal that ran a command is often gone by the time somebody needs to know what it did: a CI job is cleaned up, a laptop is closed. JournalEventListener keeps the record on disk instead. It writes every event of every command into your state directory, failures included, along with everything Terraform printed and a copy of each state file the command wrote. Octo's state says what your infrastructure is now, and the journal says how it got there.

Each command writes a few files into .octo/, named after the command and an ID that starts with the time it started, so they sort in order:

journal.1791692243700-a690d91d.generate.json
journal.1791692244187-30dd727f.validate.json
journal.1791692244187-30dd727f.validate.terraform.json
journal.1791692253700-032b3e64.lock.json

The .terraform.json file holds what each Terraform and Terragrunt process printed, as it printed it. A long apply saves its journal every ten seconds, so a run that was killed keeps what it had done up to then.

When the lock is released, the files of that change move together into .octo/archives/<commitId>/, beside a copy of the state the change left. A change that needed three tries at apply has all three in its folder. Archives are never deleted for you.

Every file is JSON, so jq reads it. The error a command failed with, and the next command it suggested, if it had one:

jq -r '.entries[]? | select(.type == "CommandFailedEvent") | .payload.error.message, .payload.nextStep // empty' .octo/journal.*.json

The journal is gitignored in a new project. It records each module's inputs as given, so a secret passed as an input is in it in plain text. Nothing in Octo reads it back, and deleting it changes nothing about your infrastructure.

To read the files from code, their shapes are published as TypeScript types in @quadnix/octo-event-listeners/types, and @quadnix/octo-event-listeners/wording prints an entry as the terminal printed it.

Emitting your own​

Event is abstract, so an event of your own is a class extending it:

class DeployApprovedEvent extends Event<{ reviewer: string }> {}

EventService.getInstance().emit(new DeployApprovedEvent('deploy approved', { reviewer }));

Useful from inside a module or an action when something happens that your own listeners should react to, and it costs nothing when nobody is listening. The journal records Octo's own events only, so an event of yours reaches your listeners and nothing else.

What to build on it​

The three that come up most, and what each needs.

A pull request comment. Listen for ValidateModeCompletedEvent, take event.payload.report, and post it. The validate journal holds the same report in its ValidateModeCompletedEvent entry, if you would rather read a file than write a listener.

An audit trail. The journal already is one, file by file. To send it somewhere else as it happens, listen for CommandFinishedEvent and CommandFailedEvent: between them you get the command, the duration, and the error, every time.

A gate. A listener cannot stop a run; it observes. To refuse a change, use a hook, which sits in the call path and can throw.

Writing to the right stream​

If you print, print to stderr. Octo's logging listener writes there too, and stdout carries only a command's own output: octo lock prints the lock ID for a shell to capture, and Terraform reads octo run-action and octo get-response as JSON. A line on stdout breaks all three.