Skip to content

Commit

Permalink
Pull repeated code to try something a number of...
Browse files Browse the repository at this point in the history
of times into a sub.
  • Loading branch information
MasterDuke17 committed Jun 17, 2021
1 parent a9510b8 commit 7dfed29
Showing 1 changed file with 35 additions and 50 deletions.
85 changes: 35 additions & 50 deletions src/core.c/CompUnit/PrecompilationStore/File.pm6
Expand Up @@ -285,30 +285,36 @@ class CompUnit::PrecompilationStore::File
$dest.add($precomp-id ~ $extension)
}

# File renaming can easily race and fail on Windows. There's no great solution,
# so instead just try 10 times catching a failure (and returning out of the
# loop and sub if it succeeds).
my sub try-rename-n-times(&rename-block, $n is copy --> Bool:D) {
while $n-- {
&rename-block();
CATCH {
when X::IO::Rename {
sleep 0.1;
next;
}
}
return True;
}
return False;
}

method store-file(
CompUnit::PrecompilationId:D $compiler-id,
CompUnit::PrecompilationId:D $precomp-id,
IO::Path:D $path,
Str:D :$extension = ''
) {
my &rename-block = { $path.rename(self!file($compiler-id, $precomp-id, :$extension)); };
if $*DISTRO.is-win {
# File renaming can easily race and fail on Windows. There's no great solution,
# so instead just try 10 times catching a failure (and returning out of the
# loop and method if it succeeds), and if it hasn't succeeded after that, try
# one more time without catching a failure.
my $retry-count = 10;
while $retry-count-- {
$path.rename(self!file($compiler-id, $precomp-id, :$extension));
CATCH {
when X::IO::Rename {
sleep 0.1;
next;
}
}
return;
}
# If the rename attempts don't succeed, we'll end up
# trying again one more time but not catching any failures.
return if try-rename-n-times(&rename-block, 10);
}
$path.rename(self!file($compiler-id, $precomp-id, :$extension));
&rename-block();
}

method store-unit(
Expand All @@ -318,26 +324,16 @@ class CompUnit::PrecompilationStore::File
) {
my $precomp-file := self!file($compiler-id, $precomp-id, :extension<.tmp>);
$unit.save-to($precomp-file);
my &rename-block = {
$precomp-file.rename(self!file($compiler-id, $precomp-id));
self.remove-from-cache($precomp-id);
};
if $*DISTRO.is-win {
# File renaming can easily race and fail on Windows. There's no great solution,
# so instead just try 10 times catching a failure (and returning out of the
# loop and method if it succeeds), and if it hasn't succeeded after that, try
# one more time without catching a failure.
my $retry-count = 10;
while $retry-count-- {
$precomp-file.rename(self!file($compiler-id, $precomp-id));
CATCH {
when X::IO::Rename {
sleep 0.1;
next;
}
}
self.remove-from-cache($precomp-id);
return;
}
# If the rename attempts don't succeed, we'll end up
# trying again one more time but not catching any failures.
return if try-rename-n-times(&rename-block, 10);
}
$precomp-file.rename(self!file($compiler-id, $precomp-id));
self.remove-from-cache($precomp-id);
&rename-block();
}

method store-repo-id(
Expand All @@ -347,24 +343,13 @@ class CompUnit::PrecompilationStore::File
) {
my $repo-id-file := self!file($compiler-id, $precomp-id, :extension<.repo-id.tmp>);
$repo-id-file.spurt($repo-id);
my &rename-block = { $repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>)); };
if $*DISTRO.is-win {
# File renaming can easily race and fail on Windows. There's no great solution,
# so instead just try 10 times catching a failure (and returning out of the
# loop and method if it succeeds), and if it hasn't succeeded after that, try
# one more time without catching a failure.
my $retry-count = 10;
while $retry-count-- {
$repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>));
CATCH {
when X::IO::Rename {
sleep 0.1;
next;
}
}
return;
}
# If the rename attempts don't succeed, we'll end up
# trying again one more time but not catching any failures.
return if try-rename-n-times(&rename-block, 10);
}
$repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>));
&rename-block();
}

method delete(
Expand Down

0 comments on commit 7dfed29

Please sign in to comment.