Skip to content

Instantly share code, notes, and snippets.

@jberger
Created February 16, 2021 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/e7a5ef096e6637facdc196eea7a063d8 to your computer and use it in GitHub Desktop.
Save jberger/e7a5ef096e6637facdc196eea7a063d8 to your computer and use it in GitHub Desktop.
{
foo => 1,
plugins => [
{'HypnotoadRestarter' => {}},
]
}
{
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