2017-03-07 12 views
0

openwhisk에서 trycatch 연결자를 사용하려고 시도하고 오류가 발생했을 때를 대비하여 작업을 재전송하지만 리디렉션 할 수 없습니다. 다음은 내가 시도하고있는 샘플 코드이다.openwhisk에서 trycatch 연결자를 사용하는 동안 발생하는 문제

var openwhisk = require('openwhisk') 
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3'); 
var visualrecognition = new VisualRecognitionV3({ 
api_key: '******', 
version_date: '***'}); 
var pkgcloud = require('pkgcloud'); 
var osConfig = { 
provider: 'openstack', 
useServiceCatalog: true, 
useInternal: false, 
keystoneAuthVersion: 'v3', 
authUrl: '*****', 
tenantId: '******', 
domainId: '********', 
username: 'admin_******', 
password: '******', 
region: 'dallas'}; 
var client = pkgcloud.storage.createClient(osConfig); 
function main(params) { 
return new Promise(function(resolve, reject) { 
    var options = { 
     container: params.containername || 'my-container', 
     remote: params.filename || 'openwhisk.jpg', 
     local: 'test.jpg' 
    }; 
    client.download(options, function(err, result) { 
     var params = { 
      images_file: fs.createReadStream('test.jpg') 
     }; 
     visualrecognition.classify(params, function(err, res) { 
      if (err) { 
       const wsk = openwhisk({ignore_certs: params['$ignore_certs'] || false}) 
      const catchName = "hello" 
    return wsk.actions 
     .invoke({ 
       actionName: catchName, 
       params: catchArgs, 
       blocking: true 
      }) 
     .then(activation => activation.response.result) 
     .catch(error => { 
       try { 
        // if the action ran and failed, the result field is guaranteed 
        // to contain an error field causing the overall action to fail 
        // with that error 
        return error.error.response.result 
       } catch (e) { 
        return { 
         error: { 
          message: `There was a problem invoking ${catchName}.`, 
          cause: error.error 
         } 
        } 
       } 
      }) 
      } else { 
       var json = { 
        container: params.containername, 
        filename: params.filename 
       }; 
       var length = res.images[0].classifiers[0].classes.length; 
       json.score = 0; 
       var type_hierarchy = ""; 
       for (i = 0; i < length; i++) { 
        var score = res.images[0].classifiers[0].classes[i].score; 
        var classes = res.images[0].classifiers[0].classes[i].class; 
        var htype = res.images[0].classifiers[0].classes[i].type_hierarchy; 
        if (score > json.score) { 
         json.score = score; 
         json.class = classes; 
         json.type_hierarchy = htype || "test"; 
        } 
       } 
       console.log(json); 
       resolve(json); 
      } 
     }); 
    }); 
});}; 

Openwhisk nodejs 작업에 trycatch Combinator를 추가하는 방법.

답변

0

OpenWhisk에서이 trycatch 동작을 사용하려면 먼저 OpenWhisk에서 다른 두 가지 동작을 사용할 수 있어야합니다. 호출 할 try 액션 (tryName 키에 정의 됨)이라고하는 하나의 액션이 사용되며, 다른 하나는 오류/처리를 처리하는 catch 액션 (키 catchName에 정의 된)입니다. 예를 들어, try 액션은 action1, catch 액션은 action2입니다.

CLI를 사용하여 trycatch 작업을 호출하는 경우 : wsk action invoke -br trycatch -p '$ tryName'action1 -p '$ catchName'action2 -p param1 -p param2 더 많은 매개 변수가있을 수 있으므로 action1은 다음과 같습니다. 첫 번째 시도라고합니다. 오류가 없으면 결과가 리턴됩니다. action2에는 아무런 문제가 없습니다. 그러나 action1을 호출하는 중에 오류가 발생하면 action2가 catch 처리로 호출되어 오류를 처리하고 결과는 오류 처리시 action2가 반환하는 결과가됩니다.

OpenWhisk에서 trycatch 작업을 사용하는 방법을 보려면이 작업의 테스트 사례를 참조하십시오. 이 파일의 끝에는 https://github.com/openwhisk/openwhisk-catalog/blob/master/tests/src/packages/combinators/CombinatorTests.scala#L144으로 시작합니다.