I'm toying with socket server processes on Win32, for example Plack on Twiggy. As I develop, I'd like the server to automatically detect changes and restart. Unfortunately, the fork-emulation of Perl on Windows, while amazingly good, is still not good enough - for example, if I kill a forked (pseudo-)process, resources allocated to it, like sockets, don't get released.
Hence, I wrote this wrapping launcher, that (kills and re-)launches a command if it detects changes in a directory tree. Unfortunately, it can only watch a single tree for changes. But on the upside, that's all I currently need, the lib/ directory:
#!perl -w use strict; use Win32::ChangeNotify; use Getopt::Long; GetOptions( 'dir|d:s' => \my @directories, ); my $child; sub launch_child { warn "Launching [@_]"; my $pid = system(1, @_); warn "Server PID: $pid"; $pid; }; my $path = $directories[0]; while (1) { my $child = launch_child(@ARGV); my $notify = Win32::ChangeNotify->new($path,1,'DIR_NAME FILE_NAME +LAST_WRITE SIZE'); $notify->wait or warn "Something failed: $!\n"; # There has been a change. warn "Killing $child"; kill 9 => $child; undef $child; }; END { kill $child if $child; }; __END__ =head1 SYNOPSIS server-restarter.pl -d lib -- perl -w \strawberry\perl\bin\plackup + --server Twiggy bin\server.psgi =head1 USAGE server-restarter.pl -d DIRECTORY -- [command to launch] =over 4 =item * C<-d> - the directory tree to watch for changes =back =cut
In reply to Crude Win32 Server Restarter by Corion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |