Skip to content
This repository

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:

  1. Create a named counter, c1, with a limit of 1.
  2. Create one queue, q1, which refers to the named counter c1.
  3. Use jobs:info(queues) to verify that the queue was created.
  4. Create another queue, q2 after jobs has started, just to show how that's done. Again, verify with jobs:info(queues).
  5. 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.
Something went wrong with that request. Please try again.