| Author |
Message |
|
| daws |
Posted: Tue Aug 28, 2007 8:41 pm |
|
|
|
Joined: 28 Aug 2007
Posts: 6
|
Hi, I want to get into OTP and I followed along with Joe's book and made a gen_server and supervisor. The server just has one [non-callback] function (divide) which takes two numbers and divides the first by the second. In Joe's example we have the area_server with a supervisor, and when it comes across a bad_match, the server is restarted by the supervisor. My server, when divide is called with the second argument being 0, crashes, restarts, crashes and then the supervisor terminates. Can anyone enlighten me to what I'm doing wrong? Attached are the two pertinent files.
P.S., I used the emacs skeletons to write them.
Thanks a lot! |
| Description: |
|
 Download |
| Filename: |
divide_sup.erl |
| Filesize: |
2.16 KB |
| Downloaded: |
1063 Time(s) |
| Description: |
|
 Download |
| Filename: |
divide_server.erl |
| Filesize: |
4.34 KB |
| Downloaded: |
1097 Time(s) |
|
|
| Back to top |
|
| khigia |
Posted: Wed Aug 29, 2007 4:09 am |
|
|
|
User
Joined: 27 Mar 2007
Posts: 52
|
Haven't read The Book ... so not sure what I'm talking about
My first assumption was that the trap_exit in supervisor cause the supervision tree to crash on first gen_server crash ... I was wrong, but not yet sure why ...
Second assumption gave better result:
I guess the divide_sup is started (linked!) from erlang shell, as well as the divide_server:divide/2 call ...
when the divide call fail, it causes the erlang process of the shell to crash (normal); but then the (linked) supervisor also crashes, and stop its gen_server child at the same time!
... I'm not clear, but try to make a bad call from another process, and you'll got the expected behaviour:
Code: spawn(fun() -> divide_server:divide(10,0) end).
hope this help |
|
|
|
| Back to top |
|
| daws |
Posted: Wed Aug 29, 2007 12:32 pm |
|
|
|
Joined: 28 Aug 2007
Posts: 6
|
| That does work! Is there any way, however to keep he shell from crashing while calling from the shell so I have an easier time debugging? |
|
|
|
| Back to top |
|
| khigia |
Posted: Wed Aug 29, 2007 12:54 pm |
|
|
|
User
Joined: 27 Mar 2007
Posts: 52
|
Starting the supervisor without link to the shell (using start instead of start_link) should do what you want.
It may also be possible to start the supervisor in a special process (following code is not tested):
Code: spawn(fun() -> divide_sup:start_link(), receive X -> ok end end)
(the supervisor is started, and the process wait) |
|
|
|
| Back to top |
|
| daws |
Posted: Wed Aug 29, 2007 1:55 pm |
|
|
|
Joined: 28 Aug 2007
Posts: 6
|
| I don't see a function called supervisor:start, though. |
|
|
|
| Back to top |
|
| daws |
Posted: Wed Aug 29, 2007 2:03 pm |
|
|
|
Joined: 28 Aug 2007
Posts: 6
|
| However the code you wrote does work. Thank you very much for your time! |
|
|
|
| Back to top |
|
| khigia |
Posted: Wed Aug 29, 2007 2:12 pm |
|
|
|
User
Joined: 27 Mar 2007
Posts: 52
|
oh, sorry, my bad ... didn't checked supervisor has start function.
Maybe a cleaner solution is to start the supervisor in one shell, and start another shell (connected to the same erlang node) for testing (or another job in the same shell, see CTRL-G h).
Hope this help. |
|
|
|
| Back to top |
|
| daws |
Posted: Fri Aug 31, 2007 1:44 pm |
|
|
|
Joined: 28 Aug 2007
Posts: 6
|
| I'll just have to do that or wrap it in a spawn function with the receive loop. Thanks for identifying the problem for me though. It was a great help. |
|
|
|
| Back to top |
|
|
|