Erlang-主管不会重新启动工作进程

时间:2020-03-20 03:05:29

标签: erlang erlang-shell erlang-supervisor

我正在与Erlang OTP主管打交道,并希望与主管和工作人员一起构建一个简单的应用程序。

我已经创建了一个简单的基于 gen_server 的工作程序,该工作程序使用2个数字并先除以第二。如果第二个数字为0,则它​​将失败并显示错误。

我有一个主管,理论上应该在工作失败后重新开始工作。但是,当我启动该应用程序时,该工作程序在失败后不会重新启动。相反,外壳会碎裂。 我认为发生这种情况是由于import SwiftUI import Foundation import AlamofireImage import Alamofire import Combine struct ListCellView: View { @State var dataSource = DataSource() var viewModel: RestrauntListViewModel var imageLoader = ImageLoader() var body: some View { HStack { LoadableImageView(with: viewModel.imageURL.absoluteString) Spacer() VStack(alignment: .center, spacing: 1.0) { Text(viewModel.name) Image(dataSource.configureRatings(rating: viewModel.rating)) Text("\(viewModel.formattedDistance) miles") } .padding(.all) Spacer() } } } struct ListCellView_Previews: PreviewProvider { static var previews: some View { ListCellView(viewModel: RestrauntListViewModel(name: "in 3nout", imageURL: URL(string: "https://s3-media2.fl.yelpcdn.com/bphoto/FNYCY1myO6qlqXLTpGPyIA/o.jpg")!, distance: 4.94578764532, id: "34526434", rating: 4)) } } 在发生错误时的行为,但我不确定。


工人:

gen_server:call

主管:

-module(my_worker).
-behaviour(gen_server).
-export([init/1,handle_call/3,create/0,terminate/2]).

create() -> gen_server:start({local,my_calc},?MODULE,0,[]).

init(_) -> {ok, {}}.

handle_call({divide, _, 0}, _From, State) ->
  {stop, div_by_zero, State};
handle_call({divide, X, Y}, _From, State) ->
  {reply, X / Y, State}.

terminate(Reason,State) ->
  io:format("Terminated because of ~p", [Reason]).

Shell:

-module(my_supervisor).
-behaviour(supervisor).
-export([init/1,create/0]).

create() -> supervisor:start_link(?MODULE,{}).

init(_) ->
  {
    ok,
    {
       #{strategy => one_for_one, intensity => 10, period => 1000},
      [
        #{id => child_id1,
          start => {my_worker, create, []},
          restart => permanent,
          shutdown => 1000,
          type => worker,
          modules => [my_worker]
        }
      ]
    }
  }.

Running in shell

0 个答案:

没有答案