Skip to content

Producer

Ulf Wiger edited this page Jun 16, 2013 · 2 revisions

Examples

This example illustrates how to create a 'producer' in Jobs.

A producer generates jobs rather than dispatching incoming job requests. In their simplest form, the jobs are generated via a 0-arity 'fun', and this is what we will demonstrate first:

Eshell V5.9.2  (abort with ^G)
1> application:start(jobs).

ok2> jobs:add_queue(p,
  [{producer, fun() -> io:fwrite("job: ~p~n",[time()]) end},

                       {standard_rate,1}]).

job: {14,33,51}

ok3> job: {14,33,52}

job: {14,33,53}

job: {14,33,54}

job: {14,33,55}
...

Internally, Jobs maintains a state between dispatches, and this can be exploited in order to create a stateful producer.

We provide an arity-2 fun F, which is initialized by calling F(init, Info) -> State, where Info is an info function encapsulating the entire Jobs configuration (ignored in our example).

For each individual dispatch, Jobs calls F(State, Info) -> {SpawnFun, NewState}, where SpawnFun is an arity-0 fun used by Jobs to spawn a job instance. In our example, our state maintains a counter which is incremented modulo 5 for each dispatch.

Eshell V5.9.2  (abort with ^G)
1> application:start(jobs).
ok
2> jobs:add_queue(p1, [{standard_rate,1},{producer, fun(init,_) -> 0; (N,_) -> {fun() -> io:fwrite("job: ~p~n", [N]) end, (N+1) rem 5} end}]).
job: 0
ok
3> job: 1
job: 2
job: 3
job: 4
job: 0
job: 1
job: 2
job: 3
job: 4
job: 0
...

Here's the above command pretty-printed for clarity:

2> jobs:add_queue(p1,
                  [{standard_rate,1},
                   {producer, fun(init,_) -> 0;
                                 (N,_) ->
                                    {fun() ->
                                         io:fwrite("job: ~p~n", [N])
                                     end,
                                     (N+1) rem 5}
                              end}]).

Examples

Clone this wiki locally