2016-10-10 18:01:36 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-10 21:22:50 +08:00
|
|
|
require('../helper');
|
|
|
|
var assert = require('assert');
|
|
|
|
var appServer = require('../../app/server');
|
|
|
|
var redisUtils = require('./redis_utils');
|
2016-10-10 18:01:36 +08:00
|
|
|
var debug = require('debug')('batch-test-client');
|
|
|
|
|
2016-10-10 21:22:50 +08:00
|
|
|
var JobStatus = require('../../batch/job_status');
|
2016-10-10 18:01:36 +08:00
|
|
|
var metadataBackend = require('cartodb-redis')(redisUtils.getConfig());
|
2016-10-10 21:22:50 +08:00
|
|
|
var batchFactory = require('../../batch/index');
|
2016-10-10 18:01:36 +08:00
|
|
|
|
|
|
|
function response(code) {
|
|
|
|
return {
|
|
|
|
status: code
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var RESPONSE = {
|
|
|
|
OK: response(200),
|
|
|
|
CREATED: response(201)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function BatchTestClient(config) {
|
|
|
|
this.config = config || {};
|
|
|
|
this.server = appServer();
|
|
|
|
|
2016-10-11 01:46:07 +08:00
|
|
|
this.batch = batchFactory(metadataBackend, redisUtils.getConfig(), this.config.name);
|
2016-10-10 18:01:36 +08:00
|
|
|
this.batch.start();
|
|
|
|
|
|
|
|
this.pendingJobs = [];
|
|
|
|
this.ready = false;
|
|
|
|
this.batch.on('ready', function() {
|
|
|
|
this.ready = true;
|
|
|
|
this.pendingJobs.forEach(function(pendingJob) {
|
|
|
|
this.createJob(pendingJob.job, pendingJob.callback);
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BatchTestClient;
|
|
|
|
|
|
|
|
BatchTestClient.prototype.isReady = function() {
|
|
|
|
return this.ready;
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.createJob = function(job, override, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
2016-10-10 18:01:36 +08:00
|
|
|
if (!this.isReady()) {
|
|
|
|
this.pendingJobs.push({
|
|
|
|
job: job,
|
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
return debug('Waiting for Batch service to be ready');
|
|
|
|
}
|
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-10-17 16:52:35 +08:00
|
|
|
url: this.getUrl(override),
|
2016-10-10 18:01:36 +08:00
|
|
|
headers: {
|
2016-10-17 16:52:35 +08:00
|
|
|
host: this.getHost(override),
|
2016-10-10 18:01:36 +08:00
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: JSON.stringify(job)
|
|
|
|
},
|
|
|
|
RESPONSE.CREATED,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-10-17 16:52:35 +08:00
|
|
|
return callback(null, new JobResult(JSON.parse(res.body), this, override));
|
2016-10-10 18:01:36 +08:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getJobStatus = function(jobId, override, callback) {
|
2016-10-10 18:01:36 +08:00
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-10-17 16:52:35 +08:00
|
|
|
url: this.getUrl(override, jobId),
|
2016-10-10 18:01:36 +08:00
|
|
|
headers: {
|
2016-10-17 16:52:35 +08:00
|
|
|
host: this.getHost(override)
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
RESPONSE.OK,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, JSON.parse(res.body));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.cancelJob = function(jobId, override, callback) {
|
2016-10-10 18:01:36 +08:00
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
|
|
|
url: this.getUrl(jobId),
|
|
|
|
headers: {
|
2016-10-17 16:52:35 +08:00
|
|
|
host: this.getHost(override)
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
|
|
|
method: 'DELETE'
|
|
|
|
},
|
|
|
|
RESPONSE.OK,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, JSON.parse(res.body));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
BatchTestClient.prototype.drain = function(callback) {
|
2016-10-12 22:43:18 +08:00
|
|
|
this.batch.stop(function() {
|
|
|
|
return redisUtils.clean('batch:*', callback);
|
|
|
|
});
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getHost = function(override) {
|
|
|
|
return override.host || this.config.host || 'vizzuality.cartodb.com';
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getUrl = function(override, jobId) {
|
2016-10-10 18:01:36 +08:00
|
|
|
var urlParts = ['/api/v2/sql/job'];
|
|
|
|
if (jobId) {
|
|
|
|
urlParts.push(jobId);
|
|
|
|
}
|
2016-10-17 16:52:35 +08:00
|
|
|
return urlParts.join('/') + '?api_key=' + this.getApiKey(override);
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getApiKey = function(override) {
|
|
|
|
return override.apiKey || this.config.apiKey || '1234';
|
|
|
|
};
|
2016-10-10 18:01:36 +08:00
|
|
|
|
|
|
|
/****************** JobResult ******************/
|
|
|
|
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
function JobResult(job, batchTestClient, override) {
|
2016-10-10 18:01:36 +08:00
|
|
|
this.job = job;
|
|
|
|
this.batchTestClient = batchTestClient;
|
2016-10-17 16:52:35 +08:00
|
|
|
this.override = override;
|
2016-10-10 18:01:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JobResult.prototype.getStatus = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
var interval = setInterval(function () {
|
2016-10-17 16:52:35 +08:00
|
|
|
self.batchTestClient.getJobStatus(self.job.job_id, self.override, function (err, job) {
|
2016-10-10 18:01:36 +08:00
|
|
|
if (err) {
|
|
|
|
clearInterval(interval);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JobStatus.isFinal(job.status)) {
|
|
|
|
clearInterval(interval);
|
|
|
|
return callback(null, job);
|
|
|
|
} else {
|
|
|
|
debug('Job %s [status=%s] waiting to be done', self.job.job_id, job.status);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 50);
|
|
|
|
};
|
|
|
|
|
|
|
|
JobResult.prototype.cancel = function(callback) {
|
2016-10-17 16:52:35 +08:00
|
|
|
this.batchTestClient.cancelJob(this.job.job_id, this.override, callback);
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|