From 3507313013238e4c5f9b5bb34ce0925e39f6f387 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Mon, 2 Dec 2013 09:07:11 +0200 Subject: [PATCH] Make generate more reliable --- lib/Gruntmaster/Page.pm | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/Gruntmaster/Page.pm b/lib/Gruntmaster/Page.pm index 12e7329..00f2cdb 100644 --- a/lib/Gruntmaster/Page.pm +++ b/lib/Gruntmaster/Page.pm @@ -6,9 +6,11 @@ use warnings; use parent qw/Exporter/; our @EXPORT_OK = qw/generate/; +use Fcntl qw/:flock/; use File::Basename qw/fileparse/; use File::Slurp qw/write_file/; use IO::Compress::Gzip qw/gzip/; +use IO::File; our $VERSION = '0.001'; our @generators; @@ -46,19 +48,27 @@ sub generate{ my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/; my $basename = fileparse $path_noext; - open my $typemap, ">$path_noext.var"; + IO::File->new(">$path_noext.var")->close unless -f "$path_noext.var"; + flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX; + open my $typemap, ">$path_noext.var.new"; say $typemap "URI: $basename\n"; for my $gen(@generators) { my ($regex, $generator) = @$gen; next unless $path_noext =~ $regex; for my $lang (@{LANGUAGES()}) { my $page = $generator->($path, $lang); - write_file "$path_noext.$lang.$ext", $page; + write_file "$path_noext.$lang.$ext.new", $page; say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n"; - gzip \$page => "$path_noext.$lang.gz.$ext", Minimal => 1; + gzip \$page => "$path_noext.$lang.gz.$ext.new", Minimal => 1; say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n"; } } + + for my $lang(@{LANGUAGES()}) { + rename "$path_noext.$lang.$ext.new", "$path_noext.$lang.$ext"; + rename "$path_noext.$lang.gz.$ext.new", "$path_noext.$lang.gz.$ext"; + } + rename "$path_noext.var.new", "$path_noext.var"; close $typemap; } -- 2.39.2