forked from uwiger/jobs
-
Notifications
You must be signed in to change notification settings - Fork 3
Shared counters
uwiger edited this page Apr 15, 2012
·
1 revision
It is possible to create a named counter, and have multiple queues refer to it.
This has the effect of regulating a set of queues using a single credit pool.
In the following example, we demonstrate the following:
- Create a named counter,
c1
, with a limit of 1. - Create one queue,
q1
, which refers to the named counterc1
. - Use
jobs:info(queues)
to verify that the queue was created. - Create another queue,
q2
after jobs has started, just to show how that's done. Again, verify withjobs:info(queues)
. - Spawn a number of jobs for each queue. Note that jobs tries to dispatch jobs fairly from the queues, and only one job gets to run at a time (since the shared counter had a limit of 1).
Eshell V5.9 (abort with ^G)
1> application:load(jobs).
ok
2> application:set_env(jobs,counters, [{c1,[{limit,1}]}]).
ok
3> application:set_env(jobs,queues,[{q1,[{regulators,[{named_counter,c1,1}]}]}]).
ok
4> application:start(jobs).
ok
5> jobs:info(queues).
[{queue,[{name,q1},
{mod,jobs_queue},
{type,fifo},
{group,undefined},
{regulators,[{counter,c1,1}]},
{max_time,undefined},
{max_size,undefined},
{latest_dispatch,0},
{check_interval,infinity},
{oldest_job,undefined},
{timer,undefined},
{check_counter,0},
{waiters,[]},
{st,{st,16400}}]}]
6> jobs:add_queue(q2,[{named_counter,c1,1}]).
ok
7> jobs:info(queues).
[{queue,[{name,q1},
{mod,jobs_queue},
{type,fifo},
{group,undefined},
{regulators,[{counter,c1,1}]},
{max_time,undefined},
{max_size,undefined},
{latest_dispatch,0},
{check_interval,infinity},
{oldest_job,undefined},
{timer,undefined},
{check_counter,0},
{waiters,[]},
{st,{st,16400}}]},
{queue,[{name,q2},
{mod,jobs_queue},
{type,fifo},
{group,undefined},
{regulators,[{counter,c1,1}]},
{max_time,undefined},
{max_size,undefined},
{latest_dispatch,0},
{check_interval,infinity},
{oldest_job,undefined},
{timer,undefined},
{check_counter,0},
{waiters,[]},
{st,{st,24596}}]}]
8> [spawn_link(fun() -> jobs:run(Q, fun() -> io:fwrite("~p running~n", [{Q,N}]), timer:sleep(1000), io:fwrite("~p done.~n", [{Q,N}]) end) end) || Q <- [q1,q2], N <- lists:seq(1,3)].
{q1,1} running
[<0.50.0>,<0.51.0>,<0.52.0>,<0.53.0>,<0.54.0>,<0.55.0>]
{q1,1} done.
{q2,1} running
{q2,1} done.
{q1,2} running
{q1,2} done.
{q2,2} running
{q2,2} done.
{q1,3} running
{q1,3} done.
{q2,3} running
{q2,3} done.