2015-08-31 16 views
0

問題:滑軌:使用霧和resque文件上載到AS3

我有需要用戶上傳一些類型電子表格的rails應用程序(CSV,XSLX,XSL等)進行處理,其可以是一項代價高昂的操作,因此我們決定將其發送給後臺服務作爲解決此問題的解決方案。我們關心的問題是,由於我們的生產系統是在Heroku我們需要存儲在AS3文件先後來進行處理檢索。

由於上傳文件到AS3,本身就是一個昂貴的操作,這可能應該也可以作爲後臺作業完成。問題是關注的,使用Resque這樣做可以吃了大量的RAM由於Resque需要把文件數據到Redis的或以後的檢索。如您所知,Redis僅將其數據存儲在RAM中,並且還喜歡簡單的鍵值對,因此我們希望儘量避免這種情況。

下面有一些僞因爲我們希望有哪一個例子嘗試做:

工人/ AS3Uploader.rb

require 'fog' 

class AS3Uploader 
    @queue = :as3_uploader 
    def self.perform(some, file, data) 
    # create a connection 
    connection = Fog::Storage.new({ 
     :provider     => 'AWS', 
     :aws_access_key_id  => APP_CONFIG['s3_key'], 
     :aws_secret_access_key => APP_CONFIG['s3_secret'] 
    }) 

    # First, a place to contain the glorious details 
    directory = connection.directories.create(
     :key => "catalog-#{Time.now.to_i}", # globally unique name 
     :public => true 
    ) 

    # list directories 
    p connection.directories 

    # upload that catalog 
    file = directory.files.create(
     :key => 'catalog.xml', 
     :body => File.open(blah), # not sure how to get file data here with out putting it into RAM first using Resque/Redis 
     :public => true 
    end 

    # make a call to Enqueue the processing of the catalog 
    Resque.enqueue(CatalogProcessor, some, parameters, here) 
end 

控制器/ catalog_upload_controller.rb

def create 
    # process params 

    # call Enqueue to start the file processing 
    # What do I do here? I could send all of the file data here right now 
    # but like I said previously that means storing potentially 100s of MB into RAM 
    Resque.enqueue(AS3Uploader, some, parameters, here) 
end 

回答

0

我建議的方式,你做的是

  • 您在tmp目錄文件創建並獲得file-path
  • 告訴Resque使用file-path
  • 使Resque存儲在redis不是file-path上傳文件存儲整個file-content(這將是非常昂貴)
  • 現在工人將文件上傳到AWS- S3

注意:如果你有多個實例像後臺處理一個實例,一個數據庫,一個作爲實用實例,然後您的TMP目錄可能無法使用到其他實例..所以將文件存儲在實例中的temp DIR控股在resque