【问题标题】:Render back response after job completion - Delayed jobs作业完成后返回响应 - 延迟作业
【发布时间】:2020-10-09 13:27:25
【问题描述】:

之前我有我的控制器类,它正在创建服务实例并调用服务方法“同步”,如下所示。在“同步”方法完成后,rails 用于将 JSON 消息呈现回我的 node.js。

if @service.valid_connection?
      @service.synchronize
      render json: {
        status: :unprocessable_entity,
        errors: @service.message
      }, status: :ok
    else
      render json: {
        status: :unprocessable_entity,
        errors: @event.errors.full_messages
      }, status: :ok
    end

但是,由于我的“同步”方法执行时间有点长,所以我创建了一个延迟作业来占用我的“同步”任务。所以目前我的控制器看起来像这样

if @service.valid_connection?
      ::Events::WegSyncJob.perform_later(event_id, b_cancelled)
    else
      render json: {
        status: :unprocessable_entity,
        errors: @event.errors.full_messages
      }, status: :ok
    end

现在我无法将我的渲染 JSON 放在这里,因为它会在作业传递给延迟作业后立即执行,我在作业类中使用 after_perform 方法:

after_perform do |job|
    //below code is wrong
    render json: {
      status: :ok,
      message: "Check notifications"
    }, status: :ok
  end

  def perform(event_id, b_cancelled)
    //call to synchronize
  end

但是,我不能从我的 Job 类中调用“渲染”,因为它只能从控制器中调用。完成此后台作业后,如何将 JSON 消息返回到我的 node.js (UI)?

【问题讨论】:

    标签: ruby-on-rails response sidekiq delayed-job rails-activejob


    【解决方案1】:

    第 1 步。 延迟作业完成后,它必须将同步方法的输出保存到数据库中。

    第 2 步。在“UI”方面,编写一个定期向服务器发送 GET 请求的方法。

    第 3 步。您的控制器方法检查数据库并返回 'Nil' 或返回延迟作业输出的 JSON 对象。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2013-06-10
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多