tags:

views:

243

answers:

1

I'm trying to get some code called after each request completes using Catalyst. Basically, I want to run some code as part of finalize. Supposedly Catalyst::Plugin::Observe will do this, but it appears completely broken (just loading the plugin breaks Catalyst).

I'm trying to fix the Observe plugin, but that's proving stubborn.

So, is there a better way to do get some cleanup code called at the end of each request?

(Note: This is in a model, not a controller, so I can't just use sub end { ... })

+5  A: 

You can actually just add the code directly to your "MyApp" class:

package MyApp;
use Catalyst ...;

...

sub finalize {
    my $c = shift;
    $c->NEXT::finalize(@_);
    # do your thing
}

This is how all plugins work; they are just methods that become part of your app.

I do agree that making "finalize" generate an event to observe is cleaner... but this is what we have to work with for now :) Join #catalyst on irc.perl.org, and we can discuss further. (I am jrockway, as you may guess.)

Edited to reply to:

(Note: This is in a model, not a controller, so I can't just use sub end { ... })

You do know that you have $c in end, right?

package Your::Model;

sub cleanup {
   my $self = shift;
   ...
}

package Your::Controller;

sub end :Private {
    my ($self, $c) = @_;
    $c->model('Your::Model')->cleanup( ... )
}

Or you can do it from MyApp::finalize, as I suggested above.

The real question is, why does your model need to know about the request cycle? That sounds like awfully tight coupling.

jrockway
Thanks for the answer. If I can't get the Observer plugin to work, I'll try just putting that hook in the app as you suggest. FYI, I'm 'derobert_work' or 'derobert' in #catalyst.
derobert
Oh, and as to why I'm doing this, basically, the model needs to send off a rollback-like command at request end, just in case something breaks.
derobert