Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- DSL-configured `shutdown_after` and `hibernate_after` options were ignored at runtime — processes never shut down even when `shutdown_after` was set in the `options` block. The values were stored for introspection but never read during process startup. `Server.init/1` and `Server.start_link/1` now fall back to the module's DSL-configured values when options are not explicitly passed.

### Added

- Built-in `state.id` field on every object's `State` struct, automatically set to the object's ID at init time
Expand Down
16 changes: 14 additions & 2 deletions lib/durable_object/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ defmodule DurableObject.Server do
def start_link(opts) do
module = Keyword.fetch!(opts, :module)
object_id = Keyword.fetch!(opts, :object_id)
hibernate_after = Keyword.get(opts, :hibernate_after, @default_hibernate_after)

hibernate_after =
Keyword.get_lazy(opts, :hibernate_after, fn ->
if function_exported?(module, :__durable_object__, 1),
do: module.__durable_object__(:hibernate_after),
else: @default_hibernate_after
end)

GenServer.start_link(__MODULE__, opts,
name: via_tuple(module, object_id),
Expand Down Expand Up @@ -122,7 +128,13 @@ defmodule DurableObject.Server do
def init(opts) do
module = Keyword.fetch!(opts, :module)
object_id = Keyword.fetch!(opts, :object_id)
shutdown_after = Keyword.get(opts, :shutdown_after)

shutdown_after =
Keyword.get_lazy(opts, :shutdown_after, fn ->
if function_exported?(module, :__durable_object__, 1),
do: module.__durable_object__(:shutdown_after)
end)

repo = Keyword.get(opts, :repo)
prefix = Keyword.get(opts, :prefix)
default_state = module.__durable_object__(:default_state)
Expand Down
42 changes: 42 additions & 0 deletions test/durable_object/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,49 @@ defmodule DurableObject.ServerTest do
end
end

defmodule ShutdownHandler do
use DurableObject

state do
field(:data, :string, default: nil)
end

handlers do
handler(:get)
end

options do
shutdown_after(50)
end

def handle_get(state) do
{:reply, state, state}
end
end

describe "shutdown_after" do
test "DSL-configured shutdown_after is respected" do
id = unique_id("dsl-shutdown")

{:ok, pid} = Server.start_link(module: ShutdownHandler, object_id: id)

assert Process.alive?(pid)
Process.sleep(100)
refute Process.alive?(pid)
end

test "explicit shutdown_after overrides DSL config" do
id = unique_id("dsl-shutdown-override")

{:ok, pid} =
Server.start_link(module: ShutdownHandler, object_id: id, shutdown_after: 200)

Process.sleep(100)
assert Process.alive?(pid)
Process.sleep(150)
refute Process.alive?(pid)
end

test "process shuts down after timeout" do
{:ok, pid} =
Server.start_link(
Expand Down