2016-07-19 22:20:53 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-20 01:56:12 +08:00
|
|
|
require('../../helper');
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var BATCH_SOURCE = '../../../lib/batch/';
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2016-07-20 01:56:12 +08:00
|
|
|
var assert = require('../../support/assert');
|
2019-10-04 00:24:39 +08:00
|
|
|
var redisUtils = require('../../support/redis-utils');
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2019-04-04 20:31:41 +08:00
|
|
|
var BatchLogger = require(BATCH_SOURCE + 'batch-logger');
|
2019-10-04 00:24:39 +08:00
|
|
|
var JobQueue = require(BATCH_SOURCE + 'job-queue');
|
|
|
|
var JobBackend = require(BATCH_SOURCE + 'job-backend');
|
2016-10-12 00:28:46 +08:00
|
|
|
var JobPublisher = require(BATCH_SOURCE + 'pubsub/job-publisher');
|
2019-10-04 00:24:39 +08:00
|
|
|
var jobStatus = require(BATCH_SOURCE + 'job-status');
|
|
|
|
var JobCanceller = require(BATCH_SOURCE + 'job-canceller');
|
|
|
|
var JobService = require(BATCH_SOURCE + 'job-service');
|
2016-07-19 22:20:53 +08:00
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
|
2016-10-17 21:02:34 +08:00
|
|
|
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
|
2019-04-04 20:31:41 +08:00
|
|
|
var logger = new BatchLogger(null, 'batch-queries');
|
2016-10-17 21:02:34 +08:00
|
|
|
var jobPublisher = new JobPublisher(redisUtils.getPool());
|
2019-12-24 01:19:08 +08:00
|
|
|
var jobQueue = new JobQueue(metadataBackend, jobPublisher, logger);
|
2019-04-04 20:31:41 +08:00
|
|
|
var jobBackend = new JobBackend(metadataBackend, jobQueue, logger);
|
2018-05-30 23:36:55 +08:00
|
|
|
var jobCanceller = new JobCanceller();
|
2016-07-19 22:20:53 +08:00
|
|
|
|
|
|
|
var USER = 'vizzuality';
|
|
|
|
var QUERY = 'select pg_sleep(0)';
|
|
|
|
var HOST = 'localhost';
|
|
|
|
var JOB = {
|
|
|
|
user: USER,
|
|
|
|
query: QUERY,
|
2018-05-30 23:36:55 +08:00
|
|
|
host: HOST,
|
|
|
|
dbname: 'cartodb_test_user_1_db',
|
|
|
|
dbuser: 'test_cartodb_user_1',
|
|
|
|
port: 5432,
|
2019-12-24 01:19:08 +08:00
|
|
|
pass: 'test_cartodb_user_1_pass'
|
2018-05-30 23:36:55 +08:00
|
|
|
|
2016-07-19 22:20:53 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function createWadusDataJob () {
|
2016-07-22 20:27:18 +08:00
|
|
|
return JSON.parse(JSON.stringify(JOB));
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:20:53 +08:00
|
|
|
// sets job to running, run its query and returns inmediatly (don't wait for query finishes)
|
|
|
|
// in order to test query cancelation/draining
|
2019-12-24 01:19:08 +08:00
|
|
|
function runQueryHelper (job, callback) {
|
2019-12-26 23:10:41 +08:00
|
|
|
var jobId = job.job_id;
|
2016-07-19 22:20:53 +08:00
|
|
|
var sql = job.query;
|
|
|
|
|
|
|
|
job.status = jobStatus.RUNNING;
|
|
|
|
|
|
|
|
jobBackend.update(job, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:28:59 +08:00
|
|
|
const dbConfiguration = {
|
|
|
|
host: job.host,
|
|
|
|
port: job.port,
|
|
|
|
dbname: job.dbname,
|
|
|
|
user: job.dbuser,
|
2019-12-24 01:19:08 +08:00
|
|
|
pass: job.pass
|
2018-06-04 17:28:59 +08:00
|
|
|
};
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2018-06-04 17:28:59 +08:00
|
|
|
var pg = new PSQL(dbConfiguration);
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2019-12-26 23:10:41 +08:00
|
|
|
sql = '/* ' + jobId + ' */ ' + sql;
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2018-06-04 17:28:59 +08:00
|
|
|
pg.eventedQuery(sql, function (err, query) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-07-19 22:20:53 +08:00
|
|
|
|
2018-06-04 17:28:59 +08:00
|
|
|
callback(null, query);
|
2016-07-19 22:20:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('job service', function () {
|
2016-07-19 22:20:53 +08:00
|
|
|
var jobService = new JobService(jobBackend, jobCanceller);
|
|
|
|
|
2016-07-22 20:27:18 +08:00
|
|
|
after(function (done) {
|
2016-07-22 23:05:01 +08:00
|
|
|
redisUtils.clean('batch:*', done);
|
2016-07-22 20:27:18 +08:00
|
|
|
});
|
|
|
|
|
2016-07-19 22:20:53 +08:00
|
|
|
it('.get() should return a job', function (done) {
|
2016-07-22 20:27:18 +08:00
|
|
|
jobService.create(createWadusDataJob(), function (err, jobCreated) {
|
2016-07-19 22:20:53 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobService.get(jobCreated.data.job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(job.data.job_id, jobCreated.data.job_id);
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.get() should return a not found error', function (done) {
|
|
|
|
jobService.get('wadus_job_id', function (err) {
|
|
|
|
assert.ok(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(err.message, 'Job with id wadus_job_id not found');
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.create() should persist a job', function (done) {
|
2016-07-22 20:27:18 +08:00
|
|
|
jobService.create(createWadusDataJob(), function (err, jobCreated) {
|
2016-07-19 22:20:53 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ok(jobCreated.data.job_id);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(jobCreated.data.status, jobStatus.PENDING);
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.create() should return error with invalid job data', function (done) {
|
2016-07-22 20:27:18 +08:00
|
|
|
var job = createWadusDataJob();
|
2016-07-19 22:20:53 +08:00
|
|
|
|
|
|
|
delete job.query;
|
|
|
|
|
|
|
|
jobService.create(job, function (err) {
|
|
|
|
assert.ok(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(err.message, 'You must indicate a valid SQL');
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.cancel() should cancel a running job', function (done) {
|
2016-07-22 20:27:18 +08:00
|
|
|
var job = createWadusDataJob();
|
|
|
|
job.query = 'select pg_sleep(3)';
|
2016-07-19 22:20:53 +08:00
|
|
|
|
|
|
|
jobService.create(job, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
runQueryHelper(job.data, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobService.cancel(job.data.job_id, function (err, jobCancelled) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(jobCancelled.data.job_id, job.data.job_id);
|
|
|
|
assert.strictEqual(jobCancelled.data.status, jobStatus.CANCELLED);
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.cancel() should return a job not found error', function (done) {
|
|
|
|
jobService.cancel('wadus_job_id', function (err) {
|
|
|
|
assert.ok(err, err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(err.name, 'NotFoundError');
|
|
|
|
assert.strictEqual(err.message, 'Job with id wadus_job_id not found');
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.drain() should draing a running job', function (done) {
|
2016-07-22 20:27:18 +08:00
|
|
|
var job = createWadusDataJob();
|
|
|
|
job.query = 'select pg_sleep(3)';
|
2016-07-19 22:20:53 +08:00
|
|
|
|
|
|
|
jobService.create(job, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
runQueryHelper(job.data, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobService.drain(job.data.job_id, function (err, jobDrained) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(jobDrained.job_id, job.data.job_id);
|
|
|
|
assert.strictEqual(jobDrained.status, jobStatus.PENDING);
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('.drain() should return a job not found error', function (done) {
|
|
|
|
jobService.drain('wadus_job_id', function (err) {
|
|
|
|
assert.ok(err, err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(err.name, 'NotFoundError');
|
|
|
|
assert.strictEqual(err.message, 'Job with id wadus_job_id not found');
|
2016-07-19 22:20:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|