Use a lock file to stop multiple tile expirys running at once
[chef.git] / cookbooks / tile / templates / default / cleanup-tiles.erb
1 #!/usr/bin/perl
2
3 # DO NOT EDIT - This file is being maintained by Chef
4
5 use strict;
6 use warnings;
7
8 use File::Temp qw(tmpnam);
9 use Filesys::Df;
10 use LockFile::Simple qw(lock unlock);
11
12 my $tiledir = shift @ARGV;
13
14 lock($tiledir) || exit 0;
15
16 my $tempfile = tmpnam();
17
18 if (df($tiledir)->{per} > 90)
19 {
20     system("find", $tiledir, "-xdev", "-type", "f", "-name", "*.meta", "-atime", "+35", "-fprintf", $tempfile, "%A@ %p\n");
21
22     open(TILES, "-|", "sort", "-n", $tempfile) || die "Can't open $tempfile: $!";
23
24     while (df($tiledir)->{per} > 80)
25     {
26         for my $n (1..1000)
27         {
28             if (defined(my $line = readline(TILES)))
29             {
30                 chomp $line;
31
32                 my($time,$tile) = split(/ /, $line);
33
34                 unlink($tile) || warn "Can't remove $tile: $!";
35             }
36             else
37             {
38                 goto done;
39             }
40         }
41     }
42
43   done:
44
45     close(TILES);
46
47     warn "Tile expiry finished before threshold reached" if df($tiledir)->{per} > 80;
48 }
49
50 unlink($tempfile);
51
52 unlock($tiledir);
53
54 exit 0;