Skip to content

Commit

Permalink
Merge pull request #2147 from hernan604/restart_morbo_when_file_is_de…
Browse files Browse the repository at this point in the history
…leted

Triggers modified_files on file deletion
  • Loading branch information
kraih authored Feb 9, 2024
2 parents 69ca0d9 + 632ae27 commit 37a4c39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/Mojo/Server/Morbo/Backend/Poll.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ use Mojo::File qw(path);
sub modified_files {
my $self = shift;

my $cache = $self->{cache} //= {};
my $cache = $self->{cache} //= {};
my $known_files = {map { $_ => 1 } keys %$cache};
my @files;
for my $file (map { -f $_ && -r _ ? $_ : _list($_) } @{$self->watch}) {
delete $known_files->{$file};
my ($size, $mtime) = (stat $file)[7, 9];
next unless defined $size and defined $mtime;
my $stats = $cache->{$file} ||= [$^T, $size];
next if $mtime <= $stats->[0] && $size == $stats->[1];
@$stats = ($mtime, $size);
push @files, $file;
}

for my $file (keys %$known_files) {

# Any leftover file means they were deleted
push @files, $file;
delete $cache->{$file};
}

sleep $self->watch_timeout unless @files;

return \@files;
Expand Down
13 changes: 13 additions & 0 deletions t/mojo/morbo.t
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ EOF
is $tx->res->body, 'Hello!', 'right content';
};

subtest 'Watch for deleted files' => sub {
my $morbo = Mojo::Server::Morbo->new;
$morbo->backend->watch([$dir]);
$morbo->backend->modified_files;
my $tmp_file = $dir->child('tmp_file.txt');
$tmp_file->spew("some data");
is_deeply $morbo->backend->modified_files, [$tmp_file], 'file was created';
is_deeply $morbo->backend->modified_files, [], 'no files changed';
unlink $tmp_file;
is_deeply $morbo->backend->modified_files, [$tmp_file], 'file was deleted';
is_deeply $morbo->backend->modified_files, [], 'no files changed';
};

subtest 'New file(s)' => sub {
is_deeply $morbo->backend->modified_files, [], 'directory has not changed';
my @new = map { $subdir->child("$_.txt") } qw/test testing/;
Expand Down

0 comments on commit 37a4c39

Please sign in to comment.