* Register builds as GC roots so they don't get deleted.

This commit is contained in:
Eelco Dolstra 2009-02-06 21:01:20 +00:00
parent dcacf2c790
commit 66602def16
4 changed files with 38 additions and 15 deletions

View file

@ -2,11 +2,13 @@ package Hydra::Helper::Nix;
use strict;
use Exporter;
use File::Path;
use File::Basename;
our @ISA = qw(Exporter);
our @EXPORT = qw(
isValidPath getHydraPath getHydraDBPath openHydraDB
registerRoot getGCRootsDir
getPrimaryBuildsForReleaseSet getRelease getLatestSuccessfulRelease );
@ -42,6 +44,28 @@ sub openHydraDB {
}
sub getGCRootsDir {
die unless defined $ENV{LOGNAME};
return "/nix/var/nix/gcroots/per-user/$ENV{LOGNAME}/hydra-roots";
}
sub registerRoot {
my ($path) = @_;
my $gcRootsDir = getGCRootsDir;
mkpath($gcRootsDir) if !-e $gcRootsDir;
my $link = "$gcRootsDir/" . basename $path;
if (!-l $link) {
symlink($path, $link)
or die "cannot create symlink in $gcRootsDir to $path";
}
}
sub attrsToSQL {
my ($attrs, $id) = @_;
my @attrs = split / /, $attrs;

View file

@ -25,6 +25,8 @@ sub doBuild {
my $errormsg = undef;
registerRoot $outPath;
if (!isValidPath($outPath)) {
$isCachedBuild = 0;

View file

@ -248,6 +248,9 @@ sub checkJob {
, sha256hash => $input->{sha256hash}
});
}
# !!! this should really by done by nix-instantiate to prevent a GC race.
registerRoot $drvPath;
});
};

View file

@ -9,25 +9,12 @@ use POSIX qw(strftime);
my $db = openHydraDB;
die unless defined $ENV{LOGNAME};
my $gcRootsDir = "/nix/var/nix/gcroots/per-user/$ENV{LOGNAME}/hydra-roots";
my %roots;
sub registerRoot {
my ($path) = @_;
#print "$path\n";
mkpath($gcRootsDir) if !-e $gcRootsDir;
my $link = "$gcRootsDir/" . basename $path;
if (!-l $link) {
symlink($path, $link)
or die "cannot create symlink in $gcRootsDir to $path";
}
Hydra::Helper::Nix::registerRoot($path);
$roots{$path} = 1;
}
@ -97,11 +84,14 @@ my @buildsToKeep = $db->resultset('Builds')->search({finished => 1, keep => 1},
keepBuild $_ foreach @buildsToKeep;
# For scheduled builds, we register the derivation as a GC root.
# For scheduled builds, we register the derivation and the output as a GC root.
print "*** looking for scheduled builds\n";
foreach my $build ($db->resultset('Builds')->search({finished => 0}, {join => 'schedulingInfo'})) {
if (isValidPath($build->drvpath)) {
print "keeping scheduled build ", $build->id, " (",
strftime("%Y-%m-%d %H:%M:%S", localtime($build->timestamp)), ")\n";
registerRoot $build->drvpath;
registerRoot $build->outpath;
} else {
print STDERR "warning: derivation ", $build->drvpath, " has disappeared\n";
}
@ -109,6 +99,10 @@ foreach my $build ($db->resultset('Builds')->search({finished => 0}, {join => 's
# Remove existing roots that are no longer wanted. !!! racy
print "*** removing unneeded GC roots\n";
my $gcRootsDir = getGCRootsDir;
opendir DIR, $gcRootsDir or die;
foreach my $link (readdir DIR) {