Catalyst-Plugin-RunAfterRequest
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/RunAfterRequest.pm view on Meta::CPAN
package Catalyst::Plugin::RunAfterRequest;
BEGIN {
$Catalyst::Plugin::RunAfterRequest::AUTHORITY = 'cpan:FLORA';
}
BEGIN {
$Catalyst::Plugin::RunAfterRequest::VERSION = '0.04';
}
# ABSTRACT: run code after the response has been sent.
use Moose::Role;
use MooseX::Types::Moose qw/ArrayRef CodeRef/;
use namespace::autoclean;
has callbacks => (
traits => ['Array'],
isa => ArrayRef[CodeRef],
default => sub { [] },
handles => {
run_after_request => 'push',
_callbacks => 'elements',
},
);
after finalize => sub {
my $self = shift;
for my $callback ($self->_callbacks) {
$self->$callback;
}
};
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Catalyst::Plugin::RunAfterRequest - run code after the response has been sent.
=head1 SYNOPSIS
#### In MyApp.pm
use Catalyst qw(RunAfterRequest);
#### In your controller
sub my_action : Local {
my ( $self, $c ) = @_;
# do your normal processing...
# add code that runs after response has been sent to client
$c->run_after_request( #
sub { $self->do_something_slow(); },
sub { $self->do_something_else_as_well(); }
);
# continue handling the request
}
#### Or in your Model:
package MyApp::Model::Foo;
use Moose;
extends 'Catalyst::Model';
with 'Catalyst::Model::Role::RunAfterRequest';
sub some_method {
my $self = shift;
$self->_run_after_request(
sub { $self->do_something_slow(); },
sub { $self->do_something_else_as_well(); }
);
}
=head1 DESCRIPTION
Sometimes you want to run something after you've sent the reponse back to the
client. For example you might want to send a tweet to Twitter, or do some
logging, or something that will take a long time and would delay the response.
( run in 2.068 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )