Optionalconfig: { enableCleanup?: boolean }Optional cleanup hook invoked during successful workflow completion.
The orchestrator calls this automatically when a workflow instance:
This executes AFTER final state persistence but BEFORE the completion event is emitted back to the initiator. This ordering ensures the final state is durably saved before cleanup runs, while allowing cleanup logic to complete before the workflow signals completion to external systems.
IMPORTANT: This hook is NOT called when orchestration execution fails (e.g., lock acquisition failure, state persistence failure, handler errors). It only executes for workflows that successfully reach their final state.
Receives the same state transition data as write() to enable intelligent cleanup decisions based on how the workflow completed and what changed in the final step.
This hook enables custom memory management strategies:
Implementations are not required to delete state immediately - they can implement whatever retention/archival strategy suits their operational needs.
Workflow instance identifier (event.subject)
Gets stored state for a machine instance
Machine instance ID
State data or null if not found
Stores state for a machine instance
Machine instance ID
State to store
In-memory implementation of machine state storage for single-instance NodeJS applications.
Example
Warning
Concurrency Limitations
This implementation is NOT concurrency-safe. Lock acquisition is not atomic, making it unsuitable for parallel event processing where multiple handlers might process events for the same workflow simultaneously.
Safe Usage:
Unsafe Usage:
For concurrent in-process event processing, use the concurrency-safe memory backend from the
@arvo-tools/concurrentpackage, which provides atomic lock operations suitable for parallel execution within a single process.Unsuitable For:
For these scenarios, implement or use a database-backed memory backend (Redis, PostgreSQL, DynamoDB, etc.) that provides distributed state persistence and atomic locking across instances. You can also explore the Arvo tool eco-system
@arvo-tools