FAQ
Hi All,

i was looking for something that's similar to "auto" in the sense that it
also runs in all the parent controllers of the dispatched action, but is
run right before "end" (that dispatches to the view).

The actual problem that led to this was that I had to log the processing
time of the request *excluding* the processing time of the view
component. Since I had some Catalyst::Controller::REST based controllers
(that inherit their own "end" actions from C:P::REST) I could not put the
logging in the root "end" action as it did not run for the C:P::REST based
controllers. Overriding the "end" in each REST controller to call the
logging is error-prone (even with a role, as one can easily forget to
apply the role when writing the next REST controller). And since I wanted
to exclude the processing time of the view,
Catalyst::Plugin::RunAfterRequest alone did not solve my problem either -
it runs after the view, thus it's perfect for the actual logging but is
too late to measure the time elapsed since the beginning of the request.

I did not find anything like that, so I wrote this module:

https://github.com/mendel/Catalyst-Plugin-SpecialAction-Trail/blob/master/lib/Catalyst/Plugin/SpecialAction/Trail.pm

Now the code looks like this:

package MyApp;

...

use Catalyst qw/
...
RunAfterRequest
SpecialAction::Trail
/;

...

package MyApp::Controller::Root;

...

__PACKAGE__->config(namespace => '');

...

sub trail : Private {
my ($self, $c) = @_;

# $c->stash->{req_start_time} is set to gettimeofday()
# at the beginning of request processing
my $time_elapsed = tv_interval($c->stash->{req_start_time});

$c->run_after_request(sub {
<actual logging>
});
}

sub end : Private : ActionClass('RenderView') {
...
}


Comments welcome.

norbi

Search Discussions

  • Tomas Doran at Jun 1, 2011 at 8:34 pm

    On 1 Jun 2011, at 19:35, BUCHMULLER Norbert wrote:
    Comments welcome.
    Monkey patching Catalyst::Controller is a horrible horrible horrible
    idea, please don't do that!

    If (for example) one were to load two Catalyst apps at once (e.g.
    FCGI::Engine running multiple apps, or one mod_perl instance with two
    apps), then this would result in both applications getting the
    behavior, which I'm quite sure is not what you intended.

    If you really need to do this, then apply the roles to the controller
    _instances_ after they have been initialized.

    E.g.

    around setup_component => sub {
    my ($orig, $self, @args) = @_;
    my $instance = $self->$orig(@args);
    Moose::Util::apply_all_roles($instance, 'My::Role');
    return $instance;
    };

    This will only affect the application you load it into, rather than
    all Catalyst applications sharing that memory space.

    Cheers
    t0m
  • BUCHMULLER Norbert at Jun 1, 2011 at 10:19 pm

    On Wed, 1 Jun 2011 21:34:25 +0100 Tomas Doran wrote:

    Monkey patching Catalyst::Controller is a horrible horrible horrible
    idea, please don't do that!

    If (for example) one were to load two Catalyst apps at once (e.g.
    FCGI::Engine running multiple apps, or one mod_perl instance with two
    apps), then this would result in both applications getting the
    behavior, which I'm quite sure is not what you intended.
    True. One tends to forget that there can be multiple instances of
    Catalyst in one process.
    If you really need to do this, then apply the roles to the controller
    _instances_ after they have been initialized.
    Advice taken, now only patching the C:C instances.

    Thanks for the comment!

    norbi

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedJun 1, '11 at 6:35p
activeJun 1, '11 at 10:19p
posts3
users2
websitecatalystframework.org
irc#catalyst

2 users in discussion

BUCHMULLER Norbert: 2 posts Tomas Doran: 1 post

People

Translate

site design / logo © 2022 Grokbase