mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
simplified upload progress class, add @frequency to determine how often to update the upload progress class
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@291 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
d00bcefbe7
commit
a24136cd55
1 changed files with 30 additions and 31 deletions
|
@ -6,15 +6,16 @@ class Upload < GemPlugin::Plugin "/handlers"
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@path_info = options[:path_info]
|
@path_info = options[:path_info]
|
||||||
|
@frequency = options[:frequency] || 3
|
||||||
@request_notify = true
|
@request_notify = true
|
||||||
if options[:drb]
|
if options[:drb]
|
||||||
require 'drb'
|
require 'drb'
|
||||||
DRb.start_service
|
DRb.start_service
|
||||||
Mongrel.const_set :UploadStatus, DRbObject.new(nil, options[:drb])
|
Mongrel.const_set :Uploads, DRbObject.new(nil, options[:drb])
|
||||||
else
|
else
|
||||||
Mongrel.const_set :UploadStatus, Mongrel::UploadProgress.new
|
Mongrel.const_set :Uploads, Mongrel::UploadProgress.new
|
||||||
end
|
end
|
||||||
Mongrel::Upload::Status.instance.instance_variable_set(:@debug, true) if options[:debug]
|
Mongrel::Uploads.debug = true if options[:debug]
|
||||||
end
|
end
|
||||||
|
|
||||||
def request_begins(params)
|
def request_begins(params)
|
||||||
|
@ -31,55 +32,57 @@ class Upload < GemPlugin::Plugin "/handlers"
|
||||||
|
|
||||||
private
|
private
|
||||||
def upload_notify(action, params, *args)
|
def upload_notify(action, params, *args)
|
||||||
return unless params['PATH_INFO'] == @path_info && params[Mongrel::Const::REQUEST_METHOD] == 'POST'
|
return unless params['PATH_INFO'] == @path_info &&
|
||||||
upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
|
params[Mongrel::Const::REQUEST_METHOD] == 'POST' &&
|
||||||
Mongrel::UploadStatus.send(action, upload_id, *args) if upload_id
|
upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
|
||||||
|
if action == :mark
|
||||||
|
last_checked_time = instance_variable_get(checked_var(upid)) rescue nil
|
||||||
|
return unless last_checked_time && Time.now - last_checked_time > @frequency
|
||||||
|
end
|
||||||
|
return unless Mongrel::Uploads.send(action, upload_id, *args)
|
||||||
|
instance_variable_set(checked_var(upload_id), (action == :finish ? nil : Time.now))
|
||||||
|
end
|
||||||
|
|
||||||
|
def checked_var(key)
|
||||||
|
key && "@checked_#{key}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Keeps track of the status of all currently processing uploads
|
# Keeps track of the status of all currently processing uploads
|
||||||
class Mongrel::UploadProgress
|
class Mongrel::UploadProgress
|
||||||
|
attr_accessor :debug
|
||||||
def initialize
|
def initialize
|
||||||
@guard = Mutex.new
|
@guard = Mutex.new
|
||||||
@counters = {}
|
@counters = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def check(upid)
|
def check(upid)
|
||||||
status = @counters[upid]
|
|
||||||
puts "#{upid}: Checking" if @debug
|
puts "#{upid}: Checking" if @debug
|
||||||
instance_variable_get(upload_var(status)) if status
|
instance_variable_get(upload_var(upid)) rescue nil
|
||||||
end
|
|
||||||
|
|
||||||
def last_checked(upid)
|
|
||||||
status = @counters[upid]
|
|
||||||
instance_variable_get(checked_var(status)) if status
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(upid, size)
|
def add(upid, size)
|
||||||
@guard.synchronize do
|
@guard.synchronize do
|
||||||
@counters[upid] = rand(Time.now.to_i).to_s.intern
|
@counters[upid] = Time.now
|
||||||
instance_variable_set(upload_var(@counters[upid]), {:size => size, :received => 0})
|
instance_variable_set(upload_var(upid), {:size => size, :received => 0})
|
||||||
puts "#{upid}: Added" if @debug
|
puts "#{upid}: Added" if @debug
|
||||||
end
|
end
|
||||||
|
true
|
||||||
|
rescue NameError # bad upid instance var
|
||||||
|
puts $!.message
|
||||||
|
@guard.synchronize { @counters[upid] = nil }
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark(upid, len)
|
def mark(upid, len)
|
||||||
last_checked_time = last_checked(upid)
|
puts "#{upid}: Marking" if @debug
|
||||||
if last_checked_time.nil? || Time.now-last_checked_time > 3
|
status = check(upid)
|
||||||
status = check(upid)
|
status[:received] = status[:size] - len if status
|
||||||
status[:received] = status[:size] - len
|
|
||||||
instance_variable_set(checked_var(@counters[upid]), Time.now)
|
|
||||||
puts "#{upid}: #{status[:received]}" if @debug
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def finish(upid)
|
def finish(upid)
|
||||||
@guard.synchronize do
|
@guard.synchronize do
|
||||||
puts "#{upid}: Finished" if @debug
|
puts "#{upid}: Finished" if @debug
|
||||||
counter = @counters.delete(upid)
|
instance_variable_set(upload_var(upid), nil) if @counters.delete(upid)
|
||||||
return unless counter
|
|
||||||
instance_variable_set(upload_var(counter), nil)
|
|
||||||
instance_variable_set(checked_var(counter), nil)
|
|
||||||
end
|
end
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
@ -90,10 +93,6 @@ class Mongrel::UploadProgress
|
||||||
|
|
||||||
private
|
private
|
||||||
def upload_var(key)
|
def upload_var(key)
|
||||||
"@upload_#{key}"
|
key && "@upload_#{key}"
|
||||||
end
|
|
||||||
|
|
||||||
def checked_var(key)
|
|
||||||
"@checked_#{key}"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue