| 1 | 1 | ▶ | use v5.40; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use Future; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Future::IO; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use PAGI::Nano; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | # Ports PAGI-Tools' background-tasks to PAGI::Nano. | ||||||||||||
| 9 | # Respond immediately, then do work in the background. Nano sends the handler's | ||||||||||||
| 10 | # return value after the handler returns, so a Future that is *retained* (not | ||||||||||||
| 11 | # awaited) keeps running on the loop without delaying the response. | ||||||||||||
| 12 | # | ||||||||||||
| 13 | # pagi-server app.pl | ||||||||||||
| 14 | # curl -X POST -H 'content-type: application/json' -d '{"email":"a@b.com"}' \ | ||||||||||||
| 15 | # http://127.0.0.1:5000/signup | ||||||||||||
| 16 | |||||||||||||
| 17 | # Fire-and-forget background work. Returns a Future the handler retains. | ||||||||||||
| 18 | 1 | ▶ | sub send_welcome_email ($address) { | ||||||||||
|
Subroutine
send_welcome_email: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | return (async sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | await Future::IO->sleep(1); # pretend to talk to an SMTP server | |||||||||||
| 21 | 1 | warn "[bg] welcome email sent to $address\n"; | |||||||||||
| 22 | 1 | })->(); | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 1 | ▶ | my $app = app { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||
| 26 | 0 | ▶ | get '/' => sub ($c) { { hint => 'POST /signup {"email":...} to fire a background task' } }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 27 | |||||||||||||
| 28 | 1 | ▶ | post '/signup' => async sub ($c) { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 29 | 1 | my $attrs = await $c->params->permitted('email'); | |||||||||||
| 30 | 1 | ▶ | my $email = $attrs->{email} // 'anonymous'; | ||||||||||
|
Condition50%1/2
$attrs->{"email"} // "anonymous"
MC/DC0%0/1
$attrs->{"email"} // "anonymous"
$attrs->{"email"}
Truth table50%1/2
A
A= $attrs->{"email"}
| |||||||||||||
| 31 | |||||||||||||
| 32 | # Kick off the slow work and retain it: not awaited, so the 202 returns now. | ||||||||||||
| 33 | 1 | send_welcome_email($email)->retain; | |||||||||||
| 34 | |||||||||||||
| 35 | 1 | $c->json({ status => 'accepted', email => $email }, status => 202); | |||||||||||
| 36 | 1 | }; | |||||||||||
| 37 | 1 | }; | |||||||||||
| 38 | |||||||||||||
| 39 | 1 | $app; | |||||||||||