I have a rest server written in perl dancer on a VM, this is the workflow :
- customers POST some json stuff in
/post_wait
route : the POST should waiting the business processing to return the result - the request is processed by another VMs (communication is processed by POE + IKC)
- the report should be returned by
/report
route to the customer identified with the session id by theREST
server
DANCER CODE : (that's what I tried so far)
package var;
use Data::Dumper;
use Dancer ':syntax';
use JSON::XS;
my $session = {};
# curl -H 'Content-Type: application/json' -X POST -d '{"id":22}' http://127.0.0.1:3000/post_wait
post '/post_wait' => sub {
my $json = request->body;
my $h = decode_json $json;
my $id = $h->{id};
until (exists $session->{$id}->{report}) {
sleep 1;
print STDERR ".";
}
return Dumper $session;
};
# curl -H 'Content-Type: application/json' -X POST -d '{"foobar":"xxxx"}' http://127.0.0.1:3000/report
post '/report' => sub {
my $json = request->body;
my $h = decode_json $json;
my $id = $h->{id};
$session->{$id}->{report} = $h;
return "OK\n";
};
true;
The problem is that this code hangs, because I sleep the parent process.
Maybe I should use :
- rewrite that with POE-Component-Server-REST ?
- Dancer::Plugin::Async module ? but can I avoid using this ? (there's some technical restrictions)...
- another script as a subprocess ?
- a database like redis queue using
BLPOP
to dequeue ? (that needs some subprocess code around) - Dancer::Session ?
fork()
?- Threads ? (I guess no)
My information system should be scalable and in high-availability mode.
What is the best practice to do what I want ?