Add <%= job_id %> template support for onerror and onsuccess fallback queries

This commit is contained in:
Raul Ochoa 2016-06-30 17:41:02 +02:00
parent ebe30b108c
commit be0f059f01
5 changed files with 80 additions and 9 deletions

View File

@ -1,3 +1,10 @@
1.33.0 - 2016-mm-dd
-------------------
New features:
* Add `<%= job_id %>` template support for onerror and onsuccess fallback queries.
1.32.0 - 2016-06-30 1.32.0 - 2016-06-30
------------------- -------------------

View File

@ -30,7 +30,11 @@ Fallback.prototype.getNextQuery = function (job) {
Fallback.prototype.getOnSuccess = function (job) { Fallback.prototype.getOnSuccess = function (job) {
if (job.query.query[this.index].status === jobStatus.DONE && if (job.query.query[this.index].status === jobStatus.DONE &&
job.query.query[this.index].fallback_status === jobStatus.PENDING) { job.query.query[this.index].fallback_status === jobStatus.PENDING) {
return job.query.query[this.index].onsuccess; var onsuccessQuery = job.query.query[this.index].onsuccess;
if (onsuccessQuery) {
onsuccessQuery = onsuccessQuery.replace(/<%=\s*job_id\s*%>/g, job.job_id);
}
return onsuccessQuery;
} }
}; };
@ -43,6 +47,7 @@ Fallback.prototype.getOnError = function (job) {
job.query.query[this.index].fallback_status === jobStatus.PENDING) { job.query.query[this.index].fallback_status === jobStatus.PENDING) {
var onerrorQuery = job.query.query[this.index].onerror; var onerrorQuery = job.query.query[this.index].onerror;
if (onerrorQuery) { if (onerrorQuery) {
onerrorQuery = onerrorQuery.replace(/<%=\s*job_id\s*%>/g, job.job_id);
onerrorQuery = onerrorQuery.replace(/<%=\s*error_message\s*%>/g, job.query.query[this.index].failed_reason); onerrorQuery = onerrorQuery.replace(/<%=\s*error_message\s*%>/g, job.query.query[this.index].failed_reason);
} }
return onerrorQuery; return onerrorQuery;

2
npm-shrinkwrap.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "cartodb_sql_api", "name": "cartodb_sql_api",
"version": "1.31.1", "version": "1.33.0",
"dependencies": { "dependencies": {
"cartodb-psql": { "cartodb-psql": {
"version": "0.6.1", "version": "0.6.1",

View File

@ -5,7 +5,7 @@
"keywords": [ "keywords": [
"cartodb" "cartodb"
], ],
"version": "1.32.0", "version": "1.33.0",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/CartoDB/CartoDB-SQL-API.git" "url": "git://github.com/CartoDB/CartoDB-SQL-API.git"

View File

@ -13,7 +13,7 @@ var metadataBackend = require('cartodb-redis')({
var batchFactory = require('../../batch'); var batchFactory = require('../../batch');
var jobStatus = require('../../batch/job_status'); var jobStatus = require('../../batch/job_status');
describe('Batch API query timing', function () { describe('Batch API callback templates', function () {
function createJob(jobDefinition, callback) { function createJob(jobDefinition, callback) {
assert.response(app, { assert.response(app, {
@ -100,18 +100,18 @@ describe('Batch API query timing', function () {
}); });
}); });
describe('should report start and end time for each query with fallback queries', function () { describe('should use templates for error_message and job_id onerror callback', function () {
var jobResponse; var jobResponse;
before(function(done) { before(function(done) {
createJob({ createJob({
"query": { "query": {
"query": [ "query": [
{ {
"query": "create table batch_errors (error_message text)" "query": "create table batch_errors (job_id text, error_message text)"
}, },
{ {
"query": "SELECT * FROM invalid_table", "query": "SELECT * FROM invalid_table",
"onerror": "INSERT INTO batch_errors values ('<%= error_message %>')" "onerror": "INSERT INTO batch_errors values ('<%= job_id %>', '<%= error_message %>')"
} }
] ]
} }
@ -125,12 +125,12 @@ describe('Batch API query timing', function () {
var expectedQuery = { var expectedQuery = {
query: [ query: [
{ {
"query": "create table batch_errors (error_message text)", "query": "create table batch_errors (job_id text, error_message text)",
status: 'done' status: 'done'
}, },
{ {
"query": "SELECT * FROM invalid_table", "query": "SELECT * FROM invalid_table",
"onerror": "INSERT INTO batch_errors values ('<%= error_message %>')", "onerror": "INSERT INTO batch_errors values ('<%= job_id %>', '<%= error_message %>')",
status: 'failed', status: 'failed',
fallback_status: 'done' fallback_status: 'done'
} }
@ -146,6 +146,7 @@ describe('Batch API query timing', function () {
if (err) { if (err) {
return done(err); return done(err);
} }
assert.equal(result.rows[0].job_id, jobResponse.job_id);
assert.equal(result.rows[0].error_message, 'relation "invalid_table" does not exist'); assert.equal(result.rows[0].error_message, 'relation "invalid_table" does not exist');
getQueryResult('drop table batch_errors', done); getQueryResult('drop table batch_errors', done);
}); });
@ -158,4 +159,62 @@ describe('Batch API query timing', function () {
}); });
}); });
describe('should use template for job_id onsuccess callback', function () {
var jobResponse;
before(function(done) {
createJob({
"query": {
"query": [
{
query: "create table batch_jobs (job_id text)"
},
{
"query": "SELECT 1",
"onsuccess": "INSERT INTO batch_jobs values ('<%= job_id %>')"
}
]
}
}, function(err, job) {
jobResponse = job;
return done(err);
});
});
it('should keep the original templated query but use the job_id', function (done) {
var expectedQuery = {
query: [
{
query: "create table batch_jobs (job_id text)",
status: 'done'
},
{
query: "SELECT 1",
onsuccess: "INSERT INTO batch_jobs values ('<%= job_id %>')",
status: 'done',
fallback_status: 'done'
}
]
};
var interval = setInterval(function () {
getJobStatus(jobResponse.job_id, function(err, job) {
if (job.status === jobStatus.DONE) {
clearInterval(interval);
validateExpectedResponse(job.query, expectedQuery);
getQueryResult('select * from batch_jobs', function(err, result) {
if (err) {
return done(err);
}
assert.equal(result.rows[0].job_id, jobResponse.job_id);
getQueryResult('drop table batch_jobs', done);
});
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
clearInterval(interval);
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be "done"'));
}
});
}, 50);
});
});
}); });