Improved taskNew creation by async initialization

pull/153/head
Piero Toffanin 2021-05-24 17:02:49 -04:00
rodzic 30e1038339
commit 18a714b2de
3 zmienionych plików z 193 dodań i 167 usunięć

Wyświetl plik

@ -38,9 +38,8 @@ const archiver = require('archiver');
const statusCodes = require('./statusCodes'); const statusCodes = require('./statusCodes');
module.exports = class Task{ module.exports = class Task{
constructor(uuid, name, options = [], webhook = null, skipPostProcessing = false, outputs = [], dateCreated = new Date().getTime(), done = () => {}){ constructor(uuid, name, options = [], webhook = null, skipPostProcessing = false, outputs = [], dateCreated = new Date().getTime(), imagesCountEstimate = -1){
assert(uuid !== undefined, "uuid must be set"); assert(uuid !== undefined, "uuid must be set");
assert(done !== undefined, "ready must be set");
this.uuid = uuid; this.uuid = uuid;
this.name = name !== "" ? name : "Task of " + (new Date()).toISOString(); this.name = name !== "" ? name : "Task of " + (new Date()).toISOString();
@ -58,14 +57,18 @@ module.exports = class Task{
this.skipPostProcessing = skipPostProcessing; this.skipPostProcessing = skipPostProcessing;
this.outputs = utils.parseUnsafePathsList(outputs); this.outputs = utils.parseUnsafePathsList(outputs);
this.progress = 0; this.progress = 0;
this.imagesCountEstimate = imagesCountEstimate;
this.initialized = false;
}
async.series([ initialize(done, additionalSteps = []){
async.series(additionalSteps.concat([
// Handle post-processing options logic // Handle post-processing options logic
cb => { cb => {
// If we need to post process results // If we need to post process results
// if pc-ept is supported (build entwine point cloud) // if pc-ept is supported (build entwine point cloud)
// we automatically add the pc-ept option to the task options by default // we automatically add the pc-ept option to the task options by default
if (skipPostProcessing) cb(); if (this.skipPostProcessing) cb();
else{ else{
odmInfo.supportsOption("pc-ept", (err, supported) => { odmInfo.supportsOption("pc-ept", (err, supported) => {
if (err){ if (err){
@ -113,20 +116,22 @@ module.exports = class Task{
} }
}); });
} }
], err => { ]), err => {
this.initialized = true;
done(err, this); done(err, this);
}); });
} }
static CreateFromSerialized(taskJson, done){ static CreateFromSerialized(taskJson, done){
new Task(taskJson.uuid, const task = new Task(taskJson.uuid,
taskJson.name, taskJson.name,
taskJson.options, taskJson.options,
taskJson.webhook, taskJson.webhook,
taskJson.skipPostProcessing, taskJson.skipPostProcessing,
taskJson.outputs, taskJson.outputs,
taskJson.dateCreated, taskJson.dateCreated);
(err, task) => {
task.initialize((err, task) => {
if (err) done(err); if (err) done(err);
else{ else{
// Override default values with those // Override default values with those
@ -578,7 +583,7 @@ module.exports = class Task{
// Re-executes the task (by setting it's state back to QUEUED) // Re-executes the task (by setting it's state back to QUEUED)
// Only tasks that have been canceled, completed or have failed can be restarted. // Only tasks that have been canceled, completed or have failed can be restarted.
restart(options, cb){ restart(options, cb){
if ([statusCodes.CANCELED, statusCodes.FAILED, statusCodes.COMPLETED].indexOf(this.status.code) !== -1){ if ([statusCodes.CANCELED, statusCodes.FAILED, statusCodes.COMPLETED].indexOf(this.status.code) !== -1 && this.initialized){
this.setStatus(statusCodes.QUEUED); this.setStatus(statusCodes.QUEUED);
this.dateCreated = new Date().getTime(); this.dateCreated = new Date().getTime();
this.dateStarted = 0; this.dateStarted = 0;
@ -601,7 +606,7 @@ module.exports = class Task{
processingTime: this.processingTime, processingTime: this.processingTime,
status: this.status, status: this.status,
options: this.options, options: this.options,
imagesCount: this.images.length, imagesCount: this.images !== undefined ? this.images.length : this.imagesCountEstimate,
progress: this.progress progress: this.progress
}; };
} }

Wyświetl plik

@ -184,7 +184,7 @@ class TaskManager{
// Finds the first QUEUED task. // Finds the first QUEUED task.
findNextTaskToProcess(){ findNextTaskToProcess(){
for (let uuid in this.tasks){ for (let uuid in this.tasks){
if (this.tasks[uuid].getStatus() === statusCodes.QUEUED){ if (this.tasks[uuid].getStatus() === statusCodes.QUEUED && this.tasks[uuid].initialized){
return this.tasks[uuid]; return this.tasks[uuid];
} }
} }

Wyświetl plik

@ -30,7 +30,7 @@ const async = require('async');
const odmInfo = require('./odmInfo'); const odmInfo = require('./odmInfo');
const request = require('request'); const request = require('request');
const ziputils = require('./ziputils'); const ziputils = require('./ziputils');
const { cancelJob } = require('node-schedule'); const statusCodes = require('./statusCodes');
const download = function(uri, filename, callback) { const download = function(uri, filename, callback) {
request.head(uri, function(err, res, body) { request.head(uri, function(err, res, body) {
@ -224,10 +224,6 @@ module.exports = {
}, },
createTask: (req, res) => { createTask: (req, res) => {
// IMPROVEMENT: consider doing the file moving in the background
// and return a response more quickly instead of a long timeout.
req.setTimeout(1000 * 60 * 20);
const srcPath = path.join("tmp", req.id); const srcPath = path.join("tmp", req.id);
// Print error message and cleanup // Print error message and cleanup
@ -236,9 +232,6 @@ module.exports = {
removeDirectory(srcPath); removeDirectory(srcPath);
}; };
if (req.error !== undefined){
die(req.error);
}else{
let destPath = path.join(Directories.data, req.id); let destPath = path.join(Directories.data, req.id);
let destImagesPath = path.join(destPath, "images"); let destImagesPath = path.join(destPath, "images");
let destGcpPath = path.join(destPath, "gcp"); let destGcpPath = path.join(destPath, "gcp");
@ -254,17 +247,7 @@ module.exports = {
} }
}; };
async.series([ let initSteps = [
cb => {
odmInfo.filterOptions(req.body.options, (err, options) => {
if (err) cb(err);
else {
req.body.options = options;
cb(null);
}
});
},
// Check if dest directory already exists // Check if dest directory already exists
cb => { cb => {
if (req.files && req.files.length > 0) { if (req.files && req.files.length > 0) {
@ -375,23 +358,61 @@ module.exports = {
}, cb); }, cb);
} }
}); });
}, }
];
// Create task if (req.error !== undefined){
die(req.error);
}else{
let imagesCountEstimate = -1;
async.series([
cb => { cb => {
new Task(req.id, req.body.name, req.body.options, // Basic path check
fs.exists(srcPath, exists => {
if (exists) cb();
else cb(new Error(`Invalid UUID`));
});
},
cb => {
odmInfo.filterOptions(req.body.options, (err, options) => {
if (err) cb(err);
else {
req.body.options = options;
cb(null);
}
});
},
cb => {
fs.readdir(srcPath, (err, entries) => {
if (!err) imagesCountEstimate = entries.length;
cb();
});
},
cb => {
const task = new Task(req.id, req.body.name, req.body.options,
req.body.webhook, req.body.webhook,
req.body.skipPostProcessing === 'true', req.body.skipPostProcessing === 'true',
req.body.outputs, req.body.outputs,
req.body.dateCreated, req.body.dateCreated,
(err, task) => { imagesCountEstimate
if (err) cb(err); );
else {
TaskManager.singleton().addNew(task); TaskManager.singleton().addNew(task);
res.json({ uuid: req.id }); res.json({ uuid: req.id });
cb(); cb();
}
}); // We return a UUID right away but continue
// doing processing in the background
task.initialize(err => {
if (err) {
task.setStatus(statusCodes.FAILED, { errorMessage: err.message });
// Cleanup
removeDirectory(srcPath);
removeDirectory(destPath);
} else TaskManager.singleton().processNextTask();
}, initSteps);
} }
], err => { ], err => {
if (err) die(err.message); if (err) die(err.message);