On Feb 22, 2007, at 10:28 AM, Rocco Caputo wrote:

On Feb 21, 2007, at 15:05, Kevin Scaldeferri wrote:

I'm trying to figure out how to schedule an action to happen after my PoCo::Server::TCP shuts down. I.e., after all the pending requests have finished processing. I tried doing it through _stop like this, but it didn't work:

  POE::Component::Server::TCP->new
      ( Alias => 'server',
        Port => $port,
        ClientFilter => 'POE::Filter::HTTPD',
        ClientInput => \&dispatch,
        PackageStates => [ main => [ ...
                                     "_stop"
                                   ],
                           ],
        );
...
In fact, _stop() was never called.


I think I mentioned this in IRC. You can do this from ClientDisconnected callbacks if you maintain a semaphore and a shutdown flag. Catching a callback from the server's _stop is probably a lot easier, though.


The semaphore approach (at least as I implemented it) doesn't work. The thing is, you might get a shutdown signal when there are no active connections. So, ClientDisconnected never gets called after the shutdown is initiated and you never get the message.

Based on another IRC suggestion, I ended up doing this instead:

   POE::Session->create
       ( inline_states => {
           _start => sub {
             if (exists($opts{Started})) {
               $opts{Started}->(@_);
             }

             $_[HEAP]->{server_id} =
                 POE::Component::Server::TCP->new
                   ( Alias => 'server',
                     Port => $port,
                     ClientFilter => 'POE::Filter::HTTPD',
                     ClientInput => \&dispatch,
                     PackageStates => [ ... ],
                   );
           },
           _child => sub {
             my ($what, $who) = @_[ARG0, ARG1];
             my $id = $who->ID();
             debug ("_child($what:$id) called\n");
             if ($what eq 'lose'
                 && $id == $_[HEAP]->{server_id}
                 && exists $opts{Shutdown}) {
               $opts{Shutdown}->(@_);
             }
           },
         }
       );


And that seems to work fine.


-kevin

Reply via email to