本文介绍了拖延工作的Rails载波背景程序需要太多时间来上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用载波和载波背景器()在background.whernver中尝试延迟上传文件的作业,我成功上传了文件,并且可以看到作业在后台执行,但是我看不到使用延迟作业和不使用延迟作业的区别。不延迟工作就需要与延迟工作一样的执行时间。不知道出了什么问题请帮助。

I am using carrierwave and carrierwave backgrounder(https://github.com/lardawge/carrierwave_backgrounder) with delayed job for uploading file in a background.whernver i try to upload my file, it uploads successfully and i can see that jobs are executed in the background , but i cant see any difference with using delayed job and without using delayed job.it takes same time to execute without delayed job as it takes with delayed job.dont know what went wrong plz help.Thanks in advance.

这是我的上载器

             # encoding: utf-8

        class AvatarUploader < CarrierWave::Uploader::Base

          # Include RMagick or MiniMagick support:
          # include CarrierWave::RMagick
          # include CarrierWave::MiniMagick

          include ::CarrierWave::Backgrounder::Delay
          include Sprockets::Helpers::RailsHelper

          include Sprockets::Helpers::IsolatedHelper
         # Choose what kind of storage to use for this uploader:
           storage :file
           # storage :fog

        # Override the directory where uploaded files will be stored.
        # This is a sensible default for uploaders that are meant to be mounted:
        def store_dir
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
        end

       def cache_dir
           ""
       end


        # Provide a default URL as a default if there hasn't been a file uploaded:
       def default_url
         "rails.png"
       end


    end

config / initializers / carrierwave_backgrounder.rb内的carrierwave backgrounder文件

carrierwave backgrounder file inside config/initializers/carrierwave_backgrounder.rb

            CarrierWave::Backgrounder.configure do |c|
             c.backend :delayed_job, queue: :carrierwave 
            end

发布模型

        class Post < ActiveRecord::Base
          attr_accessible :description , :avatar, :remote_avatar_url
          mount_uploader :avatar,   AvatarUploader
          process_in_background :avatar
          validates_presence_of :description
        end

后控制器操作

        def create
          @post = Post.new(params[:post])
          respond_to do |format|
         if @post.save
           format.html { redirect_to @post, notice: 'Post was successfully created.' }
           format.json { render json: @post, status: :created, location: @post }
         else
           format.html { render action: "new" }
           format.json { render json: @post.errors, status: :unprocessable_entity }
         end
      end
    end

这是我的观点

        <%= form_for(@post) do |f| %>
          <% if @post.errors.any? %>
          <div id="error_explanation">
          <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from      being saved:</h2>

         <ul>
         <% @post.errors.full_messages.each do |msg| %>
         <li><%= msg %></li>
         <% end %>
         </ul>
        </div>
       <% end %>

      <div class="field">
       <%= f.label :description %><br />
       <%= f.text_area :description %>
      </div>
      <div class="field">
       <%= f.file_field :avatar %>
      </div>
        <div class="actions">
        <%= f.submit %>
      </div>
     <% end %>


推荐答案

不在后台进行初始文件传输。它可以做两件事:

CarrierWave Backgrounder doesn't do the initial file transfer in the background. It can do exactly two things:

process_in_background 让后台任务处理处理,这就是载波所调用的调整图片大小,更改格式,生成缩略图以及执行其他自定义操作的步骤。原始图像仍然照常存储(如下所述)。

process_in_background lets a background task handle "processing," which is what carrierwave calls the step where it resizes your image, changes the format, generates thumbnails, and does other custom manipulations. The original image is still "stored" (described below) as usual.

store_in_background 让后台任务处理存储,这是运营商称之为将上传的图片移动到本地磁盘或其他远程服务器(如S3)上的最终位置的步骤,如果您使用的是Fog。

store_in_background lets a background task handle "storage," which is what carrierwave calls the step where it moves the uploaded image to its final location on the local disk, or on a remote server such as S3 if you're using fog.

您已将模型配置为 process_in_background ,但是您的上传器未配置为进行任何处理,因此,您没有发现任何变化。如果需要,可以将模型也配置为 store_in_background ,但是由于您使用的是本地文件存储(通常文件复制速度非常快),所以这也可以基本上对您的应用程序没有影响。

You've configured your model to process_in_background, but your uploader isn't configured to do any processing, so you're observing no change, as expected. If you wanted, you could configure your model to also store_in_background, but because you're using local file storage (where file copies are generally quite fast), this, also, would have basically no effect on your application.

有关处理和存储的更多信息,请参见。

For more on processing and storage, see the Carrierwave Readme.

这篇关于拖延工作的Rails载波背景程序需要太多时间来上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 17:30