-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvacuum.pl
76 lines (64 loc) · 1.84 KB
/
vacuum.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/perl
use strict;
use warnings;
use File::Path;
use CSplat::Util qw/run_service/;
use CSplat::Config;
use CSplat::TtyrecDir;
# How old a ttyrec directory must be before it will be blown away
my $VACUUM_THRESHOLD_SECONDS = 3 * 24 * 60 * 60; # 72h
main();
sub main {
run_service('vacuum', \&run_vacuum);
}
sub each_ttyrec_directory(&) {
my $action = shift;
my $dir = CSplat::TtyrecDir->new(CSplat::Config::ttyrec_dir());
$dir->each_player_directory($action);
}
sub file_older_than {
my ($file, $threshold) = @_;
my $mtime = (stat $file)[9];
$mtime && time() - $mtime > $threshold
}
sub newest_ttyrec_needs_vacuum {
my $dir = shift;
my @ttyrecs = glob("$dir/*.ttyrec");
my $newest_mtime;
for my $ttyrec (@ttyrecs) {
my $mtime = (stat $ttyrec)[9];
$newest_mtime = $mtime if !$newest_mtime || $mtime > $newest_mtime;
}
return $newest_mtime && time() - $newest_mtime > $VACUUM_THRESHOLD_SECONDS;
}
sub directory_needs_vacuum {
my $dir = shift;
my $fetch_timestamp_file = CSplat::TtyrecDir->timestamp_file($dir);
unless (-f $fetch_timestamp_file) {
return newest_ttyrec_needs_vacuum($dir);
}
return file_older_than($fetch_timestamp_file, $VACUUM_THRESHOLD_SECONDS);
}
sub run_vacuum {
while (1) {
each_ttyrec_directory(sub {
my $dir = shift;
sleep 1;
eval {
CSplat::TtyrecDir->lock_for_vacuum($dir, 2,
sub {
if (directory_needs_vacuum($dir)) {
vacuum_dir($dir);
}
});
};
warn "$@" if $@;
});
sleep 10;
}
}
sub vacuum_dir {
my $dir = shift;
print "Deleting obsolete directory: $dir\n";
rmtree($dir) or die "Couldn't delete $dir: $!\n";
}