2

I have a server written in on a VM, this is the workflow :

  • customers POST some 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 the REST 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 :

My information system should be scalable and in mode.

What is the best practice to do what I want ?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223

1 Answers1

2

You need some kind of seesion tracking. Use Dancer::Session for that.

And you need to do some 'asynchronus', may be fork().

Then you can start the business process by calling

# curl -H 'Content-Type: application/json' -X POST -d '{"id":22}' http://127.0.0.1:3000/post_wait

where the process is forked and the pid is stored in the session.

Then you can call

# curl -H 'Content-Type: application/json' -X POST -d '{"foobar":"xxxx"}' http://127.0.0.1:3000/report

get the pid from your session (if you have one) and check if the process is still running or not.

smartmeta
  • 1,149
  • 1
  • 17
  • 38