Skip to content

Instantly share code, notes, and snippets.

@japaz
Created November 25, 2011 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japaz/1394490 to your computer and use it in GitHub Desktop.
Save japaz/1394490 to your computer and use it in GitHub Desktop.
7L7W Erlang - Day3
% Monitor the translate_service and restart it should it die.
-module(doctor_translate).
-export([loop/0]).
loop() ->
process_flag(trap_exit, true),
receive
new ->
io:format("Creating and monitoring process.~n"),
register(translator, spawn_link(fun translate_service:loop/0)),
loop();
{'EXIT', From, Reason} ->
io:format("The translate service ~p died with reason ~p.", [From, Reason]),
io:format(" Restarting. ~n"),
self() ! new,
loop()
end.
% Make the Doctor process restart itself if it should die.
-module(doctor_translate2).
-export([loop/0]).
loop() ->
process_flag(trap_exit, true),·
receive
new ->
io:format("Creating and monitoring process.~n"),
register(translator, spawn_link(fun translate_service:loop/0)),
loop();
{'EXIT', From, Reason} ->·
case Reason of
doctor ->
io:format("The doctor service ~p died with reason ~p.", [From, Reason]),
io:format(" Restarting. ~n"),
exit(whereis(translator), translator),
Doctor = spawn(fun doctor_translate2:loop/0),
Doctor ! new;
_Else ->
io:format("The translate service ~p died with reason ~p.", [From, Reason]),
io:format(" Restarting. ~n"),
self() ! new,·
loop()
end····
end.
% An OTP service that will restart a process if it dies
% http://learnyousomeerlang.com/supervisors
% Documentation for building a simple OTP server
% http://learnyousomeerlang.com/what-is-otp
% http://learnyousomeerlang.com/
1> c(doctor_translate2).
{ok,doctor_translate2}
2> c(translate_service).
{ok,translate_service}
3> Doctor = spawn(fun doctor_translate2:loop/0).
<0.43.0>
4> Doctor ! new.
Creating and monitoring process.
new
5> translate_service:translate(translator, "casa").
"house"
6> exit(Doctor, doctor).
The doctor service <0.31.0> died with reason doctor.true
Restarting.
Creating and monitoring process.
7> translate_service:translate(translator, "blanca").
"white"
8> exit(whereis(translator), translator).
The translate service <0.50.0> died with reason translator.true
Restarting.
Creating and monitoring process.
9> translate_service:translate(translator, "blan").
The translate service <0.53.0> died with reason {translate,service,die,at,
{23,5,57}}."I don't understand."
Restarting.
Creating and monitoring process.
10> translate_service:translate(translator, "blanca").
"white"
% Monitor the translate_service and restart it should it die.
-module(translate_service).
-export([loop/0, translate/2]).
loop() ->
receive
{From, "casa"} ->
From ! "house",
loop();
{From, "blanca"} ->
From ! "white",
loop();
{From, _} ->
From ! "I don't understand.",
exit({translate,service,die,at,erlang:time()})
end.
translate(To, Word) ->
To ! {self(), Word},
receive
Translation -> Translation
end.
@tuafeeqahmed
Copy link

Can you write "money denomination" code in erlang Language ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment