| Author |
Message |
|
| kornerhill |
Posted: Sun Apr 25, 2010 12:17 pm |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
I have 2 scripts, superv3.erl creates a supervisor and gensvrcalc.erl provides a worker process with gen_server. They runs successfully, but gen_server:call fails. This is what I do in the shell:
D:\tmp\erl>erl -noshell -setcookie cat -sname foo -s superv3 start
start()init()initial value is 0.
and I tried to call gen_server:call and got an exception:
D:\tmp\erl>erl -setcookie cat -sname bar
Eshell V5.7.4 (abort with ^G)
(bar@hk)1> gen_server:call({gensvrcalc, foo@hk},{add, 80}).
** exception exit: {noproc,
{gen_server,call,[{gensvrcalc,foo@hk},{add,80}]}}
in function gen_server:call/2
But If I donnot use supervisor, gen_server:call always succeed:
D:\tmp\erl>erl -noshell -setcookie cat -sname foo -s gensvrcalc start_calc
initial value is 0.add
(bar@hk)2> gen_server:call({gensvrcalc, foo@hk},{add, 80}).
80
this is my code. Hope someone can figure me out.
%filename:superv3.erl
-module(superv3).
-compile(export_all).
-behaviour(supervisor).
start() ->
io:format("start()"),
supervisor:start_link(superv3, []).
init(Param) ->
io:format("init()"),
{ok,{{one_for_one, 100, 2},[{proc1, {gensvrcalc, start_calc, []}, permanent,500, worker, [gensvrcalc]}]}}.
%filename:gensvrcalc.erl
-module(gensvrcalc).
-compile(export_all).
-behaviour(gen_server).
start_calc() ->
start_calc(0).
start_calc(InitValue) ->
gen_server:start_link({local, gensvrcalc}, gensvrcalc, InitValue, []).
stop(Pid) ->
gen_server:cast(Pid, stop).
add(Pid, Number) ->
gen_server:call(Pid, {add, Number}).
sub(Pid, Number) ->
gen_server:call(Pid, {sub, Number}).
init(InitValue) ->
io:format("initial value is ~w.", [InitValue]),
{ok, InitValue}.
terminate(_Reason, _LoopData) ->
io:format("~w ~w", [_Reason, _LoopData]).
handle_cast(stop, LoopData) ->
{stop, normal, LoopData}.
handle_call({add, Data}, _From, LoopData) ->
Reply = LoopData + Data,
io:format("add"),
{reply, Reply, Reply}. |
|
|
| Back to top |
|
| zajda |
Posted: Sun Apr 25, 2010 1:19 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
instead of compile(export_all) input all requested by gen_srv api functions. The code works for me.
I also started every thing from command line (from erl shell). It is better for debugging (there is very few use-cases to start shell like you do). Btw, you have some typos in node names.
Couple advises:
- use api to gen_server (so use add, not gen_srv:call..), thats why we have it!
- all around OTP server is abbreviated to srv not to svr. It can cause of some mistakes later on
- never use export_all
- group in code api functions and internal functions (take a look on Emacs skeleton for gen_srv - that's a classic one;)
- use traditional function names (i.e. start_link), it is much easier to read
- build easy to use api to gen_srv. Your api requests Pid - user should not know Pid of something. It should be sorted out internally by gen_srv (btw. name of gen_srv is always registred, so you dont even need Pid)
Michał Zajda
------------------
Erlang Solutions Ltd |
|
|
| Back to top |
|
| kornerhill |
Posted: Sun Apr 25, 2010 2:55 pm |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
hi, zajda, thanks, I replaced export_all to export([...]) and runned the scripts in ONE shell, and they did work.
But What I really want to do is to test to send msg from one node to another, e.g., the supervisor and gen_server is running in node A, and gen_server:call() is called in another node B. I cannot work it out. Maybe I choose the wrong way to do it. Any ideas?
And I wonder how you run erlang scripts after they are released? not like this "erl -noshell -s script.erl func" ? |
|
|
| Back to top |
|
| zajda |
Posted: Sun Apr 25, 2010 3:38 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
1. supervisior
take a look at OTP documentation (http://www.erlang.org/doc/man/supervisor.html) especially start_child/2.
2. To distribute erlang systems or deploy it on production we use 'Targer Systems' and 'Releases'. In short, it is copy of ERTS and necessary libs from OTP + our app. There is lot of useful tools in OTP to create boot scripts, create tarball or upgrade code (system wide).
Doc is here: http://www.erlang.org/doc/design_principles/release_structure.html#id2272649
There are also some tools to handle code in simple way (mainly sets of scripts with makefiles to build nodes, applications etc).
keep up good fight with erlang
Michał Zajda
------------------
Erlang Solutions Ltd |
|
|
| Back to top |
|
| kornerhill |
Posted: Mon Apr 26, 2010 2:25 am |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
| It's weird. I tried it just now and it worked. Thanks for your kind help, ^_^. I still don not know why we need applicaiton, or when need we make an application since we can call our scripts directly from shell? |
|
|
| Back to top |
|
| zajda |
Posted: Mon Apr 26, 2010 7:56 am |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
hmm:) there are lot of benefits. In bigger system, you can define dependencies between apps, pass to it some initial config. The most significant, I think, is robustness of a whole system when we use release build of apps. When anything goes down system (monitored by heart scripts) restarts everything in right order and using proper strategy (like failover/takeover etc..)
What else.. it is also more independent and compatible. It is important when you have to distribute your system across many nodes.
Maybe some book would introduce you to this topic better than me;) |
|
|
| Back to top |
|
| babyjing |
Posted: Thu Jan 12, 2012 2:46 am |
|
|
|
Guest
|
| Popular nfl jerseys cheap are going to be scarce over the next three months.Then they're going to be more expensive.The most popular teams will experience the worst shortages, and the James Jones Jersey are breaking all sorts of popularity records this year, from on-field performance to stock sales to merchandise sales.To lure the customer towards their service, providers are leaving no stone unturned in order to provide several plans to make Josh Sitton Jersey.Thats why your website's critics are so vital, and ofcourse why companies must be alerted at all times.With the sport's new passing-crazed style of play challenging the traditional notion that Buffalo Sabres jerseys is a fundamental ingredient for postseason success, these Nick Collins Jersey will provide an intriguing look at the way to win in the NFL when the stakes are highest. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|