AmazonS3ã«ä¸¦åã¢ãããã¼ã
http://d.hatena.ne.jp/shokai/20100220/1266646461 ãgearmanã§ä¸¦ååãã
upload-worker.rb
#!/usr/bin/env ruby require 'rubygems' require 'gearman' require 'aws/s3' BUCKET = "your-bucket-name" GEAR_TASK = 'upload-awss3' if ARGV.size > 0 (ARGV.first.to_i-1).times do pid = fork break if !pid puts "fork pid:#{pid}" end end AWS::S3::Base.establish_connection!(:access_key_id => "your-key", :secret_access_key => "your-secret") #Gearman::Util.debug = true w = Gearman::Worker.new(['localhost:7003']) w.add_ability(GEAR_TASK){|query, job| prefix, name = query.split(/,/) filename = name.split(/\//).last begin print "upload #{name} ... " AWS::S3::S3Object.store("/#{prefix}/"+filename, open(name), BUCKET, :access => :public_read) rescue AWS::S3::ResponseError => error puts error else puts "success => http://#{BUCKET}.s3.amazonaws.com/#{prefix}/#{filename}" end "http://#{BUCKET}.s3.amazonaws.com/#{prefix}/#{filename}" } loop do w.work end
jobã¯ã«ã³ãåºåãã§"prefix,filename"ã§æ¸¡ãã
workerãè¤æ°èµ·åããã¦ããã¦ããclientã§taskç»é²ããã
ruby upload-worker.rb 5
5ã¤èµ·åãã
fork pid:89264 fork pid:89265 fork pid:89266 fork pid:89267
upload-client.rb
#!/usr/bin/env ruby require 'rubygems' require 'gearman' GEAR_TASK = 'upload-awss3' c = Gearman::Client.new(['localhost:7003']) taskset = Gearman::TaskSet.new(c) now = Time.now ARGV.sort{|a,b|a.split(/\//).last.to_i <=> b.split(/\//).last.to_i}.each{|name| query = "#{now.to_i},#{name}" puts "add task #{query}" task = Gearman::Task.new(GEAR_TASK, query) task.on_complete{|res| puts "#{name} => #{res}" } taskset.add_task(task) } taskset.wait(ARGV.size*5) puts "finished #{ARGV.size} files #{Time.now-now} (sec)"
EC2ã¤ã³ã¹ã¿ã³ã¹ããS3ã¸ã¢ãããã¼ãããå ´åãworker 5ã¤ã§ç´5åã10åã§ç´7åéã«ãªã£ãã