Skip to content

Commit 0c94732

Browse files
committed
Flush all in case of sigterm
1 parent c6bca39 commit 0c94732

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/markdoc/application.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ defmodule Markdoc.Application do
3131
Supervisor.start_link(children, opts)
3232
end
3333

34+
@impl true
35+
def stop(_state) do
36+
doc_ids =
37+
Registry.select(Markdoc.DocRegistry, [
38+
{{:"$1", :_, :_}, [], [:"$1"]}
39+
])
40+
41+
Enum.each(doc_ids, fn doc_id ->
42+
try do
43+
Markdoc.DocServer.flush(doc_id)
44+
catch
45+
_ -> :ok
46+
end
47+
end)
48+
49+
:ok
50+
end
51+
3452
# Tell Phoenix to update the endpoint configuration
3553
# whenever the application is updated.
3654
@impl true

lib/markdoc/doc_server.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ defmodule Markdoc.DocServer do
100100
GenServer.cast(Markdoc.DocRegistry.via_tuple(doc_id), {:save_snapshot, snapshot_binary})
101101
end
102102

103+
@doc """
104+
Forces a flush to the configured storage backend.
105+
"""
106+
def flush(doc_id) when is_binary(doc_id) do
107+
GenServer.call(Markdoc.DocRegistry.via_tuple(doc_id), :flush)
108+
end
109+
103110
## Server Callbacks
104111

105112
@impl true
@@ -277,6 +284,12 @@ defmodule Markdoc.DocServer do
277284
{:reply, state.history, state}
278285
end
279286

287+
@impl true
288+
def handle_call(:flush, _from, state) do
289+
new_state = maybe_flush(state, :manual)
290+
{:reply, :ok, new_state}
291+
end
292+
280293
@impl true
281294
def handle_info(:check_inactivity, state) do
282295
# Periodic check for inactivity

0 commit comments

Comments
 (0)