渲染后如何调用方法?

时间:2021-09-10 21:32:16

I need to do request on remote service after rendering the page

渲染页面后,我需要在远程服务上做请求

My controller:

after_filter :remote_action, only: :update

def update
  @res = MyService.do_action foo, bar
  return render json: @res[:json], status: @res[:status] unless @res[:success]
end

def remote_action
  # There is remote http request
end

I need to call remote_action method after rendering the page

我需要在渲染页面后调用remote_action方法

1 个解决方案

#1


9  

after_filter is run after the template has been converted into html, but before that html is sent as a response to the client. So, if you're doing something slow like making a remote http request, then that will slow your response down, as it needs to wait for that remote request to finish: in other words, the remote request will block your response.

在将模板转换为html之后运行after_filter,但在此之前将html作为对客户端的响应发送。因此,如果您正在做一些像制作远程http请求那样慢的事情,那么这会降低您的响应速度,因为它需要等待远程请求完成:换句话说,远程请求将阻止您的响应。

To avoid blocking, you could fork off a different thread: have a look at

为了避免阻塞,你可以分叉一个不同的线程:看看

https://github.com/tra/spawnling

Using this, you would just change your code to

使用它,您只需将代码更改为

def remote_action
  Spawnling.new do
    # There is remote http request
  end
end

The remote call will still be triggered before the response is sent back, but because it's been forked off into a new thread, the response won't wait for the remote request to come back, it will just happen straight away.

在回复响应之前,仍然会触发远程调用,但由于它已被分叉到新线程中,因此响应将不会等待远程请求返回,它将立即发生。

You could also look at https://github.com/collectiveidea/delayed_job, which puts jobs into a database table, where a seperate process will pull them out and execute them.

您还可以查看https://github.com/collectiveidea/delayed_job,它将作业放入数据库表,其中一个单独的进程将它们拉出并执行它们。

#1


9  

after_filter is run after the template has been converted into html, but before that html is sent as a response to the client. So, if you're doing something slow like making a remote http request, then that will slow your response down, as it needs to wait for that remote request to finish: in other words, the remote request will block your response.

在将模板转换为html之后运行after_filter,但在此之前将html作为对客户端的响应发送。因此,如果您正在做一些像制作远程http请求那样慢的事情,那么这会降低您的响应速度,因为它需要等待远程请求完成:换句话说,远程请求将阻止您的响应。

To avoid blocking, you could fork off a different thread: have a look at

为了避免阻塞,你可以分叉一个不同的线程:看看

https://github.com/tra/spawnling

Using this, you would just change your code to

使用它,您只需将代码更改为

def remote_action
  Spawnling.new do
    # There is remote http request
  end
end

The remote call will still be triggered before the response is sent back, but because it's been forked off into a new thread, the response won't wait for the remote request to come back, it will just happen straight away.

在回复响应之前,仍然会触发远程调用,但由于它已被分叉到新线程中,因此响应将不会等待远程请求返回,它将立即发生。

You could also look at https://github.com/collectiveidea/delayed_job, which puts jobs into a database table, where a seperate process will pull them out and execute them.

您还可以查看https://github.com/collectiveidea/delayed_job,它将作业放入数据库表,其中一个单独的进程将它们拉出并执行它们。