Make afterLayergroupCreate function as a 'middleware' builder

This commit is contained in:
Daniel García Aubert
2017-10-31 17:29:55 +01:00
parent bb02494e02
commit d85a5d83b7

View File

@@ -184,7 +184,9 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
res.locals.analysesResults = context.analysesResults;
res.locals.layergroup = layergroup;
self.afterLayergroupCreate(req, res, this);
const afterLayergroupCreate = self.afterLayergroupCreateBuilder();
afterLayergroupCreate(req, res, this);
},
function finish(err) {
if (err) {
@@ -284,7 +286,9 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
res.locals.analysesResults = mapConfigProvider.analysesResults;
res.locals.layergroup = layergroup;
self.afterLayergroupCreate(req, res, this);
const afterLayergroupCreate = self.afterLayergroupCreateBuilder();
afterLayergroupCreate(req, res, this);
},
function finishTemplateInstantiation(err) {
if (err) {
@@ -315,62 +319,58 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
);
};
MapController.prototype.afterLayergroupCreate = function (req, res, callback) {
MapController.prototype.afterLayergroupCreateBuilder = function () {
var self = this;
return function (req, res, callback) {
step(
function incrementMapViewCount () {
self.incrementMapViewCount(req, res, this);
},
function augmentLayergroupData (err) {
assert.ifError(err);
self.augmentLayergroupData(req, res, this);
},
function getAffectedTables (err) {
assert.ifError(err);
self.getAffectedTables(req, res, this);
},
function setCacheChannel (err) {
assert.ifError(err);
self.setCacheChannel(req, res, this);
},
function setLastUpdatedTime (err) {
assert.ifError(err);
self.setLastUpdatedTimeToLayergroup(req, res, this);
},
function setCacheControl (err) {
assert.ifError(err);
self.setCacheControl(req, res, this);
},
function setLayerStats (err) {
assert.ifError(err);
self.setLayerStats(req, res, this);
},
function finish(err) {
callback(err);
}
);
};
};
MapController.prototype.incrementMapViewCount = function (req, res, callback) {
const { mapconfig, user } = res.locals;
var tasksleft = 2; // redis key and affectedTables
var errors = [];
var done = function(err) {
if ( err ) {
errors.push('' + err);
}
if ( ! --tasksleft ) {
err = errors.length ? new Error(errors.join('\n')) : null;
callback(err);
}
};
// Perform mapview count increment in parallel along the rest of after-layergroup-create
// tasks. Error won't blow up, just be logged.
this.metadataBackend.incMapviewCount(user, mapconfig.obj().stat_tag, function(err) {
// Error won't blow up, just be logged.
this.metadataBackend.incMapviewCount(user, mapconfig.obj().stat_tag, (err) => {
req.profiler.done('incMapviewCount');
if ( err ) {
global.logger.log("ERROR: failed to increment mapview count for user '" + user + "': " + err);
if (err) {
global.logger.log(`ERROR: failed to increment mapview count for user '${user}': ${err.message}`);
}
done();
callback();
});
step(
function () {
self.augmentLayergroupData(req, res, this);
},
function getAffectedTables (err) {
assert.ifError(err);
self.getAffectedTables(req, res, this);
},
function setCacheChannel (err) {
assert.ifError(err);
self.setCacheChannel(req, res, this);
},
function setLastUpdatedTime (err) {
assert.ifError(err);
self.setLastUpdatedTimeToLayergroup(req, res, this);
},
function setCacheControl (err) {
assert.ifError(err);
self.setCacheControl(req, res, this);
},
function setLayerStats (err) {
assert.ifError(err);
self.setLayerStats(req, res, this);
},
function finish(err) {
done(err);
}
);
};
MapController.prototype.augmentLayergroupData = function (req, res, callback) {