Created
February 16, 2021 04:20
-
-
Save jberger/e7a5ef096e6637facdc196eea7a063d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
foo => 1, | |
plugins => [ | |
{'HypnotoadRestarter' => {}}, | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
package Mojolicious::Plugin::HypnotoadRestarter; | |
use Mojo::Base 'Mojolicious::Plugin', -signatures; | |
use Mojo::Server::Morbo::Backend::Poll; | |
sub register ($plugin, $app, $conf) { | |
my $files = $conf->{files}; | |
$files ||= [$ENV{MOJO_CONFIG}] if $ENV{MOJO_CONFIG}; | |
$files ||= [$app->moniker . '.' . ($conf->{ext} || 'conf')]; | |
my $timeout = $conf->{min_check_time} || 1; | |
my $watcher = Mojo::Server::Morbo::Backend::Poll->new( | |
watch => $files, | |
watch_timeout => 0, | |
); | |
$app->hook(before_server_start => sub ($server, $app) { | |
$app->log->debug(sub{ "Watching files @$files for changes" }); | |
my $time = time(); | |
my $stop = 0; | |
$server->on(finish => sub { $stop = 1 }); | |
$server->on(wait => sub ($server) { | |
return if $stop; | |
# rate limit | |
my $now = time(); | |
return if ($now - $time) > $timeout; | |
$time = $now; | |
return unless my $pid = $server->check_pid; | |
return unless my @files = @{$watcher->modified_files}; | |
$server->app->log->info( | |
@files == 1 | |
? qq{File "@{[$files[0]]}" changed, restarting} | |
: qq{@{[scalar @files]} files changed, restarting} | |
); | |
$stop = 1; | |
kill 'USR2', $pid; | |
}); | |
}); | |
} | |
} | |
use Mojolicious::Lite -signatures; | |
app->log->path('out.log'); | |
plugin 'Config'; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment