How to trigger elixir supervisor tree termination from a supervised worker process
I am trying to terminate whole supervision tree from a supervised worker process. Here is my supervision tree:
+--------------------------+
| |
+--------+ Sup1: Dynamic Supervisor +---------+
| | | |
| +-------------+------------+ |
| | |
| | |
v v v
+------------------+ +------------------+ +------------------+
| | | | | |
| Job1: Supervisor | | Job2: Supervisor | | Job3: Supervisor |
| | | | | |
+------------------+ +-+-------- +---+--+ +------------------+
| |
| |
| |
| |
v v
+-------------------+ +--------------+
| | | |
| Progress Monitor: | | Work: Worker |
| Worker | | |
| | +--------------+
+-------------------+
Process life cycle:
- A
Job
is started via:DynamicSupervisor.start_child(__MODULE__, spec)
- Each job is a supervision tree as well: 1 supervisor (restart strategy -
one_for_one
) -> 2 workers
Progress Monitor
worker knows when the given job is done- On job done,
Progress Monitor
worker makes an attempt to terminate the whole job supervision tree, by calling:DynamicSupervisor.terminate_child(__MODULE__, pid)
Progress Monitor
is expected to do cleanup steps interminate
callback - it is trapping exit signals
Problems and observations:
DynamicSupervisor.terminate_child
is a blocking call, which means it waits for all child processes to terminate as well, including the calling process -Progress Monitor
Progress Monitor
is in a deadlock and can not terminate. Parent supervisor sends:kill
signal, which does not triggerterminate
callback
Quick workarounds:
Call
DynamicSupervisor.terminate_child
fromProgress Monitor
worker asynchronously:
spawn(fn -> DynamicSupervisor.terminate_child(__MODULE__, pid) end)
Define shutdown strategy for
Sup1: Dynamic Supervisor
:
shutdown: 5_000
It will wait at most 5s for a job supervision tree termination and then it will send
shutdown
exit signal. This will ensureterminate
callback being called forProgress Monitor
process.
Not happy with both of them.
Questions:
- How to trigger supervision tree termination from a worker process and avoid deadlocks?
- If terminating supervision tree from a worker is not the best practice, what is the recommended way then?
- Any recommendations how to redesign supervision tree to make graceful termination easier?
erlang elixir otp supervisor
add a comment |
I am trying to terminate whole supervision tree from a supervised worker process. Here is my supervision tree:
+--------------------------+
| |
+--------+ Sup1: Dynamic Supervisor +---------+
| | | |
| +-------------+------------+ |
| | |
| | |
v v v
+------------------+ +------------------+ +------------------+
| | | | | |
| Job1: Supervisor | | Job2: Supervisor | | Job3: Supervisor |
| | | | | |
+------------------+ +-+-------- +---+--+ +------------------+
| |
| |
| |
| |
v v
+-------------------+ +--------------+
| | | |
| Progress Monitor: | | Work: Worker |
| Worker | | |
| | +--------------+
+-------------------+
Process life cycle:
- A
Job
is started via:DynamicSupervisor.start_child(__MODULE__, spec)
- Each job is a supervision tree as well: 1 supervisor (restart strategy -
one_for_one
) -> 2 workers
Progress Monitor
worker knows when the given job is done- On job done,
Progress Monitor
worker makes an attempt to terminate the whole job supervision tree, by calling:DynamicSupervisor.terminate_child(__MODULE__, pid)
Progress Monitor
is expected to do cleanup steps interminate
callback - it is trapping exit signals
Problems and observations:
DynamicSupervisor.terminate_child
is a blocking call, which means it waits for all child processes to terminate as well, including the calling process -Progress Monitor
Progress Monitor
is in a deadlock and can not terminate. Parent supervisor sends:kill
signal, which does not triggerterminate
callback
Quick workarounds:
Call
DynamicSupervisor.terminate_child
fromProgress Monitor
worker asynchronously:
spawn(fn -> DynamicSupervisor.terminate_child(__MODULE__, pid) end)
Define shutdown strategy for
Sup1: Dynamic Supervisor
:
shutdown: 5_000
It will wait at most 5s for a job supervision tree termination and then it will send
shutdown
exit signal. This will ensureterminate
callback being called forProgress Monitor
process.
Not happy with both of them.
Questions:
- How to trigger supervision tree termination from a worker process and avoid deadlocks?
- If terminating supervision tree from a worker is not the best practice, what is the recommended way then?
- Any recommendations how to redesign supervision tree to make graceful termination easier?
erlang elixir otp supervisor
It seems to me that theProgress Monitor
is unnecessary. Could you explain the reason why theWorker
cannot simply do its thing and then terminate with reasonnormal
? It seems like you could be usingTask.Supervisor
for this purpose.
– Paweł Obrok
Nov 14 '18 at 12:55
add a comment |
I am trying to terminate whole supervision tree from a supervised worker process. Here is my supervision tree:
+--------------------------+
| |
+--------+ Sup1: Dynamic Supervisor +---------+
| | | |
| +-------------+------------+ |
| | |
| | |
v v v
+------------------+ +------------------+ +------------------+
| | | | | |
| Job1: Supervisor | | Job2: Supervisor | | Job3: Supervisor |
| | | | | |
+------------------+ +-+-------- +---+--+ +------------------+
| |
| |
| |
| |
v v
+-------------------+ +--------------+
| | | |
| Progress Monitor: | | Work: Worker |
| Worker | | |
| | +--------------+
+-------------------+
Process life cycle:
- A
Job
is started via:DynamicSupervisor.start_child(__MODULE__, spec)
- Each job is a supervision tree as well: 1 supervisor (restart strategy -
one_for_one
) -> 2 workers
Progress Monitor
worker knows when the given job is done- On job done,
Progress Monitor
worker makes an attempt to terminate the whole job supervision tree, by calling:DynamicSupervisor.terminate_child(__MODULE__, pid)
Progress Monitor
is expected to do cleanup steps interminate
callback - it is trapping exit signals
Problems and observations:
DynamicSupervisor.terminate_child
is a blocking call, which means it waits for all child processes to terminate as well, including the calling process -Progress Monitor
Progress Monitor
is in a deadlock and can not terminate. Parent supervisor sends:kill
signal, which does not triggerterminate
callback
Quick workarounds:
Call
DynamicSupervisor.terminate_child
fromProgress Monitor
worker asynchronously:
spawn(fn -> DynamicSupervisor.terminate_child(__MODULE__, pid) end)
Define shutdown strategy for
Sup1: Dynamic Supervisor
:
shutdown: 5_000
It will wait at most 5s for a job supervision tree termination and then it will send
shutdown
exit signal. This will ensureterminate
callback being called forProgress Monitor
process.
Not happy with both of them.
Questions:
- How to trigger supervision tree termination from a worker process and avoid deadlocks?
- If terminating supervision tree from a worker is not the best practice, what is the recommended way then?
- Any recommendations how to redesign supervision tree to make graceful termination easier?
erlang elixir otp supervisor
I am trying to terminate whole supervision tree from a supervised worker process. Here is my supervision tree:
+--------------------------+
| |
+--------+ Sup1: Dynamic Supervisor +---------+
| | | |
| +-------------+------------+ |
| | |
| | |
v v v
+------------------+ +------------------+ +------------------+
| | | | | |
| Job1: Supervisor | | Job2: Supervisor | | Job3: Supervisor |
| | | | | |
+------------------+ +-+-------- +---+--+ +------------------+
| |
| |
| |
| |
v v
+-------------------+ +--------------+
| | | |
| Progress Monitor: | | Work: Worker |
| Worker | | |
| | +--------------+
+-------------------+
Process life cycle:
- A
Job
is started via:DynamicSupervisor.start_child(__MODULE__, spec)
- Each job is a supervision tree as well: 1 supervisor (restart strategy -
one_for_one
) -> 2 workers
Progress Monitor
worker knows when the given job is done- On job done,
Progress Monitor
worker makes an attempt to terminate the whole job supervision tree, by calling:DynamicSupervisor.terminate_child(__MODULE__, pid)
Progress Monitor
is expected to do cleanup steps interminate
callback - it is trapping exit signals
Problems and observations:
DynamicSupervisor.terminate_child
is a blocking call, which means it waits for all child processes to terminate as well, including the calling process -Progress Monitor
Progress Monitor
is in a deadlock and can not terminate. Parent supervisor sends:kill
signal, which does not triggerterminate
callback
Quick workarounds:
Call
DynamicSupervisor.terminate_child
fromProgress Monitor
worker asynchronously:
spawn(fn -> DynamicSupervisor.terminate_child(__MODULE__, pid) end)
Define shutdown strategy for
Sup1: Dynamic Supervisor
:
shutdown: 5_000
It will wait at most 5s for a job supervision tree termination and then it will send
shutdown
exit signal. This will ensureterminate
callback being called forProgress Monitor
process.
Not happy with both of them.
Questions:
- How to trigger supervision tree termination from a worker process and avoid deadlocks?
- If terminating supervision tree from a worker is not the best practice, what is the recommended way then?
- Any recommendations how to redesign supervision tree to make graceful termination easier?
erlang elixir otp supervisor
erlang elixir otp supervisor
asked Nov 14 '18 at 10:31
mkorszunmkorszun
2,60732232
2,60732232
It seems to me that theProgress Monitor
is unnecessary. Could you explain the reason why theWorker
cannot simply do its thing and then terminate with reasonnormal
? It seems like you could be usingTask.Supervisor
for this purpose.
– Paweł Obrok
Nov 14 '18 at 12:55
add a comment |
It seems to me that theProgress Monitor
is unnecessary. Could you explain the reason why theWorker
cannot simply do its thing and then terminate with reasonnormal
? It seems like you could be usingTask.Supervisor
for this purpose.
– Paweł Obrok
Nov 14 '18 at 12:55
It seems to me that the
Progress Monitor
is unnecessary. Could you explain the reason why the Worker
cannot simply do its thing and then terminate with reason normal
? It seems like you could be using Task.Supervisor
for this purpose.– Paweł Obrok
Nov 14 '18 at 12:55
It seems to me that the
Progress Monitor
is unnecessary. Could you explain the reason why the Worker
cannot simply do its thing and then terminate with reason normal
? It seems like you could be using Task.Supervisor
for this purpose.– Paweł Obrok
Nov 14 '18 at 12:55
add a comment |
1 Answer
1
active
oldest
votes
Just call it in async task Task.async(fn -> Process.exit(Sup1, :shutdown) end)
it will terminate Sup1 and with it all children will shutdown
EDIT:
If you need prettier solution, it depends what elese you need. In most cases, I create Bootstrapper worker that will do initialization and some other stuff. You could add easily other features.
So considering above, and just roughly speaking, I would add in a layer above (AppSupervisor
), Another DynamicSupervisor so it can start Bootstrapper and pass self()
to it (or register it under local name to avoid this injection). After that, on start, Bootstrap worker will start Sup1 (your dynamic supervisor) and await for other messages, e.g. :terminate_sup1
that will shutdown Sup1
process. Later, in some of below workers you can shutdown Sup1
by casting :terminate_sup1
message to bootstraper. Also there is a door that allow you to start again Sup1 when another message is sent to bootstrap worker.
Further more, if you just need to shutdown Sup1, just go with Task. But if you need control, then put it into single worker process that should have control over it, when it is up or down.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298072%2fhow-to-trigger-elixir-supervisor-tree-termination-from-a-supervised-worker-proce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just call it in async task Task.async(fn -> Process.exit(Sup1, :shutdown) end)
it will terminate Sup1 and with it all children will shutdown
EDIT:
If you need prettier solution, it depends what elese you need. In most cases, I create Bootstrapper worker that will do initialization and some other stuff. You could add easily other features.
So considering above, and just roughly speaking, I would add in a layer above (AppSupervisor
), Another DynamicSupervisor so it can start Bootstrapper and pass self()
to it (or register it under local name to avoid this injection). After that, on start, Bootstrap worker will start Sup1 (your dynamic supervisor) and await for other messages, e.g. :terminate_sup1
that will shutdown Sup1
process. Later, in some of below workers you can shutdown Sup1
by casting :terminate_sup1
message to bootstraper. Also there is a door that allow you to start again Sup1 when another message is sent to bootstrap worker.
Further more, if you just need to shutdown Sup1, just go with Task. But if you need control, then put it into single worker process that should have control over it, when it is up or down.
add a comment |
Just call it in async task Task.async(fn -> Process.exit(Sup1, :shutdown) end)
it will terminate Sup1 and with it all children will shutdown
EDIT:
If you need prettier solution, it depends what elese you need. In most cases, I create Bootstrapper worker that will do initialization and some other stuff. You could add easily other features.
So considering above, and just roughly speaking, I would add in a layer above (AppSupervisor
), Another DynamicSupervisor so it can start Bootstrapper and pass self()
to it (or register it under local name to avoid this injection). After that, on start, Bootstrap worker will start Sup1 (your dynamic supervisor) and await for other messages, e.g. :terminate_sup1
that will shutdown Sup1
process. Later, in some of below workers you can shutdown Sup1
by casting :terminate_sup1
message to bootstraper. Also there is a door that allow you to start again Sup1 when another message is sent to bootstrap worker.
Further more, if you just need to shutdown Sup1, just go with Task. But if you need control, then put it into single worker process that should have control over it, when it is up or down.
add a comment |
Just call it in async task Task.async(fn -> Process.exit(Sup1, :shutdown) end)
it will terminate Sup1 and with it all children will shutdown
EDIT:
If you need prettier solution, it depends what elese you need. In most cases, I create Bootstrapper worker that will do initialization and some other stuff. You could add easily other features.
So considering above, and just roughly speaking, I would add in a layer above (AppSupervisor
), Another DynamicSupervisor so it can start Bootstrapper and pass self()
to it (or register it under local name to avoid this injection). After that, on start, Bootstrap worker will start Sup1 (your dynamic supervisor) and await for other messages, e.g. :terminate_sup1
that will shutdown Sup1
process. Later, in some of below workers you can shutdown Sup1
by casting :terminate_sup1
message to bootstraper. Also there is a door that allow you to start again Sup1 when another message is sent to bootstrap worker.
Further more, if you just need to shutdown Sup1, just go with Task. But if you need control, then put it into single worker process that should have control over it, when it is up or down.
Just call it in async task Task.async(fn -> Process.exit(Sup1, :shutdown) end)
it will terminate Sup1 and with it all children will shutdown
EDIT:
If you need prettier solution, it depends what elese you need. In most cases, I create Bootstrapper worker that will do initialization and some other stuff. You could add easily other features.
So considering above, and just roughly speaking, I would add in a layer above (AppSupervisor
), Another DynamicSupervisor so it can start Bootstrapper and pass self()
to it (or register it under local name to avoid this injection). After that, on start, Bootstrap worker will start Sup1 (your dynamic supervisor) and await for other messages, e.g. :terminate_sup1
that will shutdown Sup1
process. Later, in some of below workers you can shutdown Sup1
by casting :terminate_sup1
message to bootstraper. Also there is a door that allow you to start again Sup1 when another message is sent to bootstrap worker.
Further more, if you just need to shutdown Sup1, just go with Task. But if you need control, then put it into single worker process that should have control over it, when it is up or down.
edited Nov 14 '18 at 13:48
answered Nov 14 '18 at 13:32
Milan JaricMilan Jaric
4,65821831
4,65821831
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298072%2fhow-to-trigger-elixir-supervisor-tree-termination-from-a-supervised-worker-proce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems to me that the
Progress Monitor
is unnecessary. Could you explain the reason why theWorker
cannot simply do its thing and then terminate with reasonnormal
? It seems like you could be usingTask.Supervisor
for this purpose.– Paweł Obrok
Nov 14 '18 at 12:55