Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 65834e8

Browse files
committed
Merge pull request #11 from GameAnalytics/unnamed_scheduler
Add start_link/4 to start a scheduler without registering a name for it
2 parents 426e719 + e3f19d3 commit 65834e8

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/gascheduler.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
%% API
88
-export([start_link/5,
9+
start_link/4,
910
stop/1,
1011
execute/2,
1112
add_worker_node/2,
@@ -80,6 +81,12 @@ start_link(Name, Nodes, Client, MaxWorkers, MaxRetries) ->
8081
gen_server:start_link({local, Name}, ?MODULE,
8182
[Nodes, Client, MaxWorkers, MaxRetries], []).
8283

84+
-spec start_link(worker_nodes(), client(), max_workers(), max_retries())
85+
-> {ok, pid()}.
86+
start_link(Nodes, Client, MaxWorkers, MaxRetries) ->
87+
gen_server:start_link(?MODULE,
88+
[Nodes, Client, MaxWorkers, MaxRetries], []).
89+
8390
-spec stop(atom()) -> ok.
8491
stop(Name) ->
8592
gen_server:call(Name, stop).
@@ -196,7 +203,10 @@ handle_cast(tick, State = #state{ticks = Ticks, pending = Pending}) ->
196203

197204
handle_cast({Result, Worker, MFA}, State = #state{running = Running,
198205
client = Client}) ->
199-
{registered_name, Name} = process_info(self(), registered_name),
206+
Name = case process_info(self(), registered_name) of
207+
{registered_name, N} -> N;
208+
_ -> self() %% no name, just use pid
209+
end,
200210
Client ! {Name, Result, node(Worker), MFA},
201211
{noreply, State#state{running = remove_worker(Worker, Running)}};
202212

test/gascheduler_test.erl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ gascheduler_test_() ->
2020
{spawn, {timeout, 60, ?_test(node_down())}},
2121
{spawn, {timeout, 60, ?_test(unfinished())}},
2222
{spawn, {timeout, 60, ?_test(permanent_failure())}},
23-
{spawn, {timeout, 120, ?_test(exit_handling())}}
23+
{spawn, {timeout, 120, ?_test(exit_handling())}},
24+
{spawn, ?_test(unnamed_scheduler())}
2425
]}.
2526

2627

@@ -381,6 +382,26 @@ exit_handling() ->
381382

382383
ok.
383384

385+
%% Start a scheduler with no name
386+
%% Assert no name is registered for the pid of the scheduler
387+
%% Execute a task through the scheduler
388+
%% assert that work is finished
389+
unnamed_scheduler() ->
390+
Client = self(),
391+
Nodes = [node()],
392+
393+
{ok, Pid} = gascheduler:start_link(Nodes, Client, 1, 1),
394+
395+
%% See erlang:process_info/2 on why we get a list when there is no name
396+
%% registered.
397+
?assertEqual([], process_info(Pid, registered_name)),
398+
399+
ok = test_tasks(Pid, 1, Nodes),
400+
401+
ok = gascheduler:stop(Pid),
402+
403+
ok.
404+
384405

385406
%%
386407
%% Utilities
@@ -463,15 +484,18 @@ kill_if(Node) ->
463484
end.
464485

465486
test_tasks(NumTasks, Nodes) ->
487+
test_tasks(test, NumTasks, Nodes).
488+
489+
test_tasks(Scheduler, NumTasks, Nodes) ->
466490
Tasks = lists:seq(1, NumTasks),
467491
ok = lists:foreach(
468492
fun(Id) ->
469-
ok = gascheduler:execute(test, {gascheduler_test, sleep_100, [Id]})
493+
ok = gascheduler:execute(Scheduler, {gascheduler_test, sleep_100, [Id]})
470494
end, Tasks),
471495
Received = lists:map(
472496
fun(_) ->
473497
receive
474-
{test, {ok, Id}, Node, {Mod, Fun, Args}} ->
498+
{Scheduler, {ok, Id}, Node, {Mod, Fun, Args}} ->
475499
?assertEqual(gascheduler_test, Mod),
476500
?assertEqual(sleep_100, Fun),
477501
?assertEqual(length(Args), 1),

0 commit comments

Comments
 (0)