Refactoring, remove images.json from download API, updated hook callback with images database

pull/51/head
Piero Toffanin 2018-11-24 14:03:15 -05:00
rodzic 354bb9c591
commit 5dd99884e7
5 zmienionych plików z 76 dodań i 50 usunięć

Wyświetl plik

@ -3,7 +3,7 @@
[[_overview]]
== Overview
REST API to access OpenDroneMap
REST API to access ODM
=== Version information
@ -281,7 +281,7 @@ Retrieves an asset (the output of OpenDroneMap's processing) associated with a t
|===
|Type|Name|Description|Schema|Default
|*Path*|*asset* +
_required_|Type of asset to download. Use "all.zip" for zip file containing all assets.|enum (all.zip, orthophoto.tif, images.json)|
_required_|Type of asset to download. Use "all.zip" for zip file containing all assets.|enum (all.zip, orthophoto.tif)|
|*Path*|*uuid* +
_required_|UUID of the task|string|
|*Query*|*token* +

File diff suppressed because one or more lines are too long

Wyświetl plik

@ -419,7 +419,6 @@ app.get('/task/:uuid/output', authCheck, getTaskFromUuid, (req, res) => {
* enum:
* - all.zip
* - orthophoto.tif
* - images.json
* -
* name: token
* in: query

Wyświetl plik

@ -31,6 +31,7 @@ const archiver = require('archiver');
const Directories = require('./Directories');
const kill = require('tree-kill');
const S3 = require('./S3');
const request = require('request');
const statusCodes = require('./statusCodes');
@ -132,12 +133,6 @@ module.exports = class Task{
}else{
filename = path.join('odm_orthophoto', `odm_${filename}`);
}
}else if (filename == 'images.json'){
if (config.test){
filename = path.join('..', '..', 'processing_results', 'images.json');
}else{
// OK, do nothing
}
}else{
return false; // Invalid
}
@ -447,7 +442,66 @@ module.exports = class Task{
// Optionally starting from a certain line number
getOutput(startFromLine = 0){
return this.output.slice(startFromLine, this.output.length);
}
}
// Reads the contents of the tasks's
// images.json and returns its JSON representation
readImagesDatabase(callback){
const imagesDbPath = !config.test ?
path.join(this.getProjectFolderPath(), 'images.json') :
path.join('tests', 'processing_results', 'images.json');
fs.readFile(imagesDbPath, 'utf8', (err, data) => {
if (err) callback(err);
else{
try{
const json = JSON.parse(data);
callback(null, json);
}catch(e){
callback(e);
}
}
});
}
callWebhooks(){
// Hooks can be passed via command line
// or for each individual task
const hooks = [this.webhook, config.webhook];
this.readImagesDatabase((err, images) => {
if (err) logger.warn(err); // Continue with callback
if (!images) images = [];
hooks.forEach(hook => {
if (hook && hook.length > 3){
const notifyCallback = (attempt) => {
if (attempt > 5){
logger.warn(`Webhook invokation failed, will not retry: ${hook}`);
return;
}
request.post(hook, {
json: {
task: this.getInfo(),
images: images
}
},
(error, response) => {
if (error || response.statusCode != 200){
logger.warn(`Webhook invokation failed, will retry in a bit: ${hook}`);
setTimeout(() => {
notifyCallback(attempt + 1);
}, attempt * 5000);
}else{
logger.debug(`Webhook invoked: ${hook}`);
}
});
};
notifyCallback(0);
}
});
});
}
// Returns the data necessary to serialize this
// task to restore it later.

Wyświetl plik

@ -16,18 +16,17 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let assert = require('assert');
let config = require('../config');
let rmdir = require('rimraf');
let fs = require('fs');
let path = require('path');
let logger = require('./logger');
let Task = require('./Task');
let statusCodes = require('./statusCodes');
let async = require('async');
let schedule = require('node-schedule');
let Directories = require('./Directories');
let request = require('request');
const assert = require('assert');
const config = require('../config');
const rmdir = require('rimraf');
const fs = require('fs');
const path = require('path');
const logger = require('./logger');
const Task = require('./Task');
const statusCodes = require('./statusCodes');
const async = require('async');
const schedule = require('node-schedule');
const Directories = require('./Directories');
const TASKS_DUMP_FILE = path.join(Directories.data, "tasks.json");
const CLEANUP_TASKS_IF_OLDER_THAN = 1000 * 60 * config.cleanupTasksAfter; // minutes
@ -150,34 +149,8 @@ module.exports = class TaskManager{
if (task){
this.addToRunningQueue(task);
task.start(() => {
// Hooks can be passed via command line
// or for each individual task
const hooks = [task.webhook, config.webhook];
hooks.forEach(hook => {
if (hook && hook.length > 3){
const notifyCallback = (attempt) => {
if (attempt > 5){
logger.warn(`Callback failed, will not retry: ${hook}`);
return;
}
request.post(hook, {
json: task.getInfo()
},
(error, response) => {
if (error || response.statusCode != 200){
logger.warn(`Callback failed, will retry in a bit: ${hook}`);
setTimeout(() => {
notifyCallback(attempt + 1);
}, attempt * 5000);
}else{
logger.debug(`Callback invoked: ${hook}`);
}
});
};
notifyCallback(0);
}
});
task.callWebhooks();
this.removeFromRunningQueue(task);
this.processNextTask();