Edward J. Stembler
Edward J. Stembler

Reputation: 2042

How to restart Erlang Supervised function with parameters?

I'm learning Erlang, and have a Supervisor question...

I have a function which requires 3 parameters (string, string, number). I want to Supervise that and make sure that if it fails, it gets restarted with the 3 parameters I passed it.

Is this something a Supervisor can handle, or do I need to look into some other concept?

Thanks.

Update 1/23/2016

One thing I want to mention... I have a list of 1439 entries. I need to create a Supervisor for each entry in that list. Each entry will incur different arguments. For example, here's some psuedo-code (reminiscent of Ruby):

(360..1799).each do |index|
  export(output_path, table_name, index) # Supervise this
end

This is triggered by a user interaction at runtime. The output_path and table_name are dynamic too, but won't change for a given batch. Unravelled, a run may look something like this:

export("/output/2016-01-23/", "temp1234", 360)
export("/output/2016-01-23/", "temp1234", 361)
export("/output/2016-01-23/", "temp1234", 362)
.
.

So if 361 fails, I need it restarted with "/output/2016-01-23/", temp1234, and 361.

Is this something I can do with a Supervisor?

Upvotes: 1

Views: 204

Answers (2)

Michael
Michael

Reputation: 3729

Yes, this is what supervisor does, but you mean "arguments", not "parameters".

For ordinary (not simple_one_for_one) supervisors your init/1 implementation returns a list of so called child specifications, each of which specifies a child that will be spawned, and the arguments that will be passed are provided as part of this child specification.

With simple_one_for_one supervisors you still have to provide a child specification, but you provide the arguments when you start each supervised child.

In all cases your supervised children will be restarted with the same arguments.

Upvotes: 1

Hamidreza Soleimani
Hamidreza Soleimani

Reputation: 2544

General Concepts

  • Erlang systems are divided into modules.
  • Each module are composed of functions.
  • Works are done by processes and they use functions to do it.
  • A supervisor is a process which supervises other processes.

So your function will sure be executed inside a process, and you can specify and supervise that process by a supervisor. This way when your function fails and crashes, the process that are executing it will crash as well. When the process crashes, its supervisor will find out and can act upon it based on a predefined restart strategy. These strategies could be one_for_one, one_for_all and rest_for_one.

You can check its manual for further options.

Upvotes: 0

Related Questions