@@ -145,6 +145,12 @@ defmodule Supervisor do
145
145
and such. It is set automatically based on the `:start` value and it is rarely
146
146
changed in practice.
147
147
148
+ * `:significant` - a boolean indicating if the child process should be
149
+ considered significant with regards to automatic shutdown. Only `:transient`
150
+ and `:temporary` child processes can be marked as significant. This key is
151
+ optional and defaults to `false`. See section "Automatic shutdown" below
152
+ for more details.
153
+
148
154
Let's understand what the `:shutdown` and `:restart` options control.
149
155
150
156
### Shutdown values (:shutdown)
@@ -302,6 +308,10 @@ defmodule Supervisor do
302
308
* `:max_seconds` - the time frame in which `:max_restarts` applies.
303
309
Defaults to `5`.
304
310
311
+ * `:auto_shutdown` - the automatic shutdown option. It can be
312
+ `:never`, `:any_significant`, or `:all_significant`. Optional.
313
+ See the "Automatic shutdown" section.
314
+
305
315
* `:name` - a name to register the supervisor process. Supported values are
306
316
explained in the "Name registration" section in the documentation for
307
317
`GenServer`. Optional.
@@ -327,6 +337,27 @@ defmodule Supervisor do
327
337
328
338
To efficiently supervise children started dynamically, see `DynamicSupervisor`.
329
339
340
+ ### Automatic shutdown
341
+
342
+ Supervisors have the ability to automatically shut themselves down when child
343
+ processes marked as `:significant` exit.
344
+
345
+ Supervisors support different automatic shutdown options (through
346
+ the `:auto_shutdown` option, as seen above):
347
+
348
+ * `:never` - this is the default, automatic shutdown is disabled.
349
+
350
+ * `:any_significant` - if any significant child process exits, the supervisor
351
+ will automatically shut down its children, then itself.
352
+
353
+ * `:all_significant` - when all significant child processes have exited,
354
+ the supervisor will automatically shut down its children, then itself.
355
+
356
+ Only `:transient` and `:temporary` child processes can be marked as significant,
357
+ and this configuration affects the behavior. Significant `:transient` child
358
+ processes must exit normally for automatic shutdown to be considered, where
359
+ `:temporary` child processes may exit for any reason.
360
+
330
361
### Name registration
331
362
332
363
A supervisor is bound to the same name registration rules as a `GenServer`.
@@ -521,7 +552,8 @@ defmodule Supervisor do
521
552
@ type sup_flags ( ) :: % {
522
553
strategy: strategy ( ) ,
523
554
intensity: non_neg_integer ( ) ,
524
- period: pos_integer ( )
555
+ period: pos_integer ( ) ,
556
+ auto_shutdown: auto_shutdown ( )
525
557
}
526
558
527
559
@ typedoc "The supervisor reference"
@@ -532,6 +564,7 @@ defmodule Supervisor do
532
564
{ :strategy , strategy }
533
565
| { :max_restarts , non_neg_integer }
534
566
| { :max_seconds , pos_integer }
567
+ | { :auto_shutdown , auto_shutdown }
535
568
536
569
@ typedoc "Supported restart options"
537
570
@ type restart :: :permanent | :transient | :temporary
@@ -542,6 +575,9 @@ defmodule Supervisor do
542
575
@ typedoc "Supported strategies"
543
576
@ type strategy :: :one_for_one | :one_for_all | :rest_for_one
544
577
578
+ @ typedoc "Supported automatic shutdown options"
579
+ @ type auto_shutdown :: :never | :any_significant | :all_significant
580
+
545
581
@ typedoc """
546
582
Supervisor type.
547
583
@@ -561,7 +597,8 @@ defmodule Supervisor do
561
597
optional ( :restart ) => restart ( ) ,
562
598
optional ( :shutdown ) => shutdown ( ) ,
563
599
optional ( :type ) => type ( ) ,
564
- optional ( :modules ) => [ module ( ) ] | :dynamic
600
+ optional ( :modules ) => [ module ( ) ] | :dynamic ,
601
+ optional ( :significant ) => boolean ( )
565
602
}
566
603
567
604
@ doc """
@@ -611,7 +648,9 @@ defmodule Supervisor do
611
648
[ option | init_option ]
612
649
) :: { :ok , pid } | { :error , { :already_started , pid } | { :shutdown , term } | term }
613
650
def start_link ( children , options ) when is_list ( children ) do
614
- { sup_opts , start_opts } = Keyword . split ( options , [ :strategy , :max_seconds , :max_restarts ] )
651
+ { sup_opts , start_opts } =
652
+ Keyword . split ( options , [ :strategy , :max_seconds , :max_restarts , :auto_shutdown ] )
653
+
615
654
start_link ( Supervisor.Default , init ( children , sup_opts ) , start_opts )
616
655
end
617
656
@@ -646,6 +685,9 @@ defmodule Supervisor do
646
685
* `:max_seconds` - the time frame in seconds in which `:max_restarts`
647
686
applies. Defaults to `5`.
648
687
688
+ * `:auto_shutdown` - the automatic shutdown option. It can be either
689
+ `:never`, `:any_significant`, or `:all_significant`
690
+
649
691
The `:strategy` option is required and by default a maximum of 3 restarts
650
692
is allowed within 5 seconds. Check the `Supervisor` module for a detailed
651
693
description of the available strategies.
@@ -681,7 +723,15 @@ defmodule Supervisor do
681
723
682
724
intensity = Keyword . get ( options , :max_restarts , 3 )
683
725
period = Keyword . get ( options , :max_seconds , 5 )
684
- flags = % { strategy: strategy , intensity: intensity , period: period }
726
+ auto_shutdown = Keyword . get ( options , :auto_shutdown , :never )
727
+
728
+ flags = % {
729
+ strategy: strategy ,
730
+ intensity: intensity ,
731
+ period: period ,
732
+ auto_shutdown: auto_shutdown
733
+ }
734
+
685
735
{ :ok , { flags , Enum . map ( children , & init_child / 1 ) } }
686
736
end
687
737
@@ -805,7 +855,8 @@ defmodule Supervisor do
805
855
806
856
def child_spec ( module_or_map , overrides ) do
807
857
Enum . reduce ( overrides , init_child ( module_or_map ) , fn
808
- { key , value } , acc when key in [ :id , :start , :restart , :shutdown , :type , :modules ] ->
858
+ { key , value } , acc
859
+ when key in [ :id , :start , :restart , :shutdown , :type , :modules , :significant ] ->
809
860
Map . put ( acc , key , value )
810
861
811
862
{ key , _value } , _acc ->
0 commit comments