Does dancer have a post-return hook or something of that nature?
Here’s what I want to do: post '/info' => sub { info "\n\n\tIn /info with params:\n" . Dumper params; my $tmp_href = params; if (&do_some_stuff_here($tmp_href)) { $tmp_href->{status_code} = "200"; } return $tmp_href; # THEN DO SOME POST RETURN STUFF HERE (Obviously not here, but somehow a callback function for post-return or something like that?) }; Does it exist? I feel like I’ve been through the documentation pretty thoroughly. -Robert
"Robert" == Robert Smith <spamfree@wansecurity.com> writes:
Robert> Here’s what I want to do: Robert> post '/info' => sub { Robert> info "\n\n\tIn /info with params:\n" . Dumper params; Robert> my $tmp_href = params; Robert> if (&do_some_stuff_here($tmp_href)) { Robert> $tmp_href->{status_code} = "200"; Robert> } Robert> return $tmp_href; Robert> # THEN DO SOME POST RETURN STUFF HERE (Obviously not Robert> here, but somehow a callback function for post-return Robert> or something like that?) Well... you can't do anything in a function after a return gets called, so you will have to setup a callback function. What are you trying to do in the callback? Before the return $tmp_ref; you need to put in a call to something else, which could just be a POST to another URL. Or if it's a longer lived process (say you're kicking off a job to process all the params you passed in), then maybe what you want to do is really return a DIV and some javascript which will poll looking for updates, etc. Sorta like how some of the pagination stuff works. Sorry I'm not being very specific here, I'd be pounding my head too. :-) John
It’s for an API I wrote for the ZeroKnowledge Foundation. It allows the customer to pass nothing more than an ID, and we spin up a VM for them, and they get nothing more than an IP address to hand to their customers. After each call, for /create (spin up) I work our magic and get a new VM setup for them and pass them back an IP address returning status 200. While waiting for their next create call, in the background I want to clone a disk image which takes several minutes to complete. I’d like something more elegant than writing a file somewhere and having a background task to the copy. In fact, I’d like to even have the API block while the background copy process is working. But what I’m really trying to figure out is some kind of callback function to allow me to execute something after the return. Like a post-return callback. -Robert
On Aug 23, 2015, at 5:47 AM, John Stoffel <john@stoffel.org> wrote:
"Robert" == Robert Smith <spamfree@wansecurity.com> writes:
Robert> Here’s what I want to do: Robert> post '/info' => sub { Robert> info "\n\n\tIn /info with params:\n" . Dumper params;
Robert> my $tmp_href = params;
Robert> if (&do_some_stuff_here($tmp_href)) { Robert> $tmp_href->{status_code} = "200"; Robert> } Robert> return $tmp_href;
Robert> # THEN DO SOME POST RETURN STUFF HERE (Obviously not Robert> here, but somehow a callback function for post-return Robert> or something like that?)
Well... you can't do anything in a function after a return gets called, so you will have to setup a callback function. What are you trying to do in the callback?
Before the return $tmp_ref; you need to put in a call to something else, which could just be a POST to another URL. Or if it's a longer lived process (say you're kicking off a job to process all the params you passed in), then maybe what you want to do is really return a DIV and some javascript which will poll looking for updates, etc.
Sorta like how some of the pagination stuff works. Sorry I'm not being very specific here, I'd be pounding my head too. :-)
John
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Sat, Aug 22, 2015 at 10:36 AM, Robert Smith <spamfree@wansecurity.com> wrote:
Here’s what I want to do:
post '/info' => sub { info "\n\n\tIn /info with params:\n" . Dumper params;
my $tmp_href = params;
if (&do_some_stuff_here($tmp_href)) { $tmp_href->{status_code} = "200"; } return $tmp_href;
# THEN DO SOME POST RETURN STUFF HERE (Obviously not here, but somehow a callback function for post-return or something like that?)
};
Does it exist? I feel like I’ve been through the documentation pretty thoroughly.
-Robert
It seems like you could achieve what you want by using fork: post '/info' => sub { my $tmp_href = params; # ... my $pid = fork; if ($pid) { return $tmp_href; } else { # DO SOME POST RETURN STUFF HERE } }; Your tmp_href response would return immediately, but a child process would keep running in the background doing your POST RETURN STUFF. Though the proper way to do something like this is with some sort of a messaging/worker system. See for example: https://metacpan.org/pod/Dancer::Plugin::Stomp https://metacpan.org/pod/POE::Component::MessageQueue -Naveed
participants (3)
-
John Stoffel -
Naveed Massjouni -
Robert Smith