Skip to content

Counter based regulation

Alejandro M. Ramallo edited this page Aug 12, 2015 · 5 revisions

Examples

Jobs' counter-based regulation limits the number of jobs that can run simultaneously.

Here is an example using a shell dialogue:

  1. First, start the jobs application.
  2. Configure a counter-based queue.
  3. Spawn a number of jobs, verify that they execute only one at a time.
Eshell V5.9  (abort with ^G)
1> application:start(jobs).
ok
2> jobs:add_queue(q, [{regulators, [{counter,[{limit,1}]}]}]).
ok
3> [spawn_link(fun() -> jobs:run(q, fun() -> io:fwrite("~p running~n", [self()]), timer:sleep(1000), io:fwrite("~p done.~n", [self()]) end) end) || _ <- lists:seq(1,5)].
[<0.58.0>,<0.59.0>,<0.60.0>,<0.61.0>,<0.62.0>]
<0.62.0> running
<0.62.0> done.
<0.58.0> running
<0.58.0> done.
<0.59.0> running
<0.59.0> done.
<0.60.0> running
<0.60.0> done.
<0.61.0> running
<0.61.0> done.

Configuring the same queue using environment variables could look like this:

application:set_env(jobs, queues,
                    [{q, [{regulators,
                           [{counter, [
                                       {limit,1}
                                      ]
                            }]
                          }]
                    }]).

The above command used to spawn jobs is pretty-printed here:

[spawn_link(fun() ->
                jobs:run(
                   q, fun() ->
                          io:fwrite("~p running~n", [self()]),
                          timer:sleep(1000),
                          io:fwrite("~p done.~n", [self()])
                      end)
            end) || _ <- lists:seq(1,5)].

It is perfectly possible to combine counter-based and rate-based regulation in the same queue. Jobs will calculate the number of jobs to dispatch using all available regulators, and pick the most conservative number.

Examples

Clone this wiki locally