Batch-convert and mapnik version detection support in reset_styles

This commit is contained in:
Sandro Santilli 2012-10-08 18:06:12 +02:00
parent 89ca4e1a5a
commit 703c63d5d6
2 changed files with 66 additions and 15 deletions

View File

@ -5,6 +5,7 @@
* Include version in GET /style response
* Support version and convert parameters in POST /style request
* Autodetect target mapnik version and let config override it
* Add tools/reset_styles script to batch-reset (and optionally convert) styles
* Configurable logging format (#4)
* Detailed error on missing user metadata
* Properly handle unauthenticated requests for metadata

View File

@ -1,35 +1,85 @@
#!/usr/bin/env node
// Reset redis-stored XML styles so that they are regenerated
// from CartoCSS on next tile request
var path = require('path');
var redis = require('redis')
// Reset all styles in the store
var grainstore = require('../node_modules/grainstore/lib/grainstore');
var mapnik = require('mapnik');
var redis = require('redis');
var REDIS_PORT = 6379; // TODO: make a parameter
function usage(me, exitcode) {
console.log("Usage: " + me + " [--convert] [<target_mapnik_version>]");
process.exit(exitcode);
}
var doConvert = false;
var MAPNIK_VERSION;
var node_path = process.argv.shift();
var script_path = process.argv.shift();
var me = path.basename(script_path);
var arg;
while ( arg = process.argv.shift() ) {
if ( arg == '--convert' ) {
doConvert = true;
} else if ( ! MAPNIK_VERSION ) {
MAPNIK_VERSION = arg;
}
else {
usage(me, 1);
}
}
if ( ! MAPNIK_VERSION ) {
MAPNIK_VERSION = mapnik.versions.mapnik;
}
console.log( (doConvert ? "Converting" : "Resetting" ) + ' all styles to target ' + MAPNIK_VERSION);
var REDIS_PORT = 6379; // TODO: make a command line parameter
var dbnum = 0;
var mml_store = new grainstore.MMLStore({port:REDIS_PORT}, {mapnik_version:MAPNIK_VERSION});
var failures = [];
var client = redis.createClient(REDIS_PORT, 'localhost');
client.on('connect', function() {
client.select(dbnum);
client.keys('map_style|*', function(err, matches) {
processNext = function() {
if ( ! matches.length ) process.exit(0);
if ( ! matches.length ) process.exit(failures.length);
var k = matches.shift();
console.log("Resetting XML in key: " + k);
client.get(k, function(err, val) {
if ( err ) throw err;
val = JSON.parse(val);
delete val.xml;
client.set(k, JSON.stringify(val), function() {
console.log("done with style " + k);
processNext();
});
if ( /map_style\|.*\|.*\|/.test(k) ) {
//console.warn("Key " + k + " is EXTENDED, skipping");
processNext();
}
var params = RegExp(/map_style\|(.*)\|(.*)/).exec(k);
var db = params[1];
var tab = params[2];
var out = 'map_style|' + db + '|' + tab + ': ';
var mml_builder = mml_store.mml_builder({dbname:db, table:tab},
function(err, payload) {
if ( err ) { console.warn(out + err.message); failures.push(k); processNext(); }
else {
mml_builder.resetStyle(function(err, data) {
if ( err ) { console.warn(out + err.message); failures.push(k); }
else console.log(out + 'OK' + ( doConvert ? ' (converted)' : '' ));
processNext();
}, doConvert);
}
});
}
};
processNext();
});
});