maposmatic-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Maposmatic-dev] [PATCH 4/4] Read the GIS database lag from a local file


From: Thomas Petazzoni
Subject: [Maposmatic-dev] [PATCH 4/4] Read the GIS database lag from a local file
Date: Wed, 18 Apr 2012 22:19:22 +0200

The GIS database lag information was directly read from the GIS
database for every web page rendered by the MapOSMatic web
server. This is very inefficient since this value changes very
slowly. Instead, read the value from a local file which gets updated
from time to time by a cronjob using a small shell script.

Signed-off-by: Thomas Petazzoni <address@hidden>
---
 support/update-database-lag-file     |   15 +++++++++++++++
 www/maposmatic/context_processors.py |   19 +++++++++----------
 www/settings_local.py-template       |    4 ++++
 3 files changed, 28 insertions(+), 10 deletions(-)
 create mode 100644 support/update-database-lag-file

diff --git a/support/update-database-lag-file b/support/update-database-lag-file
new file mode 100644
index 0000000..3c371b5
--- /dev/null
+++ b/support/update-database-lag-file
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# First argument: database hostname. We make the assumption that
+# ~/.pgpass contains the necessary information to allow the connection
+# to the database.
+#
+# Second argument: output file.
+#
+# This script typically needs to be executed as a cronjob
+
+DATABASE=$1
+OUTPUT_FILE=$2
+
+echo "select last_update from maposmatic_admin;" | \
+    psql -h ${DATABASE} -A -t > ${OUTPUT_FILE}
diff --git a/www/maposmatic/context_processors.py 
b/www/maposmatic/context_processors.py
index f09ddef..dc9cc9a 100644
--- a/www/maposmatic/context_processors.py
+++ b/www/maposmatic/context_processors.py
@@ -24,6 +24,7 @@
 from django.core.urlresolvers import reverse
 import django.utils.translation
 import feedparser
+import datetime
 
 from models import MapRenderingJob
 import www.settings
@@ -36,19 +37,17 @@ def get_latest_blog_posts():
     return f.entries[:5]
 
 def get_osm_database_last_update():
-    db = gisdb.get()
-    if db is None:
+    try:
+        f = open(www.settings.GIS_DATABASE_LAG_FILE)
+    except IOError:
         return None
-    cursor = db.cursor()
-    query = "select last_update from maposmatic_admin;"
+
+    s = f.readline().strip()
     try:
-        cursor.execute(query)
-    except psycopg2.ProgrammingError:
-        db.rollback()
+        d = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
+    except ValueError:
         return None
-    # Extract datetime object. It is located as the first element
-    # of a tuple, itself the first element of an array.
-    return cursor.fetchall()[0][0]
+    return d
 
 def all(request):
     # Do not add the useless overhead of parsing blog entries when generating
diff --git a/www/settings_local.py-template b/www/settings_local.py-template
index db44d6d..306831f 100644
--- a/www/settings_local.py-template
+++ b/www/settings_local.py-template
@@ -45,6 +45,10 @@ DATABASES = {
 # Path to ocitysmap's config file to use, or None for the default in ~/
 OCITYSMAP_CFG_PATH = None
 
+# Path to the file containing the datetime of the state of the GIS
+# database. It should be of the form YYYY-MM-DD HH:MM:SS.
+GIS_DATABASE_LAG_FILE = '/path/to/gis/database/lag/file'
+
 RENDERING_RESULT_PATH = '/path/to/rendering/results/'
 RENDERING_RESULT_URL = '/results/' # Either a relative URL or an absolute URL
 RENDERING_RESULT_FORMATS = ['png', 'svgz', 'pdf', 'csv']
-- 
1.7.5.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]