From 2cf31935bcb9d18e7e2ecf635767f4beeb828774 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Thu, 9 Jul 2015 22:22:04 +0300 Subject: [PATCH] Return task object including fault details in case of fault. Return reason for connection request failure. --- lib/api-functions.coffee | 12 ++++++++---- lib/nbi.coffee | 27 +++++++++++++++++++++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/api-functions.coffee b/lib/api-functions.coffee index ef68b2a..ba34e1e 100644 --- a/lib/api-functions.coffee +++ b/lib/api-functions.coffee @@ -130,15 +130,19 @@ connectionRequest = (deviceId, callback) -> watchTask = (taskId, timeout, callback) -> setTimeout( () -> - db.tasksCollection.findOne({_id : taskId}, {'_id' : 1}, (err, task) -> + db.tasksCollection.findOne({_id : taskId}, {'_id' : 1, 'fault' : 1}, (err, task) -> + return callback(err) if err + if task timeout -= 500 - if timeout < 0 - callback(new Error('Timeout')) + if task.fault? + callback(null, 'fault') + else if timeout <= 0 + callback(null, 'timeout') else watchTask(taskId, timeout, callback) else - callback(err) + callback(null, 'completed') ) , 500) diff --git a/lib/nbi.coffee b/lib/nbi.coffee index 11588c5..9c50462 100644 --- a/lib/nbi.coffee +++ b/lib/nbi.coffee @@ -200,16 +200,31 @@ listener = (request, response) -> if urlParts.query.connection_request? apiFunctions.connectionRequest(deviceId, (err) -> if err - response.writeHead(202, {'Content-Type' : 'application/json'}) + response.writeHead(202, err.message, {'Content-Type' : 'application/json'}) response.end(JSON.stringify(task)) else - apiFunctions.watchTask(task._id, config.get('DEVICE_ONLINE_THRESHOLD', deviceId), (err) -> + apiFunctions.watchTask(task._id, config.get('DEVICE_ONLINE_THRESHOLD', deviceId), (err, status) -> if err - response.writeHead(202, {'Content-Type' : 'application/json'}) - response.end(JSON.stringify(task)) + response.writeHead(500) + response.end(errorToString(err)) return - response.writeHead(200, {'Content-Type' : 'application/json'}) - response.end(JSON.stringify(task)) + + if status is 'timeout' + response.writeHead(202, 'Task queued but not processed', {'Content-Type' : 'application/json'}) + response.end(JSON.stringify(task)) + else if status is 'fault' + db.tasksCollection.findOne({_id : task._id}, (err, task) -> + if err + response.writeHead(500) + response.end(errorToString(err)) + return + + response.writeHead(202, 'Task faulted', {'Content-Type' : 'application/json'}) + response.end(JSON.stringify(task)) + ) + else + response.writeHead(200, {'Content-Type' : 'application/json'}) + response.end(JSON.stringify(task)) ) ) else