【问题标题】:Render JSON response from Sidekiq worker从 Sidekiq 工作人员呈现 JSON 响应
【发布时间】:2015-09-20 04:14:44
【问题描述】:

在我的 rails 应用程序中,我正在尝试将视频上传移动到后台任务。我创建了一个 Sidekiq 工作器,但需要在任务结束时呈现 JSON 响应(我之前在控制器中所做的)。例如,这是当前控制器的样子:

  # start sidekiq worker
  VideoUploaderWorker.perform_async(video_info)

  respond_to do |format|
      if @video.save        
        # add thumbnail image id to video
        @video.update_attributes(:image_id=>@image.id)

        # update project
        @video.project.touch

        format.js {
          if params[:video]
            render 'images/create'
          else
            logger.debug("sending JSON video id #{@video.id.to_json}")
            video_info = @video.id, @video.video_path_url
            render :json => video_info.to_json
          end
        }

      else
        Rails.logger "video save failed"
        Rails.logger.info(@video.errors.inspect)
        format.json { render :json => @video.errors, :status => :unprocessable_entity }
      end
    end
  end

如何从 sidekiq 后台作业渲染 JSON?

【问题讨论】:

    标签: ruby-on-rails json sidekiq


    【解决方案1】:

    您不能从 sidekiq 后台工作人员渲染 Json。

    后台线程是队列,当服务器空闲处理繁重的任务时,它将在后台运行。

    当我们不希望用户等待进程结束时,它特别有用,而是将任务放入队列中(稍后执行)。

    示例用例,

    1. 发送电子邮件
    2. 上传文件
    3. 播放图像(压缩、调整大小、上传)

    所以创建一个视频上传线程,发布后您可以在控制器本身中渲染一个 json。

    【讨论】:

    • 这有帮助,但不是我想要的方式。我希望能够呈现 json。
    • 看看github.com/mperham/sidekiq/wiki/Logging,好像可以用config.log_formatter = Sidekiq::Logger::Formatters::JSON.new设置
    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 2013-07-10
    • 2013-11-05
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多