| 1 |
package Test2::Harness::Runner::Reloader; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak/; |
| 8 |
use Time::HiRes qw/time/; |
| 9 |
use Test2::Harness::Util qw/file2mod is_same_file/; |
| 10 |
|
| 11 |
use File::Spec(); |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
local $@; |
| 15 |
my $inotify = eval { require Linux::Inotify2; 1 }; |
| 16 |
if ($inotify) { |
| 17 |
my $MASK = Linux::Inotify2::IN_MODIFY(); |
| 18 |
$MASK |= Linux::Inotify2::IN_ATTRIB(); |
| 19 |
$MASK |= Linux::Inotify2::IN_DELETE_SELF(); |
| 20 |
$MASK |= Linux::Inotify2::IN_MOVE_SELF(); |
| 21 |
$MASK |= Linux::Inotify2::IN_MOVE_SELF(); |
| 22 |
|
| 23 |
*USE_INOTIFY = sub() { 1 }; |
| 24 |
require constant; |
| 25 |
constant->import(INOTIFY_MASK => $MASK); |
| 26 |
} |
| 27 |
else { |
| 28 |
*USE_INOTIFY = sub() { 0 }; |
| 29 |
*INOTIFY_MASK = sub() { 0 }; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
use Test2::Harness::Util::HashBase qw{ |
| 34 |
<notify_cb <find_loaded_cb <should_watch_cb <can_reload_cb <reload_cb <delete_symbol_cb |
| 35 |
<monitored <monitor_lookup |
| 36 |
<watcher |
| 37 |
<stat_min_gap <stat_last_checked |
| 38 |
<pid |
| 39 |
}; |
| 40 |
|
| 41 |
sub _pid_check { |
| 42 |
my $self = shift; |
| 43 |
|
| 44 |
return 1 unless USE_INOTIFY; |
| 45 |
|
| 46 |
my $pid = $self->{+PID} //= $$; |
| 47 |
|
| 48 |
croak "PID has changed $$ vs $pid (Maybe you need to call reset()?)" |
| 49 |
unless $$ == $pid; |
| 50 |
|
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
sub init { |
| 55 |
my $self = shift; |
| 56 |
$self->{+CAN_RELOAD_CB} //= $self->can('_can_reload'); |
| 57 |
$self->{+FIND_LOADED_CB} //= $self->can('_find_loaded'); |
| 58 |
$self->{+STAT_MIN_GAP} //= 2; |
| 59 |
|
| 60 |
$self->reset; |
| 61 |
} |
| 62 |
|
| 63 |
sub reset { |
| 64 |
my $self = shift; |
| 65 |
delete $self->{+PID}; |
| 66 |
$self->{+MONITORED} = {}; |
| 67 |
$self->{+MONITOR_LOOKUP} = {}; |
| 68 |
if (USE_INOTIFY) { |
| 69 |
$self->{+WATCHER} = Linux::Inotify2->new; |
| 70 |
$self->{+WATCHER}->blocking(0); |
| 71 |
} else { |
| 72 |
$self->{+WATCHER} = {}; |
| 73 |
} |
| 74 |
delete $self->{+STAT_LAST_CHECKED}; |
| 75 |
} |
| 76 |
|
| 77 |
sub _find_loaded { keys %INC } |
| 78 |
|
| 79 |
sub refresh { |
| 80 |
my $self = shift; |
| 81 |
|
| 82 |
$self->_pid_check(); |
| 83 |
|
| 84 |
my $monitored = $self->{+MONITORED}; |
| 85 |
|
| 86 |
my $cb = $self->{+FIND_LOADED_CB}; |
| 87 |
for my $file ($self->$cb($monitored)) { |
| 88 |
next if exists $monitored->{$file}; |
| 89 |
$self->monitor($file); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
sub monitor { |
| 94 |
my $self = shift; |
| 95 |
my ($file) = @_; |
| 96 |
|
| 97 |
$self->_pid_check(); |
| 98 |
|
| 99 |
my $monitored = $self->{+MONITORED}; |
| 100 |
return if exists $monitored->{$file}; |
| 101 |
|
| 102 |
my $watch = $self->find_file_to_watch($file); |
| 103 |
|
| 104 |
return $monitored->{$file} = 0 unless $watch && -e $watch; |
| 105 |
|
| 106 |
if (my $should_watch_cb = $self->{+SHOULD_WATCH_CB}) { |
| 107 |
return $monitored->{$file} = 0 unless $self->$should_watch_cb($file => $watch); |
| 108 |
} |
| 109 |
|
| 110 |
if (USE_INOTIFY) { |
| 111 |
my $inotify = $self->{+WATCHER}; |
| 112 |
$inotify->watch($watch, INOTIFY_MASK()); |
| 113 |
} |
| 114 |
else { |
| 115 |
my $stats = $self->{+WATCHER}; |
| 116 |
$stats->{$watch} = $self->_get_file_times($watch); |
| 117 |
} |
| 118 |
|
| 119 |
$self->{+MONITOR_LOOKUP}->{$watch} = $file; |
| 120 |
$monitored->{$file} = $watch; |
| 121 |
return $watch; |
| 122 |
} |
| 123 |
|
| 124 |
sub find_file_to_watch { |
| 125 |
my $self = shift; |
| 126 |
my ($file) = @_; |
| 127 |
|
| 128 |
return $INC{$file} if $INC{$file} && -e $INC{$file}; |
| 129 |
|
| 130 |
for my $dir (@INC) { |
| 131 |
next if ref($dir); |
| 132 |
my $path = File::Spec->catfile($dir, $file); |
| 133 |
return $path if -f $path; |
| 134 |
} |
| 135 |
|
| 136 |
return $file if -e $file; |
| 137 |
} |
| 138 |
|
| 139 |
sub _get_file_times { |
| 140 |
my $self = shift; |
| 141 |
my ($file) = @_; |
| 142 |
my (undef, undef, undef, undef, undef, undef, undef, undef, undef, $mtime, $ctime) = stat($file); |
| 143 |
return [$mtime, $ctime]; |
| 144 |
} |
| 145 |
|
| 146 |
sub _get_changes { |
| 147 |
my $self = shift; |
| 148 |
|
| 149 |
if (USE_INOTIFY) { |
| 150 |
my $inotify = $self->{+WATCHER}; |
| 151 |
my @todo = $inotify->read or return; |
| 152 |
return {map { ($_->fullname() => 1) } @todo}; |
| 153 |
} |
| 154 |
|
| 155 |
# Do not hammer the disk getting stat |
| 156 |
my $check_time = time; |
| 157 |
my $gap = $self->{+STAT_MIN_GAP}; |
| 158 |
my $last_checked = $self->{+STAT_LAST_CHECKED}; |
| 159 |
return if $last_checked && $gap && $gap > ($check_time - $last_checked); |
| 160 |
$last_checked = $check_time; |
| 161 |
|
| 162 |
my $found = 0; |
| 163 |
my $changed = {}; |
| 164 |
my $stats = $self->{+WATCHER}; |
| 165 |
for my $file (keys %$stats) { |
| 166 |
my $old_times = $stats->{$file}; |
| 167 |
my $new_times = $self->_get_file_times($file); |
| 168 |
|
| 169 |
# Compare times |
| 170 |
next if $old_times->[0] == $new_times->[0] && $old_times->[1] == $new_times->[1]; |
| 171 |
|
| 172 |
# Update in case we choose not to reload |
| 173 |
$stats->{$file} = $new_times; |
| 174 |
|
| 175 |
$found++; |
| 176 |
$changed->{$file} = 1; |
| 177 |
} |
| 178 |
|
| 179 |
return unless $found; |
| 180 |
return $changed; |
| 181 |
} |
| 182 |
|
| 183 |
sub _can_reload { |
| 184 |
my %params = @_; |
| 185 |
|
| 186 |
my $mod = $params{module}; |
| 187 |
|
| 188 |
return 1 unless $mod->can('import'); |
| 189 |
|
| 190 |
return 0 if $mod->can('IMPORTER_MENU'); |
| 191 |
|
| 192 |
{ |
| 193 |
no strict 'refs'; |
| 194 |
return 0 if @{"$mod\::EXPORT"}; |
| 195 |
return 0 if @{"$mod\::EXPORT_OK"}; |
| 196 |
} |
| 197 |
|
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
sub reload_changes { |
| 202 |
my $self = shift; |
| 203 |
|
| 204 |
$self->_pid_check(); |
| 205 |
|
| 206 |
my $monitored = $self->{+MONITORED}; |
| 207 |
|
| 208 |
$self->refresh(); |
| 209 |
|
| 210 |
my $changed = $self->_get_changes() or return; |
| 211 |
|
| 212 |
my $notify_cb = $self->{+NOTIFY_CB}; |
| 213 |
|
| 214 |
$notify_cb->(changes_detected => [keys %$changed]) if $notify_cb; |
| 215 |
|
| 216 |
my %out; |
| 217 |
for my $file (sort keys %$changed) { |
| 218 |
if (USE_INOTIFY) { |
| 219 |
my $inotify = $self->{+WATCHER}; |
| 220 |
$inotify->watch($file, INOTIFY_MASK()); |
| 221 |
} |
| 222 |
|
| 223 |
$notify_cb->(file_changed => $file) if $notify_cb; |
| 224 |
|
| 225 |
my $rel = $self->{+MONITOR_LOOKUP}->{$file}; |
| 226 |
my $mod = file2mod($rel); |
| 227 |
my %params = (reloader => $self, file => $file, relative => $rel, module => $mod, notify_cb => $notify_cb); |
| 228 |
|
| 229 |
my ($status, %fields) = $self->_reload_file(%params); |
| 230 |
|
| 231 |
$out{$file} = { |
| 232 |
file => $file, |
| 233 |
relative => $rel, |
| 234 |
module => $mod, |
| 235 |
reloaded => $status, |
| 236 |
%fields, |
| 237 |
}; |
| 238 |
} |
| 239 |
|
| 240 |
return \%out; |
| 241 |
} |
| 242 |
|
| 243 |
sub _reload_file { |
| 244 |
my $self = shift; |
| 245 |
my %params = @_; |
| 246 |
|
| 247 |
if (my $reload_cb = $self->{+RELOAD_CB}) { |
| 248 |
my ($status, %fields) = $reload_cb->(%params); |
| 249 |
return ($status, %fields) if defined $status; |
| 250 |
} |
| 251 |
|
| 252 |
if (my $can_reload_cb = $self->{+CAN_RELOAD_CB}) { |
| 253 |
my ($can, %fields) = $can_reload_cb->(%params); |
| 254 |
return ($can, %fields) unless $can; |
| 255 |
} |
| 256 |
|
| 257 |
my $notify_cb = delete $params{notify_cb}; |
| 258 |
$notify_cb->(reload_inplace => \%params) if $notify_cb; |
| 259 |
|
| 260 |
my $del_cb = $self->{+DELETE_SYMBOL_CB}; |
| 261 |
my ($file, $rel, $mod) = @params{qw/file relative module/}; |
| 262 |
|
| 263 |
my @warnings; |
| 264 |
my $ok = eval { |
| 265 |
local $SIG{__WARN__} = sub { push @warnings => @_ }; |
| 266 |
|
| 267 |
my $stash = do { no strict 'refs'; \%{"${mod}\::"} }; |
| 268 |
for my $sym (keys %$stash) { |
| 269 |
next if $sym =~ m/::$/; |
| 270 |
|
| 271 |
next if $del_cb && $del_cb->(%params, symbol => $sym, stash => $stash); |
| 272 |
|
| 273 |
delete $stash->{$sym}; |
| 274 |
} |
| 275 |
|
| 276 |
delete $INC{$rel}; |
| 277 |
local $.; |
| 278 |
require $rel; |
| 279 |
die "Reloading '$rel' loaded '$INC{$rel}' instead of '$file', \@INC must have been altered" |
| 280 |
unless is_same_file($file, $INC{$rel}); |
| 281 |
|
| 282 |
1; |
| 283 |
}; |
| 284 |
my $err = $@; |
| 285 |
|
| 286 |
return (1) if $ok && !@warnings; |
| 287 |
|
| 288 |
$notify_cb->(reload_fail => {%params, warnings => \@warnings, error => $err}) if $notify_cb; |
| 289 |
|
| 290 |
return (undef, error => $err, warnings => \@warnings); |
| 291 |
} |
| 292 |
|
| 293 |
1; |
| 294 |
|
| 295 |
__END__ |
| 296 |
|
| 297 |
=pod |
| 298 |
|
| 299 |
=encoding UTF-8 |
| 300 |
|
| 301 |
=head1 NAME |
| 302 |
|
| 303 |
Test2::Harness::Runner::Reloader - reload logic. |
| 304 |
|
| 305 |
=head1 DESCRIPTION |
| 306 |
|
| 307 |
=head1 SOURCE |
| 308 |
|
| 309 |
The source code repository for Test2-Harness can be found at |
| 310 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 311 |
|
| 312 |
=head1 MAINTAINERS |
| 313 |
|
| 314 |
=over 4 |
| 315 |
|
| 316 |
=item Chad Granum E<lt>[email protected]<gt> |
| 317 |
|
| 318 |
=back |
| 319 |
|
| 320 |
=head1 AUTHORS |
| 321 |
|
| 322 |
=over 4 |
| 323 |
|
| 324 |
=item Chad Granum E<lt>[email protected]<gt> |
| 325 |
|
| 326 |
=back |
| 327 |
|
| 328 |
=head1 COPYRIGHT |
| 329 |
|
| 330 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 331 |
|
| 332 |
This program is free software; you can redistribute it and/or |
| 333 |
modify it under the same terms as Perl itself. |
| 334 |
|
| 335 |
See F<http://dev.perl.org/licenses/> |
| 336 |
|
| 337 |
=cut |
| 338 |
|