Finitomata.Persistency.DETS (Finitomata v0.41.0)

Copy Markdown View Source

Built-in disk-durable Finitomata.Persistency adapter backed by a :dets file.

It behaves exactly like Finitomata.Persistency.ETS (one {state, payload} snapshot per FSM, keyed by the FSM name) but the table is persisted to disk, so snapshots survive a full node restart. Both adapters are OTP built-ins and add no dependencies.

Single node

:dets is a single-node, single-file store with a 2GB file-size limit. For multi-node or large-scale persistence, back the FSM with a database via a custom Finitomata.Persistency implementation (for example through Finitomata.Persistency.Persistable).

Usage

Add the table owner to your supervision tree and point use Finitomata at this module:

children = [
  Finitomata.Persistency.DETS,
  {Finitomata.Supervisor, id: MyApp.Fsm}
]

defmodule MyApp.Order do
  use Finitomata, fsm: @fsm, persistency: Finitomata.Persistency.DETS
end

Configuration

The :dets file path (and optionally the table name) are configurable. The default path lives under System.tmp_dir!/0; set a stable location for production:

config :finitomata, Finitomata.Persistency.DETS,
  file: "/var/lib/my_app/finitomata.dets",
  table: :my_fsm_snapshots

Summary

Functions

The :dets file path in use (configurable via config :finitomata, Finitomata.Persistency.DETS)

Reads the snapshot stored for name, or nil

Reads the last persisted failure for name, or nil

Removes the snapshot and the last error stored for name

Starts the process owning the snapshot file; add it to your supervision tree

Flushes pending writes to disk

The :dets table name in use (configurable via config :finitomata, Finitomata.Persistency.DETS)

Functions

file()

@spec file() :: String.t()

The :dets file path in use (configurable via config :finitomata, Finitomata.Persistency.DETS)

get(name)

Reads the snapshot stored for name, or nil

last_error(name)

@spec last_error(Finitomata.fsm_name()) :: Finitomata.Persistency.error() | nil

Reads the last persisted failure for name, or nil

purge(name)

@spec purge(Finitomata.fsm_name()) :: :ok

Removes the snapshot and the last error stored for name

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the process owning the snapshot file; add it to your supervision tree

sync()

@spec sync() :: :ok | {:error, term()}

Flushes pending writes to disk

table()

@spec table() :: atom()

The :dets table name in use (configurable via config :finitomata, Finitomata.Persistency.DETS)