Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Stopping an HTTP::Server::Simple server

by jaldhar (Vicar)
on Oct 26, 2007 at 17:30 UTC ( #647442=perlquestion: print w/ replies, xml ) Need Help??
jaldhar has asked for the wisdom of the Perl Monks concerning the following question:

For a web application, I'm trying to use CGI::Application::Server, a subclass of HTTP::Server::Simple in my modules' tests. So I want to create a server, do the tests and then destroy it when I've finished the tests. The creating is straightforward but how do I say "ok stop now."

Also is there a way to say "start on a guaranteed unused port"? Right now as a workaround, I start at 9323, if the server doesn't start then I add 1 and try again and so on until I get to 9330 when I bail out completely but this doesn't seem to elegant.

--
જલધર

Comment on Stopping an HTTP::Server::Simple server
Re: Stopping an HTTP::Server::Simple server
by erroneousBollock (Curate) on Oct 26, 2007 at 18:38 UTC
    Perhaps you can use the background() method of the HTTP::Server::Simple object, which runs the server in the background (I assume it forks) and returns the PID of the running server. When you're done testing you can send a KILL signal to that PID.

    -David

Re: Stopping an HTTP::Server::Simple server
by Corion (Pope) on Oct 26, 2007 at 20:49 UTC

    I've done something similar in the time before HTTP::Server::Simple with Test::HTTP::LocalServer.

    I use a special URL to tell my server that I'm done with it, normally /quit.

    As I launch the server as a child process, I have it output the URL where it can be found, so I get an "always unused port" there.

    As an aside, why are you launching a server at all, when you can just require the server module and call the ->handler() or ->handle_request() yourself?

Re: Stopping an HTTP::Server::Simple server
by textual (Novice) on Jan 26, 2011 at 13:07 UTC

    I'm facing the same problem, though not in a testing situation.

    Following Corion's advice, I'm trying to pass the server's PID (returned by its background() method) to a handler that will be responsible for killing it.

    My problem is that I can't find a way to pass the PID to the handler.

    I've tried several things which all end up the same way when I access the url associated to the handler: the PID's value has not been passed and Perl "Can't kill a non-numeric process ID".

    I've tried this (where the setup is maximally contrived for brevity):

    A) Store PID as a global in the main package and access it from the server package.

    In test.pl:
    use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); our $pid = $server->background();
    And in Webserver.pm:
    package MyWebServer; use strict; use warnings; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $main::pid; } 1;

    B) Store PID as a global in the server package and set it in the main package.

    In test.pl:
    use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); $MyWebServer::pid = $server->background();
    And in Webserver.pm:
    package MyWebServer; use strict; use warnings; our $pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $pid; } 1;

    C) Store the PID in a separate module used by test.pl and webserver.pm.

    In test.pl:
    use strict; use warnings; use Pid; use MyWebServer; my $server = MyWebServer->new(8080); Pid->set($server->background());
    In Webserver.pm:
    package MyWebServer; use strict; use warnings; use Pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, Pid->get(); } 1;
    And in Pid.pm:
    package Pid; use strict; use warnings; my $pid; sub get { my $package = shift; return $pid; } sub set { my $package = shift; $pid = shift; } 1;

    I've tried even more esoteric things until I started to feel like Wile E. Coyote, so I think it's time I submit this problem to you.

    Any advice would be welcome, and so would any explanation as to what I fail to understand.

      Maybe take a look at what you actually get in $pid before trying to kill it? .

        It's actually not defined.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://647442]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-07-29 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (227 votes), past polls