tarbaby 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env perl
  2. # PODNAME: tarbaby
  3. # ABSTRACT: starman, with autoreload without Plack::Loader::Restarter, and restarts that drop no request
  4. use 5.040;
  5. use warnings FATAL => 'all';
  6. use re '/aasx';
  7. use FindBin::libs;
  8. use Plack::Runner;
  9. use File::Basename qw{dirname};
  10. use TPSGI::Startup;
  11. =head1 USAGE
  12. tarbaby [--preload-app] [starman options] bin/tpsgi
  13. Starman, run as plackup would run it, with the server that
  14. L<Plack::Handler::TPSGI> provides. On a HUP, that server's workers answer the
  15. connection they have accepted before they retire, so a restart drops no
  16. request. See L<TPSGI::Server>.
  17. Before it forks, tarbaby sets up inotify watches on F<lib/> and on the
  18. directory of each router, so that a worker notices a changed module and
  19. restarts the server, when C<autoreload> is on. The workers load the
  20. application when they start, unless C<--preload-app> is passed.
  21. =cut
  22. sub version {
  23. require Starman;
  24. print "Starman $Starman::VERSION\n";
  25. return;
  26. }
  27. # Setup the watcher for changes
  28. # This has to be setup at global server start rather than app start per worker.
  29. # This is preferable to using -R (Plack::Loader::Restarter, see notes in orig starman)
  30. my %options = TPSGI::Startup::get_config();
  31. my @dirs = ("$options{tpsgi_dir}/lib");
  32. foreach my $route ( @{ $options{routers} } ) {
  33. push( @dirs, dirname("$options{tpsgi_dir}/$route") );
  34. }
  35. TPSGI::Startup::watch_for_changes(@dirs);
  36. my $preload_app;
  37. require Getopt::Long;
  38. Getopt::Long::Configure(qw{no_ignore_case no_auto_abbrev pass_through});
  39. Getopt::Long::GetOptions(
  40. "preload-app" => \$preload_app,
  41. );
  42. my @args = ( server => 'TPSGI', env => 'deployment', version_cb => \&version );
  43. if ( !$preload_app ) {
  44. push @args, 'loader' => 'Delayed';
  45. }
  46. my @argv = @ARGV;
  47. my $runner = Plack::Runner->new(@args);
  48. $runner->parse_options(@argv);
  49. $runner->set_options( argv => \@argv );
  50. $runner->run;