试图让我的控制器每 60 秒连续触发一次。 (rails)

Trying to have my controller continuously fire every 60 seconds. (rails)

我正在构建一个代码应用程序,它会根据每 60 秒刷新一次的数据提醒用户。我不希望新的数据调用来自客户端,但不知道如何让控制器持续进行 API 调用并推送到客户端。

 def search
    @price = find_price()
    render json: @price
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    # def setprice
    #   @price = Price.find(params[:id])
    # end

    def request_api()
      response = Excon.get(
        'example.api'
      )

      return nil if response.status != 200
    data = JSON.parse(response.body)
    data['PriceRange']['4']
    end

    def find_price()
      request_api()
    end

    # Only allow a trusted parameter "white list" through.
    def price_params
      params.require(:price).permit(:price)
    end
end

简短的回答是您不能通过控制器来完成,因为一旦控制器处理了客户端的请求,它就会“忘记”客户端。您想要的是某种 ActionCable 解决方案。 ActionCable 是 Rails' 设置 websockets 的方式;如果你有一个 websocket 连接,服务器可以将更新推送到连接到它的每个客户端。