| 1 | 1 | ▶ | use strictures 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | |||||||||||||||
| 3 | package # for internal use only | ||||||||||||||
| 4 | Dancer2::Plugin::Role::Shutdown; | ||||||||||||||
| 5 | |||||||||||||||
| 6 | # ABSTRACT: Role for L<Dancer2::Plugin::Shutdown> | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Class::Load qw(load_class); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Moo::Role 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | ▶ | use constant NORMAL => 0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use constant GRACEFUL => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | our $VERSION = '0.002'; # VERSION | |||||||||||||
| 17 | |||||||||||||||
| 18 | |||||||||||||||
| 19 | has shared => ( | ||||||||||||||
| 20 | is => 'rwp', | ||||||||||||||
| 21 | 1 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 1 | ); | |||||||||||||
| 23 | |||||||||||||||
| 24 | |||||||||||||||
| 25 | has validator => ( | ||||||||||||||
| 26 | is => 'rw', | ||||||||||||||
| 27 | 1 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 2 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||
| 29 | 2 | my ($app, $rest, $sessid) = @_; | |||||||||||||
| 30 | 2 | ▶ | return unless $sessid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 1 | ▶ | my $sx = $app->session->expires // 0; | ||||||||||||
|
Condition50%1/2
$app->session->expires // 0
MC/DC0%0/1
$app->session->expires // 0
$app->session->expires
Truth table50%1/2
A
A= $app->session->expires
| |||||||||||||||
| 32 | 1 | ▶ | $app->session->expires($rest) if $rest > $sx; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 1 | $app->response->header(Warning => "199 Application shuts down in $rest seconds"); | |||||||||||||
| 34 | 1 | return 1; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | 1 | } | |||||||||||||
| 37 | 1 | ); | |||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | 5 | ▶ | sub has_valid_session { | ||||||||||||
|
Subroutine
has_valid_session: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||
| 41 | 5 | my $app = shift; | |||||||||||||
| 42 | |||||||||||||||
| 43 | 5 | ▶ | my $engine = $app->session_engine // return; | ||||||||||||
|
Condition50%1/2
$app->session_engine // (return)
MC/DC0%0/1
$app->session_engine // (return)
$app->session_engine
Truth table50%1/2
A
A= $app->session_engine
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 5 | ▶ | return if $app->has_destroyed_session; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 5 | ▶ | my $session_cookie = $app->cookie( $engine->cookie_name ) // return; | ||||||||||||
|
Condition100%2/2
$app->cookie($engine->cookie_name) // (return)
MC/DC100%1/1
$app->cookie($engine->cookie_name) // (return)
$app->cookie($engine->cookie_name)
Truth table100%2/2
A
A= $app->cookie($engine->cookie_name)
| |||||||||||||||
| 48 | 3 | ▶ | my $session_id = $session_cookie->value // return; | ||||||||||||
|
Condition50%1/2
$session_cookie->value // (return)
MC/DC0%0/1
$session_cookie->value // (return)
$session_cookie->value
Truth table50%1/2
A
A= $session_cookie->value
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 3 | eval { $engine->retrieve( id => $session_id ) }; | |||||||||||||
| 51 | 3 | ▶ | return if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 3 | return $session_id; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | |||||||||||||||
| 57 | 1 | ▶ | sub session_status { | ||||||||||||
|
Subroutine
session_status: called CC=6 SCAR=27.1
Pod
documented
| |||||||||||||||
| 58 | 1 | my $app = shift; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 1 | ▶ | my $engine = $app->session_engine // return "unsupported"; | ||||||||||||
|
Condition50%1/2
$app->session_engine // (return "unsupported")
MC/DC0%0/1
$app->session_engine // (return "unsupported")
$app->session_engine
Truth table50%1/2
A
A= $app->session_engine
| |||||||||||||||
| 61 | |||||||||||||||
| 62 | 1 | ▶ | return "destroyed" if $app->has_destroyed_session; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | |||||||||||||||
| 64 | 1 | ▶ | my $session_cookie = $app->cookie( $engine->cookie_name ) // return "missing"; | ||||||||||||
|
Condition50%1/2
$app->cookie($engine->cookie_name) // (return "missing")
MC/DC0%0/1
$app->cookie($engine->cookie_name) // (return "missing")
$app->cookie($engine->cookie_name)
Truth table50%1/2
A
A= $app->cookie($engine->cookie_name)
| |||||||||||||||
| 65 | 0 | ▶ | my $session_id = $session_cookie->value // return "empty"; | ||||||||||||
|
Condition0%0/2
$session_cookie->value // (return "empty")
MC/DC0%0/1
$session_cookie->value // (return "empty")
$session_cookie->value
Truth table0%0/2
A
A= $session_cookie->value
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | eval { $engine->retrieve( id => $session_id ) }; | |||||||||||||
| 68 | 0 | ▶ | return "invalid" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | return "ok"; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | |||||||||||||||
| 74 | 7 | ▶ | sub before_hook { | ||||||||||||
|
Subroutine
before_hook: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||
| 75 | 7 | my $self = shift; | |||||||||||||
| 76 | 7 | ▶ | return unless $self->shared->{state}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 3 | my $app = $self->app; | |||||||||||||
| 78 | 3 | my $time = $self->shared->{final}; | |||||||||||||
| 79 | 3 | my $rest = $time - time; | |||||||||||||
| 80 | 3 | ▶ | if ($rest < 0) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 1 | $self->status(503); | |||||||||||||
| 82 | 1 | $self->halt; | |||||||||||||
| 83 | } elsif ($self->shared->{state} == GRACEFUL) { | ||||||||||||||
| 84 | 2 | ▶ | if (my $validator = $self->validator) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 2 | my $sessid = has_valid_session($app); | |||||||||||||
| 86 | 2 | ▶ | unless ($validator->($app, $rest, $sessid)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 1 | $self->status(503); | |||||||||||||
| 88 | 1 | $self->halt; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | } else { | ||||||||||||||
| 92 | 0 | croak "bad state: ".$self->shared->{state}; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 1 | ▶ | sub _shutdown_at { | ||||||||||||
|
Subroutine
_shutdown_at: called CC=4 SCAR=14.2
| |||||||||||||||
| 97 | 1 | my $self = shift; | |||||||||||||
| 98 | 1 | ▶ | croak "a validator isn't installed yet" unless ref $self->validator eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 1 | ▶ | my $time = shift // 0; | ||||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 100 | 1 | ▶ | if ($time < time) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 1 | $time += time; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 1 | $self->shared->{final} = $time; | |||||||||||||
| 104 | 1 | $self->shared->{state} = GRACEFUL; | |||||||||||||
| 105 | 1 | return $time; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 1 | 1; | |||||||||||||
| 109 | |||||||||||||||
| 110 | __END__ | ||||||||||||||