Skip to content

Instantly share code, notes, and snippets.

@fishcakez
Created April 2, 2013 21:32
Show Gist options
  • Save fishcakez/5296408 to your computer and use it in GitHub Desktop.
Save fishcakez/5296408 to your computer and use it in GitHub Desktop.
Rest for one restart bug.
-module(restforbug).
-export([start/0]).
start() ->
ok = application:start(restforbug),
exit(whereis(restforbug_first), bye).
-module(restforbug_app).
-behaviour(application).
%% callbacks
-export([start/2, stop/1]).
%% callbacks
start(_StartType, _StartArgs) ->
restforbug_sup:start_link().
stop(_State) ->
ok.
-module(restforbug_server).
-behaviour(gen_server).
%% api
-export([start_link/1]).
%% callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
start_link(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [], []).
init([]) ->
true = 4 =/= ets:update_counter(restforbug, counter, {2, 1}),
{ok, undefined}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-module(restforbug_sup).
-behaviour(supervisor).
%% api
-export([start_link/0]).
%% callbacks
-export([init/1]).
%% api
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% callbacks
init([]) ->
ets:new(restforbug, [named_table, public]),
ets:insert(restforbug, {counter, 0}),
First = {restforbug_first,
{restforbug_server, start_link, [restforbug_first]},
permanent, 5000, worker, [restforbug_server]},
Second = {restforbug_second,
{restforbug_server, start_link, [restforbug_second]},
permanent, 5000, worker, [restforbug_server]},
{ok, {{rest_for_one, 3, 1}, [First, Second]}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment