1
votes

In my rails app, I'm trying to move video uploading to a background task. I created a Sidekiq worker but need to render a JSON response at the end of the task (what I had previously done in the controller). For example, this is what the current controller looks like:

  # 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

How can I render JSON from a sidekiq background job?

1

1 Answers

2
votes

You cannot render a Json from sidekiq background worker.

Background thread are queues, that will run in the background when the server is free to process process heavy task.

It is specifically useful when we do not want the user to wait for the process to get over, instead we put the task in queue( to be executed later).

Example Use-Cases,

  1. Sending Emails
  2. Uploading Files
  3. Playing with Images ( Compression, Resizing, Uploading )

So Create a Video Upload Thread, post which you can render a json in the controller itself.