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";
}
It’d be much appreciated if you submit the bug report and patches to the issue tracker first http://github.com/miyagawa/Plack/issues before blogging it, since this post is now indexed on Google and people in the future might find this and thinks it’s broken even when it’s fixed.
> Er, okay, -R only allows you to add paths. So there is not an obvious way of removing the paths that already exist. *sigh*
No, you were not reading the document correctly:
-R, –Reload
“-R” option allows you to specify the path to watch file updates separated by comma (“,”).
plackup -R /path/to/project/lib,/path/to/project/templates
It takes the path to override, which you didn’t pass.
Although Twiggy runs on AnyEvent which means it could run on any platforms supported by AE, stuff like signal handling, reloading and forking gets easily problematic on Win32 in general. I think HTTP::Server::Simple::PSGI and Shotgun is the easiest and safer combination to run on win32.
Also, the Filesys::Notify::Simple’s error on non-existent directory seems like only happens on win32. At least specifying one on my machine (OS X) doesn’t die like that. Patches are welcome to address that issue.
Hi Tatsuhiko,
Thanks for stopping by.
Noted for the future. I’m happy to update this post as and when this stuff gets fixed (although I checked github and… well, I’m not really familiar with github but I couldn’t tell if it has been fixed yet)
I read what you wrote here a couple of times and it still isn’t clear to me how to pass it the path to override. Anyway, nevermind, now that I know it is possible, I’ll try a couple of things (or read the code).
Yeah, for sure Win32 is not my first choice OS, but unfortunately I often have little choice. And my current investigation of Plack is for something that will need to run on both Windows and Linux.
Right, nice hint!
# –
Anyway, seriously, thanks for the great work on this stuff. I guess it doesn’t get much testing on win32, but that’s fine. I wouldn’t work on it if I didn’t have to either :-/
About -R option, your inline example code didn’t pass the argument to -R itself — anyway, the -r or -R *does* add ‘lib’ relative to the .psgi file, which would cause issues like that on Windows apparently. Again, patches to Filesys::Notify::Simple is pretty much appreciated
Reporting on github is as easy as just creating an account and do it so on the web UI. If you don’t like to register you can also use the CPAN RT tracker which is just sending emails to bug-Plack at rt.cpan.org. (But i prefer github issue tracker, at least for now)
The Shotgun on Twiggy error is interesting as well but the error is so cryptic — usually not a type of errors you see as an end user program :/
The Twiggy / Shotgun error *scared* me a bit
. I might expect to see it in a C extension, but not from pure perl.
I’ll look into getting set-up on github. Thanks for the info.