@@ -274,9 +274,9 @@ defmodule Commanded.Aggregates.Aggregate do
274274 end
275275
276276 @ doc false
277- def take_snapshot ( application , aggregate_module , aggregate_uuid ) do
277+ def take_snapshot ( application , aggregate_module , aggregate_uuid , timeout \\ 5_000 ) do
278278 name = via_name ( application , aggregate_module , aggregate_uuid )
279- GenServer . cast ( name , :take_snapshot )
279+ GenServer . call ( name , :take_snapshot , timeout )
280280 end
281281
282282 @ doc false
@@ -315,11 +315,14 @@ defmodule Commanded.Aggregates.Aggregate do
315315
316316 @ doc false
317317 @ impl GenServer
318- def handle_cast ( :take_snapshot , % Aggregate { } = state ) , do: do_take_snapshot ( state )
318+ def handle_call ( :take_snapshot , _from , % Aggregate { } = state ) do
319+ case do_take_snapshot ( state ) do
320+ { :ok , state } ->
321+ { :reply , :ok , state }
319322
320- @ impl GenServer
321- def handle_cast ( { :take_snapshot , lifespan_timeout } , % Aggregate { } = state ) do
322- do_take_snapshot ( % Aggregate { state | lifespan_timeout: lifespan_timeout } )
323+ { :error , _reason } = error ->
324+ { :reply , error , state }
325+ end
323326 end
324327
325328 @ doc false
@@ -353,7 +356,7 @@ defmodule Commanded.Aggregates.Aggregate do
353356
354357 response =
355358 if Snapshotting . snapshot_required? ( snapshotting , aggregate_version ) do
356- :ok = GenServer . cast ( self ( ) , { :take_snapshot , lifespan_timeout } )
359+ send ( self ( ) , { :take_snapshot , lifespan_timeout } )
357360
358361 # Don't reply with a lifetime because we just asked for a snapshot to
359362 # be taken. When it finishes, it will set the timeout.
@@ -410,6 +413,20 @@ defmodule Commanded.Aggregates.Aggregate do
410413 end
411414 end
412415
416+ @ doc false
417+ @ impl GenServer
418+ def handle_info ( { :take_snapshot , lifespan_timeout } , % Aggregate { } = state ) do
419+ state = % Aggregate { state | lifespan_timeout: lifespan_timeout }
420+
421+ case do_take_snapshot ( state ) do
422+ { :ok , state } ->
423+ noreply_with_lifespan ( state )
424+
425+ { :error , _error } ->
426+ noreply_with_lifespan ( state )
427+ end
428+ end
429+
413430 @ doc false
414431 @ impl GenServer
415432 def handle_info ( :timeout , % Aggregate { } = state ) do
@@ -622,18 +639,15 @@ defmodule Commanded.Aggregates.Aggregate do
622639
623640 Logger . debug ( describe ( state ) <> " recording snapshot" )
624641
625- state =
626- case Snapshotting . take_snapshot ( snapshotting , aggregate_version , aggregate_state ) do
627- { :ok , snapshotting } ->
628- % Aggregate { state | snapshotting: snapshotting }
642+ case Snapshotting . take_snapshot ( snapshotting , aggregate_version , aggregate_state ) do
643+ { :ok , snapshotting } ->
644+ { :ok , % Aggregate { state | snapshotting: snapshotting } }
629645
630- { :error , error } ->
631- Logger . warning ( describe ( state ) <> " snapshot failed due to: " <> inspect ( error ) )
646+ { :error , reason } = error ->
647+ Logger . warning ( describe ( state ) <> " snapshot failed due to: " <> inspect ( reason ) )
632648
633- state
634- end
635-
636- noreply_with_lifespan ( state )
649+ error
650+ end
637651 end
638652
639653 defp telemetry_wrong_expected_version ( context , from , state ) do
0 commit comments