Skip to content

Commit 9d87b7d

Browse files
committed
Implemented /task/list
1 parent 9ece134 commit 9d87b7d

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

Diff for: libs/asrProvider.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ module.exports = {
7676
// Attempt to retrieve task info and save it in task table before deleting node
7777
// so that users can continue to access this information.
7878
try{
79-
await tasktable.add(taskId, {taskInfo: await node.taskInfo(taskId)});
79+
const route = await routetable.lookup(taskId);
80+
if (route){
81+
await tasktable.add(taskId, {taskInfo: await node.taskInfo(taskId)}, route.token);
82+
}else{
83+
logger.warn(`Cannot add task table entry for ${taskId} (route missing)`);
84+
}
8085
}catch(e){
8186
logger.warn(`Cannot add task table entry for ${taskId} from ${node}`);
8287
}

Diff for: libs/proxy.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ module.exports = {
402402

403403
if (pathname === '/task/cancel'){
404404
taskTableEntry.taskInfo.status.code = statusCodes.CANCELED;
405-
await tasktable.add(taskId, taskTableEntry);
405+
await tasktable.add(taskId, taskTableEntry, query.token);
406406
}
407407

408408
json(res, { success: true });
@@ -419,6 +419,19 @@ module.exports = {
419419
});
420420

421421
utils.stringToStream(body).pipe(busboy);
422+
}else if (req.method === 'GET' && pathname === '/task/list') {
423+
const taskIds = {};
424+
const taskTableEntries = await tasktable.findByToken(query.token);
425+
for (let taskId in taskTableEntries){
426+
taskIds[taskId] = true;
427+
}
428+
429+
const routeTableEntries = await routetable.findByToken(query.token, true);
430+
for (let taskId in routeTableEntries){
431+
taskIds[taskId] = true;
432+
}
433+
434+
json(res, Object.keys(taskIds).map(uuid => { return { uuid } }));
422435
}else{
423436
// Lookup task id
424437
const matches = pathname.match(/^\/task\/([\w\d]+\-[\w\d]+\-[\w\d]+\-[\w\d]+\-[\w\d]+)\/(.+)$/);

Diff for: libs/routetable.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818
const fs = require('fs');
19+
const async = require("async");
1920
const logger = require('./logger');
2021
const nodes = require('./nodes');
2122

@@ -95,14 +96,27 @@ module.exports = {
9596
}
9697
},
9798

98-
findByToken: async function(token){
99+
findByToken: async function(token, activeOnly = false){
99100
const result = {};
100101
for (let taskId in routes){
101102
if (routes[taskId].token === token){
102103
result[taskId] = routes[taskId];
103104
}
104105
}
105-
return result;
106+
if (!activeOnly) return result;
107+
108+
// Actually ping the node for these tasks and filter out
109+
// inactive / deleted / stale ones
110+
return new Promise((resolve) => {
111+
async.each(Object.keys(result), (taskId, cb) => {
112+
(routes[taskId]).node.taskInfo(taskId).then((taskInfo) => {
113+
if (taskInfo.error) delete(result[taskId]);
114+
cb();
115+
});
116+
}, () => {
117+
resolve(result);
118+
});
119+
});
106120
},
107121

108122
lookupNode: async function(taskId){

Diff for: libs/taskNew.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ module.exports = {
453453
const taskInfo = taskTableEntry.taskInfo;
454454
if (taskInfo){
455455
taskInfo.status.code = statusCodes.FAILED;
456-
await tasktable.add(uuid, { taskInfo, output: [err.message] });
456+
await tasktable.add(uuid, { taskInfo, output: [err.message] }, token);
457457
logger.warn(`Cannot forward task ${uuid} to processing node ${node}: ${err.message}`);
458458
}
459459
}
@@ -491,7 +491,7 @@ module.exports = {
491491
};
492492

493493
// Add item to task table
494-
await tasktable.add(uuid, { taskInfo, abort: abortTask, output: ["Launching... please wait! This can take a few minutes."] });
494+
await tasktable.add(uuid, { taskInfo, abort: abortTask, output: ["Launching... please wait! This can take a few minutes."] }, token);
495495

496496
// Send back response to user right away
497497
utils.json(res, { uuid });

Diff for: libs/tasktable.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ module.exports = {
4343
setInterval(cleanup, 1000 * 60 * 60);
4444
},
4545

46-
add: async function(taskId, obj){
46+
add: async function(taskId, obj, token){
4747
if (!taskId) throw new Error("taskId is not valid");
4848
if (!obj) throw new Error("obj is not valid");
4949

5050
logger.debug(`Added ${taskId} --> ${JSON.stringify(obj)} in task table`);
5151

5252
tasks[taskId] = {
53-
obj: obj,
54-
accessed: new Date().getTime()
53+
obj,
54+
token,
55+
accessed: new Date().getTime(),
5556
};
5657
},
5758

@@ -67,5 +68,15 @@ module.exports = {
6768
}
6869

6970
return null;
71+
},
72+
73+
findByToken: async function(token){
74+
const result = {};
75+
for (let taskId in tasks){
76+
if (tasks[taskId].token === token){
77+
result[taskId] = tasks[taskId].obj;
78+
}
79+
}
80+
return result;
7081
}
7182
};

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ClusterODM",
3-
"version": "1.3.7",
3+
"version": "1.4.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)