]> git.openstreetmap.org Git - chef.git/commitdiff
Make sure we don't lose changes if we are interrupted
authorGrant Slater <git@firefishy.com>
Thu, 23 Jan 2014 15:27:26 +0000 (15:27 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 23 Jan 2014 15:28:35 +0000 (15:28 +0000)
If we are interrupted after we have fetched changes, but before we
have applied them, then rollback so that we try againt next time.

cookbooks/tile/templates/default/replicate.erb

index 5d940375bf36e60d6e96a19c5b5692fc6b3e6a1e..e1ecab695d27ba0a68ac8c5b864076713c394207 100644 (file)
@@ -6,12 +6,20 @@
 # Setting to midnight ensures we get conistent data after first run
 # osmosis --read-replication-interval-init
 
 # Setting to midnight ensures we get conistent data after first run
 # osmosis --read-replication-interval-init
 
+# Define exit handler
+function onexit {
+    [ -f state-prev.txt ] && mv state-prev.txt state.txt
+}
+
 # Send output to the log
 exec > /var/log/tile/replicate.log 2>&1
 
 # Change to the replication state directory
 cd /var/lib/replicate
 
 # Send output to the log
 exec > /var/log/tile/replicate.log 2>&1
 
 # Change to the replication state directory
 cd /var/lib/replicate
 
+# Install exit handler
+trap onexit EXIT
+
 # Read in initial state
 . state.txt
 
 # Read in initial state
 . state.txt
 
@@ -21,6 +29,9 @@ do
     # Work out the name of the next file
     file="changes-${sequenceNumber}.osm.gz"
 
     # Work out the name of the next file
     file="changes-${sequenceNumber}.osm.gz"
 
+    # Save state file so we can rollback if an error occurs
+    cp state.txt state-prev.txt
+
     # Fetch the next set of changes
     osmosis --read-replication-interval --simc --write-xml-change file="${file}" compressionMethod="gzip"
 
     # Fetch the next set of changes
     osmosis --read-replication-interval --simc --write-xml-change file="${file}" compressionMethod="gzip"
 
@@ -45,6 +56,9 @@ do
            # Remove file, it will just be an empty changeset
            rm ${file}
 
            # Remove file, it will just be an empty changeset
            rm ${file}
 
+            # No need to rollback now
+            rm state-prev.txt
+
             # Sleep for a short while
            sleep 30
        else
             # Sleep for a short while
            sleep 30
        else
@@ -58,6 +72,9 @@ do
             osm2pgsql --slim --append ${file}
 <% end -%>
 
             osm2pgsql --slim --append ${file}
 <% end -%>
 
+            # No need to rollback now
+            rm state-prev.txt
+
             # Expire tiles which are touched by the changes
             sudo -u www-data /usr/local/bin/expire-tiles ${file} > /dev/null 2>&1 &
        fi
             # Expire tiles which are touched by the changes
             sudo -u www-data /usr/local/bin/expire-tiles ${file} > /dev/null 2>&1 &
        fi