Hi,
I am not sure I fully understand your question, but if you already have Redis and you want that only one spawned process receives an event, and that on next event next spawned process do the same, you could use queue commands from Redis for that (instead of PUB/SUB that would be addressed to all childs that are listening to the published key).
So your "publisher" should send events to a queue using for instance, RPUSH somelist someevent, and your mojo app should be listening with BLPOP somelist 0 (0 means indefinitelly).
For the listening inside your Mojo app, you should take a look on Mojo::Redis2.
Just as an example, you could do something like:
use Mojolicious::Lite;
use Mojo::Redis2;
helper redis => sub { shift->stash->{redis} ||= Mojo::Redis2->new; };
Mojo::IOLoop->next_tick(sub {
my $c = app->build_controller;
my $element_get;
$element_get = sub {
$c->redis->blpop('myqueue', 0, sub {
my ($self, $err, $res) = @_;
$c->app->log->info("Worker $$ received an event from $res->[0]: $res->[1]");
$element_get->(); # get next element from 'myqueue'
});
};
$c->app->log->info("Worker $$ started");
$element_get->();
});
get '/' => {text => 'Hello Word!!!'};
app->start;
That way, you will not block the ioloop, and only one child will take next event.
The child that gets the event should rotate just because it is the way Redis queues work.
As stated in the cookbook, you can use "next_tick" to run code whenever a new process is spawned.
You can check this code running it with hypnotoad and opening on another terminal a redis-cli instance and sending strings to the 'myqueue' list, like:
> rpush myqueue whatever "whatever 2" "whatever 3" "whatever 4" "whatever 5"
Hope this helps,