examples/job-runner/app.pl

filter: Statementstmt 66.6% 50 / 75 Branchbran 33.3% 4 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 12 / 14 Podpod n/a totaltotal 64.4% 67 / 104 CC 9 SCAR 13.1
CC
9
Cov
62%
__ANON__
12.0
__ANON__
6.0
__ANON__
4.1
CRAP 3.7
« blib/lib/PAGI/Nano/ServiceRegistry.pm ↑ Summary examples/chat-showcase/app.pl »
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::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Ports PAGI's 11-job-runner to PAGI::Nano.
8 # An async job queue: a REST API to submit and inspect jobs, a background worker
9 # (rooted in the lifespan) that advances job progress, an SSE stream of a single
10 # job's progress, and a WebSocket feed of overall queue stats. Job state lives in
11 # the app-lifetime shared state.
12 #
13 # pagi-server app.pl
14 # curl -X POST -H 'content-type: application/json' -d '{"label":"resize"}' \
15 # http://127.0.0.1:5000/api/jobs
16 # curl -N http://127.0.0.1:5000/api/jobs/1/progress
17  
18 3 ▶sub job_view ($job) {
Subroutine job_view: called CC=1 SCAR=0.0
19 return { id => $job->{id}, label => $job->{label}, status => $job->{status},
20 3 progress => $job->{progress} };
21 }
22  
23 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=1.0
24 1 ▶ startup async sub ($state) {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 $state->{jobs} = {}; # id => job
26 1 $state->{next_id} = 1;
27 # Background worker: advance the first running/queued job each tick.
28 1 ▶ $state->{worker} = (async sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
29 1 while (1) {
30 2 await Future::IO->sleep(1);
31 0 for my $job (sort { $a->{id} <=> $b->{id} } values %{ $state->{jobs} }) {
32 1 ▶ next if $job->{status} eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"status"} eq "done" 0 1 1
33 1 $job->{status} = 'running';
34 1 $job->{progress} += 25;
35 1 ▶ $job->{status} = 'done' if $job->{progress} >= 100;
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"progress"} >= 100 0 1 1
36 1 last; # one unit of work per tick
37 }
38 }
39 1 })->();
40 1 };
41  
42 # --- REST API ---
43 1 ▶ post '/api/jobs' => async sub ($c) {
Subroutine __ANON__: called CC=2 SCAR=7.0
44 1 my $attrs = await $c->params->permitted('label');
45 1 my $id = $c->state->{next_id}++;
46 1 ▶ my $job = { id => $id, label => $attrs->{label} // "job-$id",
Condition50%1/2
$attrs->{"label"} // "job-$id"
l!l
10
MC/DC0%0/1
$attrs->{"label"} // "job-$id"
$attrs->{"label"}
Truth table50%1/2
A
A= $attrs->{"label"}
Aresult
11
00
47 status => 'queued', progress => 0 };
48 1 $c->state->{jobs}{$id} = $job;
49 1 $c->json(job_view($job), status => 201);
50 1 };
51  
52 1 ▶ get '/api/jobs' => sub ($c) {
Subroutine __ANON__: called CC=1 SCAR=0.2
53 1 [ map { job_view($_) }
54 0 sort { $a->{id} <=> $b->{id} } values %{ $c->state->{jobs} } ];
55 1 };
56  
57 2 ▶ get '/api/jobs/:id' => sub ($c, $id) {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 2 ▶ my $job = $c->state->{jobs}{$id}
Branch: 1 branch
BranchTrueFalseTotal
my $job = $c->state->{"jobs"}{$id} or return $c->json({"error", "no such job"}, "status", 404) 1 1 2
59 or return $c->json({ error => 'no such job' }, status => 404);
60 1 job_view($job);
61 1 };
62  
63 # --- SSE: stream one job's progress until it finishes ---
64 0 ▶ sse '/api/jobs/:id/progress' => async sub ($c, $id) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
65 0 my $s = $c->sse;
66 0 my $job = $c->state->{jobs}{$id};
67 0 ▶ unless ($job) { await $s->send_event(event => 'error', data => 'no such job'); await $s->close; return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($job) 0 0 0
68 0 ▶ while ($job->{status} ne 'done') {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"status"} ne "done") 0 0 0
69 0 await $s->send_event(event => 'progress', data => $job->{progress});
70 0 await Future::IO->sleep(1);
71 }
72 0 await $s->send_event(event => 'done', data => 100);
73 0 await $s->close;
74 1 };
75  
76 # --- WebSocket: push overall queue stats ---
77 0 ▶ websocket '/ws/queue' => async sub ($c) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 my $ws = $c->websocket;
79 0 await $ws->accept;
80 0 ▶ while ($ws->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws->is_connected) 0 0 0
81 0 my @jobs = values %{ $c->state->{jobs} };
82 await $ws->send_json_if_connected({
83 total => scalar(@jobs),
84 0 done => scalar(grep { $_->{status} eq 'done' } @jobs),
85 });
86 0 await Future::IO->sleep(1);
87 }
88 1 };
89 1 };
90  
91 1 $app;
« blib/lib/PAGI/Nano/ServiceRegistry.pm ↑ Summary examples/chat-showcase/app.pl »