Okay, so down to business. One thing I like about straight cgi vs mod_perl is that any changes are immediately reflected on page refresh. And plackup (and twiggy) both offer an option to auto-restart when files change. Sounds good.
jared@win32 $ plackup -h
...
-r, --reload
Make plackup to watch updates from your development directory and
restarts the server whenever a file is updated. This option by
default watches the "lib" directory and the base directory where
*.psgi* file is located. Use "-R" if you want to watch other
directories.
...
jared@win32 $ twiggy -r --listen :8080 hello.psgi
Watching ./lib hello.psgi for file updates.
./lib: No such file or directory at c:/strawberry/perl/site/lib/Filesys/Notify/Simple.pm line 156
Terminating on signal SIGINT(2)
Hmmm… I didn’t ask it to watch ./lib (and it doesn’t acknowledge changes to hello.psgi). Let me change the watched files with -R.
jared@win32 $ plackup -r -R hello.psgi --listen :8080 hello.psgi
HTTP::Server::PSGI: Accepting connections at http://0:8080/
Watching hello.psgi ./lib hello.psgi for file updates.
./lib: No such file or directory at c:/strawberry/perl/site/lib/Filesys/Notify/Simple.pm line 156
Terminating on signal SIGINT(2)
Er, okay, -R only allows you to add paths. So there is not an obvious way of removing the paths that already exist. *sigh*. I submit to the inevitable.
jared@win32 $ mkdir lib
jared@win32 $ twiggy -r --listen :8080 hello.psgi
Watching ./lib hello.psgi for file updates.
So I make a change to hello.psgi and get the following:
-- C:\home\jared\plack-tests\hello.psgi updated.
Killing the existing server (pid:-2872)
Almost! But I’m missing the message that says it was able to restart the server.
waitpid($pid, 0);
warn "Successfully killed! Restarting the new server process.\n";
In actual fact, the process it claims it has killed is still running and I’m still able to connect to it. So I hack Restarter.pm a bit (more on that at the end).
jared@win32 $ twiggy -r --listen :8080 hello.psgi
Watching ./lib hello.psgi for file updates.
-- C:\home\jared\plack-tests\hello.psgi updated.
Killing the existing server (pid:-2156)
Successfully killed! Restarting the new server process.
bind: Unknown error at c:/strawberry/perl/site/lib/Twiggy/Server.pm line 71
-L, --loader
Using plackup instead of twiggy works.
jared@win32 $ plackup -r --listen :8080 hello.psgi
HTTP::Server::PSGI: Accepting connections at http://0:8080/
Watching ./lib hello.psgi for file updates.
-- C:\home\jared\plack-tests\hello.psgi updated.
Killing the existing server (pid:-3544)
Successfully killed! Restarting the new server process.
HTTP::Server::PSGI: Accepting connections at http://0:8080/
Although having thought about it, maybe the Shotgun loader is what I really want.
jared@win32 $ twiggy --listen :8080 hello.psgi -L Shotgun
Attempt to free unreferenced scalar: SV 0x2c78b04,
Perl interpreter: 0x2400054 at
c:/strawberry/perl/site/lib/Plack/Loader/Shotgun.pm line 48.
Again, plackup works here where twiggy does not.
jared@win32 $ plackup --listen :8080 hello.psgi -L Shotgun
HTTP::Server::PSGI: Accepting connections at http://0:8080/
127.0.0.1 - - [22/Mar/2010 21:21:31] "GET / HTTP/1.1" 200 42 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)"
127.0.0.1 - - [22/Mar/2010 21:21:35] "GET /favicon.ico HTTP/1.1" 200 42 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)"
127.0.0.1 - - [22/Mar/2010 21:22:07] "GET / HTTP/1.1" 200 42 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)"
# —
And at the risk of getting pointed comments for using the KILL signal, here is the hack I made to Restarter.pm. (Hey, what can I do? My OS blows.)
--- Plack.orig/Loader/Restarter.pm 2010-03-22 20:50:16 +0000
+++ Plack/Loader/Restarter.pm 2010-03-22 21:31:06 +0000
@@ -37,6 +37,12 @@
my $pid = $self->{pid} or return;
warn "Killing the existing server (pid:$pid)\n";
kill 'TERM' => $pid;
+
+ if (lc($^O) =~ /mswin32/) {
+ sleep 1;
+ kill 'KILL' => $pid;
+ }
+
waitpid($pid, 0);
warn "Successfully killed! Restarting the new server process.\n";
}
Read Full Post »