The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


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


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
        # 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 )

$time may be an absolute or a relative timestamp. A relative timestamp is indicated by an integer less than the current timestamp, so:


is the same as:



Invokation: 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

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 <>


This software is Copyright (c) 2015 by David Zurborg.

This is free software, licensed under:

  The ISC License