[cgiapp] need a fresh WebApp instance with every request ?

Matthias Ferdinand mf+cgiapp at mfedv.net
Fri Dec 2 09:08:10 EST 2011


On Fri, Dec 02, 2011 at 01:08:24PM +0000, Larig Tech wrote:
> You've prompted me to jot down some quick notes at
> http://larig.wordpress.com/2011/12/02/separating-per-application-init-from-per-request-init-in-cgiapplication/

Hi,

class storage is a good idea (that's why inside-out objects use it :-),
but this still depends on the plugins being capable of working as class
methods. I am not sure all plugins can do that (maybe after your code
hits CPAN).

My not-yet-finished idea for this is currently to have separate classes
for the single-request objects (called C:A:Capsule) and for one
persistent multi-request object (called MyApp).
The single-request class inherits from C:A. Its objects will have a
lifetime of exactly one request. On creation, they are given an object
reference to a MyApp object to invoke methods of.

C:A:Capsule object methods (override methods from C:A and runmode
methods) will not do much by themselves, they will just call a
corresponding method in the MyApp object, e.g.

# in C:A:Capsule
    sub cgiapp_postrun {
      my $self = shift;
      my $app  = $self->{_APP_OBJECT};

      # invoke MyApp teardown
      return $app->cgiapp_postrun($self)
          if ($app->can("cgiapp_postrun"));
    }

With $self in the argument list of the MyApp object callback method
the MyApp object has access to all C:A object data and methods (but not
C:A class data).

# in MyApp
    sub cgiapp_postrun {
      my $self       = shift;
      my $capsule    = shift;
      my $output_ref = shift;
       
      # make session data persist
      if ($capsule->session_loaded)
        { $capsule->session->flush; }
    }


I am not yet sure if there is a generic way to cater for the runmode
methods or if I need to create a C:A:Capsule:MyApp with similar
callback-style-runmode-methods for the individual MyApp runmode methods
for each new variant of MyApp.


This approach would not require any changes in the C:A codebase or its
plugins and still decouple per-request initialisation and
per-application-instance initialisation.



But this is still just an idea, nothing coded yet.


Regards
Matthias


More information about the cgiapp mailing list