From c809d6c35ca029703e5a54a02652f127a81ed909 Mon Sep 17 00:00:00 2001 From: Cassio Cabral Date: Wed, 21 Oct 2015 12:12:34 -0300 Subject: [PATCH] fix postRequest and add url to test --- endpoints/legenda.js | 2 +- endpoints/videornp.js | 61 ++++++++++++++++++++++++++++++++++++++++++------------------- helpers/core.js | 17 +++++++++++++---- helpers/requests.js | 8 ++++---- logsystem/errors.log | 2 +- server.js | 5 +++++ 6 files changed, 66 insertions(+), 29 deletions(-) diff --git a/endpoints/legenda.js b/endpoints/legenda.js index 984b8a7..4df8dd4 100644 --- a/endpoints/legenda.js +++ b/endpoints/legenda.js @@ -28,7 +28,7 @@ function init(req, res) { } /* Checa se o arquivo de legenda submetivo possui uma extensão válida */ - if (parameters.checkSubtitle(req.files.legenda.name) == false) { + if (parameters.checkSubtitle(req.files.legenda.name) === false) { res.send(500, parameters.errorMessage('Legenda com Extensão Inválida')); return; } diff --git a/endpoints/videornp.js b/endpoints/videornp.js index d7df323..a3ed614 100644 --- a/endpoints/videornp.js +++ b/endpoints/videornp.js @@ -12,6 +12,11 @@ var _ = require('lodash'); var kue = require('kue'), queue = kue.createQueue(); var logger = require('../logsystem/main.js'); +var url = require('url'); +var requests = require('../helpers/requests'); +var querystring = require('querystring'); + + function init(req, res, Request) { @@ -183,7 +188,7 @@ function callCoreSubtitle(id, subtitle, req, res, Request, request_object) { } var command_line = 'vlibras_user/vlibras-core/./vlibras -S ' + ' uploads/' + id + '/' + - legenda_name + ' -l portugues -b opaco --id' + id + ' --mode devel > /tmp/core_log 2>&1'; + legenda_name + ' -l portugues -b opaco --id ' + id + ' --mode devel > /tmp/core_log 2>&1'; var child; @@ -203,26 +208,44 @@ function callCoreSubtitle(id, subtitle, req, res, Request, request_object) { // console.log(stdout); }); + var data, path; + // Endereço do callback + if (req.body.callback !== undefined) { + path = url.parse(req.body.callback); + } + /* Listener que dispara quando a requisição ao core finaliza */ child.on('close', function(code, signal){ - - // res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.flv' }); - - // Se o core executou com erro - if (code !== 0) { - db.update(Request, request_object.id, 'Error', function (result) {}); - console.log("Erro no retorno do core. Código: " + code); - logger.incrementError('core', "Erro no retorno do core. Código: " + code); - } else { - // Se o core executou normal - db.update(Request, request_object.id, 'Completed', function (result) {}); - res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4'}); - logger.incrementService("videos", "traducoes"); - } - }); - - - }); + // Se o core executou com erro + if (code !== 0) { + db.update(Request, request_object.id, 'Error', function (result) {}); + console.log("Erro no retorno do core. Código: " + code); + logger.incrementError('core', "Erro no retorno do core. Código: " + code); + data = { + 'error' : "Erro no Core", + 'code' : code, + 'id' : id + }; + } else { + // Se o core executou normal + db.update(Request, request_object.id, 'Completed', function (result) {}); + res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4'}); + logger.incrementService("videos", "traducoes"); + data = { + 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4', + 'versao' : '1.0', + 'legenda' : '' + }; + } + + if (req.body.callback !== undefined) { + data = querystring.stringify(data); + requests.postRequest(path, data); + } + }); + + + }); /* Listener que dispara quando a requisição ao core da erro */ child.on('error', function(code, signal){ diff --git a/helpers/core.js b/helpers/core.js index 1929c8f..3f82454 100644 --- a/helpers/core.js +++ b/helpers/core.js @@ -68,7 +68,7 @@ function call(id, command_line, req, res, Request, request_object, req_type) { // Se a chamada foi feita com sucesso child.on('close', function(code, signal) { - var data; + var data; // Endereço do callback var path = url.parse(req.body.callback); @@ -87,15 +87,24 @@ function call(id, command_line, req, res, Request, request_object, req_type) { }); console.log("Erro no retorno do core. Código: " + code); logger.incrementError('core', "Erro no retorno do core. Código: " + code); + data = { + 'error' : code + }; } else { // Se o core executou normal db.update(Request, request_object.id, 'Completed', function (result) {}); res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4'}); logger.incrementService(req_type, "traducoes"); - } - // Chama o callback - requests.postRequest(path, data); + data = { + 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4', + 'versao' : '1.0', + 'legenda' : '' + }; + } + console.log("Path == " + path); + data = querystring.stringify(data); + requests.postRequest(path, data); }); // Se a chamada deu erro diff --git a/helpers/requests.js b/helpers/requests.js index a927ed2..fde58c9 100644 --- a/helpers/requests.js +++ b/helpers/requests.js @@ -14,8 +14,8 @@ function postRequest(path, data) { var requesting = http.request(options, function(res) { /* Debugging */ - - console.log('== Chamando callback: ' + path.hostname) + + console.log('=== Chamando callback: ' + path.hostname); console.log('=== Response status: ' + res.statusCode); console.log('=== Response headers: ' + JSON.stringify(res.headers)); @@ -24,7 +24,7 @@ function postRequest(path, data) { res.on('data', function (chunk) { console.log('=== Response body: ' + chunk); }); - + }); requesting.on('error', function (e) { @@ -35,4 +35,4 @@ function postRequest(path, data) { requesting.end(); } -module.exports.postRequest = postRequest; \ No newline at end of file +module.exports.postRequest = postRequest; diff --git a/logsystem/errors.log b/logsystem/errors.log index 99dfbeb..f63cc66 100644 --- a/logsystem/errors.log +++ b/logsystem/errors.log @@ -1,7 +1,7 @@ { "resumo": { "bloqueante": { - "1": 3, + "1": 22, "2": 0, "3": 2, "4": 49 diff --git a/server.js b/server.js index 0c4d524..444d654 100644 --- a/server.js +++ b/server.js @@ -41,6 +41,11 @@ app.get('/', function(req, res){ res.send(200, { 'status': 'server is running!' } ); }); +app.post('/testcallback', function(req, res) { + console.log(req.body); + res.send(200, { 'status': 'Callback OK' } ); +}); + app.post('/api', function(req, res) { console.log("\n\n\n============================================="); console.log("[" + new Date().toISOString() + "] Requisição do IP: " + req.ip); -- libgit2 0.21.2