Dancer2::Plugin::Shutdown - Graceful shutdown your Dancer2 application
version 0.002
use Dancer2; use Dancer2::Plugin::Shutdown; $SIG{HUP} = sub { # on hangup, shutdown in 120 seconds shutdown_at(120); };
This plugin gracefully shutdowns your application. This is done by returning a 503 error on every request after "shutdown_at" is called.
An additional check allows active sessions to proceed normally, but the session cookie expires as soon as possible.
The behaviour can be changed with shutdown_session_validator, the default behaviour is similiar to:
shutdown_session_validator(sub { my ($app, $rest, $sessid) = @_; # $sessid is session->id # or undef if no active session is detected return 0 unless $sessid; # $rest is the remaining time in seconds # till planned shutdown $app->session->expires($rest); # let valid sessions survive until shutdown return 1; });
Additionally a Warning header is added with code 199 and a message similiar to: Application shuts down in 106 seconds.
Hint: The expiration of the session cookie will not extended.
Invokation: shutdown_at( $time )
shutdown_at( $time )
$time may be an absolute or a relative timestamp. A relative timestamp is indicated by an integer less than the current timestamp, so:
$time
shutdown_at(120);
is the same as:
shutdown_at(time+120);
Invokation: shutdown_session_validator( sub { ... } )
shutdown_session_validator( sub { ... } )
Changes the session validator subroutine. The sub will be called before every request with this arguments:
$app
An instance of Dancer2::Core::App
$rest
The remaining time in seconds till application shutdown
$session
The session id, but only when a valid and active sesion is detected. Otherwise undefined.
This plugin works only in single-instance environments. With Starman or Corona, the propagation can be done via Redis. There is a plugin that fits this need: Dancer2::Plugin::Shutdown::Redis.
Please report any bugs or feature requests on the bugtracker website https://github.com/zurborg/libdancer2-plugin-shutdown-perl/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
David Zurborg <zurborg@cpan.org>
This software is Copyright (c) 2015 by David Zurborg.
This is free software, licensed under:
The ISC License
To install Dancer2::Plugin::Shutdown, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::Shutdown
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::Shutdown
For more information on module installation, please visit the detailed CPAN module installation guide.