Add a cron job to cleanup old tiles
authorTom Hughes <tom@compton.nu>
Fri, 23 Aug 2013 23:33:49 +0000 (00:33 +0100)
committerTom Hughes <tom@compton.nu>
Fri, 23 Aug 2013 23:40:10 +0000 (00:40 +0100)
cookbooks/tile/recipes/default.rb
cookbooks/tile/templates/default/cleanup-tiles.cron.erb [new file with mode: 0644]
cookbooks/tile/templates/default/cleanup-tiles.erb [new file with mode: 0644]

index 9c219e62db48e8cf22689164b05cdf17e7634b50..4f3cc5002e54e36f74e6d35805eedcbb8890b0ce 100644 (file)
@@ -473,6 +473,27 @@ template "/etc/rsyslog.d/20-renderd.conf" do
   notifies :restart, "service[rsyslog]"
 end
 
+package "libfilesys-df-perl"
+
+template "/usr/local/bin/cleanup-tiles" do
+  source "cleanup-tiles.erb"
+  owner "root"
+  group "root"
+  mode 0644
+end
+
+tile_directories = node[:tile][:styles].collect do |name,style|
+  style[:tile_directories].collect { |directory| directory[:name] }
+end.flatten.sort.uniq
+
+template "/etc/cron.d/cleanup-tiles" do
+  source "cleanup-tiles.cron.erb"
+  owner "root"
+  group "root"
+  mode 0644
+  variables :directories => tile_directories
+end
+
 munin_plugin "mod_tile_fresh"
 munin_plugin "mod_tile_response"
 munin_plugin "mod_tile_zoom"
diff --git a/cookbooks/tile/templates/default/cleanup-tiles.cron.erb b/cookbooks/tile/templates/default/cleanup-tiles.cron.erb
new file mode 100644 (file)
index 0000000..7c235a9
--- /dev/null
@@ -0,0 +1,5 @@
+# DO NOT EDIT - This file is being maintained by Chef
+
+<% @directories.each do |directory| -%>
+0 1 * * * www-data ionice -c 3 /usr/local/bin/cleanup-tiles <%= directory %>
+<% end -%>
diff --git a/cookbooks/tile/templates/default/cleanup-tiles.erb b/cookbooks/tile/templates/default/cleanup-tiles.erb
new file mode 100644 (file)
index 0000000..31a7fad
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# DO NOT EDIT - This file is being maintained by Chef
+
+use strict;
+use warnings;
+
+use File::Temp qw(tmpnam);
+use Filesys::Df;
+
+my $tiledir = shift @ARGV;
+my $tempfile = tmpnam();
+
+if (df($tiledir)->{per} > 90)
+{
+    system("find", $tiledir, "-xdev", "-type", "f", "-name", "*.meta", "-atime", "+60", "-fprintf", $tempfile, "%A@ %p\n");
+
+    open(TILES, "-|", "sort", "-n", $tempfile) || die "Can't open $tempfile: $!";
+
+    while (df($tiledir)->{per} > 80)
+    {
+        for my $n (1..1000)
+        {
+            if (defined(my $line = readline(TILES)))
+            {
+                chomp $line;
+
+                my($time,$tile) = split(/ /, $line);
+
+                unlink($tile) || warn "Can't remove $tile: $!";
+            }
+        }
+    }
+
+    close(TILES);
+}
+
+unlink($tempfile);
+
+exit 0;