사용자가 직접 파일을 S3에 업로드 할 앱이 있습니다. 이것은 작동 중입니다. 이제 파일을 가져오고 처리를 위해 'tmp/files'에 stash하는 백그라운드 작업자 (현재 delayed_job)가 필요합니다.Rails 앱은 S3에서 파일을 어떻게 다운로드합니까?
어떻게이 작업을 수행 할 수 있습니까?
편집 : 앱이 현재 EC2에서 실행 중입니다.
사용자가 직접 파일을 S3에 업로드 할 앱이 있습니다. 이것은 작동 중입니다. 이제 파일을 가져오고 처리를 위해 'tmp/files'에 stash하는 백그라운드 작업자 (현재 delayed_job)가 필요합니다.Rails 앱은 S3에서 파일을 어떻게 다운로드합니까?
어떻게이 작업을 수행 할 수 있습니까?
편집 : 앱이 현재 EC2에서 실행 중입니다.
배경 작업자는 웹 응용 프로그램과 독립적으로 실행됩니다.
일반적으로 사용되는 Rails 백그라운드 작업자 솔루션에 대해서는 Resque을 시도하십시오. 웹 앱과 독립적으로 Resque를 시작하고 응용 프로그램과 독립적으로 작업을 수행한다는 아이디어가 있습니다.
이 작업자는 S3에 기본 HTTP 요청을 보냈습니다. 시작하기에 앞서 API reference card입니다. 아이디어는 Ruby REST 클라이언트를 사용하여 이러한 요청을 보내고 S3에서 얻은 응답을 구문 분석하는 것입니다. Rest-client이 작업을 수행하는 데 사용할 수있는 보석입니다.
근로자가 조금 더 쉬울 수도있는 S3 gem을 선택적으로 사용할 수있게 할 수도 있습니다. 이 방법으로
, 당신은 당신의 노동자 뭔가 같은picture = S3Object.find 'headshot.jpg', 'photos'
사용 Resque을하는 스크립트를 실행해야 것입니다.
는 레디 스 가고 레디 스 - 투 - 나 EC2 시스템에 로컬로 레디 스를 설치 (노동자에 대한 정보를 저장하기 위해) 중 하나를 사용할 필요가 Resque으로
gem 'resque'
gem 'resque-status'
를 추가합니다.
이development: localhost:6379
test: localhost:6379
fi: localhost:6379
production: localhost:6379
에게 당신이 뭔가를해야합니다 Resque가 편집 설정/초기화/우리 지역 레디 스를 사용하고 여기에
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'production'
resque_config = YAML.load_file(rails_root + '/config/resque.yml')
Resque.redis = resque_config[rails_env]
# This is if you are using Redis to go:
# ENV["REDISTOGO_URL"] ||= "redis://REDISTOGOSTUFFGOESHERE"
# uri = URI.parse(ENV["REDISTOGO_URL"])
# Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)
Resque::Plugins::Status::Hash.expire_in = (24 * 60 * 60) # 24hrs in seconds
Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }
resque.rb, 그래서 resque.yml는 다음과 같습니다 설치 그래서 후
하느님처럼 직원을 시작/관리 할 수 있습니다.이렇게 설치 한 다음 응용 프로그램의 config/폴더에 "resque-production.god"를 추가하십시오. 작업자를 시작할 수 있습니다 이를 통해 하나님 -c 설정/resque-production.god 같은이 잡아 줄께는 config/resque-production.god 파일 :
마지막으로rails_env = ENV['RAILS_ENV'] || "production"
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/..'
num_workers = 1
num_workers.times do |num|
God.watch do |w|
w.dir = "#{rails_root}"
w.name = "resque-#{num}"
w.group = 'resque'
w.interval = 30.seconds
w.env = {"QUEUE"=>"*", "RAILS_ENV"=>"production"}
w.start = "rake -f #{rails_root}/Rakefile environment resque:work --trace"
w.log = "#{rails_root}/log/resque.log"
w.err_log = "#{rails_root}/log/resque_error.log"
# restart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 350.megabytes
c.times = 2
end
end
# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
on.condition(:process_running) do |c|
c.running = true
end
end
# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
c.interval = 5.seconds
end
# failsafe
on.condition(:tries) do |c|
c.times = 5
c.transition = :start
c.interval = 5.seconds
end
end
# start if process is not running
w.transition(:up, :start) do |on|
on.condition(:process_running) do |c|
c.running = false
end
end
end
end
노동자. 그들은 응용 프로그램/노동자/폴더 (여기에 응용 프로그램/노동자/processor.rb)로 이동
class Processor
include Resque::Plugins::Status
@queue = :collect_queue
def perform
article_id = options["article_id"]
article = Article.find(article_id)
article.download_remote_file(article.file_url)
end
end
그것은이 제 모델의 콜백 (응용 프로그램/모델/article.rb)
class Article < ActiveRecord::Base
after_create :process
def download_remote_file(url)
# OpenURI extends Kernel.open to handle URLs as files
io = open(url)
# overrides Paperclip::Upfile#original_filename;
# we are creating a singleton method on specific object ('io')
def io.original_filename
base_uri.path.split('/').last
end
io.original_filename.blank? ? nil : io
end
def process
Processor.create(:article_id => self.id)
end
end
에 의해 트리거됩니다
당신은 Heroku를 사용하고 있습니까? EC2? – Stpn
EC2. 수정 된 질문. 감사. – n8gard
답변이 도움이 되었습니까? – Stpn