简体   繁体   English

如何在ActionHero.js中以编程方式调用动作

[英]How to programmatically call action in ActionHero.js

I'm new to ActionHero and I have a need to add something to the queue, wait until that is finished and get the data back, then proceed with another queue'd item before responding to the client. 我是ActionHero的新手,需要在队列中添加一些内容,等到完成后再取回数据,然后再处理另一个队列中的项目,然后再响应客户端。 Looks like this: 看起来像这样:

  1. API client hits /foo API客户端命中/ foo
  2. the foo action calls the bar action and waits... foo动作调用bar动作并等待...
  3. When bar is finished it returns a value of 123abc 当bar完成时,它返回值123abc
  4. 123abc is then passed to the queue again for the task that needs it 然后将123abc再次传递给队列以执行需要它的任务

It's worth noting here, I understand this isn't optimal, but this involves 3 servers. 在这里值得注意的是,我知道这不是最佳选择,但这涉及3台服务器。 /foo hits my Node ActionHero server, 123abc comes from a Java server (think of it as an authentication service), and then my Node ActionHero server is going to send this off to be picked up by a .NET server. / foo命中我的Node ActionHero服务器, 123abc来自Java服务器(将其视为身份验证服务),然后我的Node ActionHero服务器将其发送给.NET服务器。

The way I have it setup now with a task does hit the Java server and the Java server logs out the value I need but in the code below it's an empty object 我现在通过任务设置的方式确实击中了Java服务器,Java服务器注销了我需要的值, 但是在下面的代码中它是一个空对象

The run() in ACTION: ACTION中的run():

api.actions.enqueue('MyJavaServerTask', {...}, function(error, toRun){
  console.log(arguments) // <-- returns { '0': null, '1': true }
  data.response.success = true
  api.queue.push(data, next)
})

TASK: 任务:

exports.task = {
  name: 'MyJavaServerTask',
  description: 'My Java Server Task',
  frequency: 0,
  queue: 'default',
  run: function (api, params, next) {
    var job = {
      response: {},
      connection: {id: 'none'},
      authorization: params.authorization,
      params: {
        apiVersion: '1',
        action: 'MyJavaServerTask',
        ...
      }
    }
    api.queue.push(job, function() {
      console.log(arguments) // <-- returns {}
      next()
    })
  }
}

On the Java server though, again, I do see the request and it's successful and it returns the right JSON. 但是,再次在Java服务器上,我确实看到了请求,请求成功了,并且返回正确的JSON。 The log for that looks like 该日志看起来像

15:19:22.542 [run-main-0] INFO  application - apiq read: {"id":"none","params":{"apiVersion":"1","action":"MyJavaServerTask"}}
generating tables and key
15:19:22.543 [run-main-0] INFO  application - result: {"key":"..."}

So... how do I get that key from there back to be used to append to the next queue'd item? 那么...我如何从那里得到那个key ,以用于附加到下一个排队的物品?

If the API client expects a synchronous response from the Java backend, why not just call the request from within the action? 如果API客户端期望Java后端进行synchronous响应,为什么不只从操作中调用请求呢?

exports.foo = {
  name: 'foo',
  description: 'foo',
  outputExample: {},
  inputs: {}

  run: function(api, data, next){
    var request = require('request);
    request.get('bar.com/bar', function(error, response){
       if(error){ return next(error); }
       var body = JSON.stringify(response.body);
       data.response = body;
       return next();
    });
  }

};

In node, this pattern is great, because while your action is waiting, the node server can serve other requests. 在节点中,这种模式很棒,因为在您的操作等待期间,节点服务器可以处理其他请求。

I can't quite tell if your task example is enqueuing another task? 我不太清楚您的任务示例是否正在排队另一个任务? What is api.queue ? 什么是api.queue Either way, there is no way to get a value from a task back to an action. 无论哪种方式,都无法将任务中的价值带回行动。 If you want the client to POLL, you can have your task store the eventual response from the Java server in redis or somewhere else, and then create another action which checks if the key exists, and returns it if it does. 如果希望客户端进行轮询,则可以让您的任务将来自Java服务器的最终响应存储在Redis或其他地方,然后创建另一个操作,该操作检查密钥是否存在,如果存在则返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM