Skip to content

Commit

Permalink
add an early return to not connect to the servers if there is nothing…
Browse files Browse the repository at this point in the history
… to write or delete
  • Loading branch information
blindgaenger committed Mar 26, 2015
1 parent fc38376 commit ae291a9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lib/paperclip/storage/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ def to_file(style_name = default_style)
end

def flush_writes
with_ftp_servers do |servers|
servers.map do |server|
Thread.new do
@queued_for_write.each do |style_name, file|
remote_path = path(style_name)
log("saving ftp://#{server.user}@#{server.host}:#{remote_path}")
server.put_file(file.path, remote_path)
unless @queued_for_write.empty?
with_ftp_servers do |servers|
servers.map do |server|
Thread.new do
@queued_for_write.each do |style_name, file|
remote_path = path(style_name)
log("saving ftp://#{server.user}@#{server.host}:#{remote_path}")
server.put_file(file.path, remote_path)
end
end
end
end.each(&:join)
end.each(&:join)
end
end

after_flush_writes # allows attachment to clean up temp files
Expand All @@ -53,18 +55,20 @@ def flush_writes
end

def flush_deletes
with_ftp_servers do |servers|
servers.map do |server|
Thread.new do
@queued_for_delete.each do |path|
log("deleting ftp://#{server.user}@#{server.host}:#{path}")
server.delete_file(path)

log("deleting empty parent directories ftp://#{server.user}@#{server.host}:#{path}")
server.rmdir_p(File.dirname(path))
unless @queued_for_delete.empty?
with_ftp_servers do |servers|
servers.map do |server|
Thread.new do
@queued_for_delete.each do |path|
log("deleting ftp://#{server.user}@#{server.host}:#{path}")
server.delete_file(path)

log("deleting empty parent directories ftp://#{server.user}@#{server.host}:#{path}")
server.rmdir_p(File.dirname(path))
end
end
end
end.each(&:join)
end.each(&:join)
end
end

@queued_for_delete = []
Expand Down
21 changes: 21 additions & 0 deletions spec/paperclip/storage/ftp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
end

context "#flush_writes" do
it "doesn't connect to the servers if there is nothing to write" do
attachment.instance_variable_set(:@queued_for_write, {})

attachment.should_not_receive(:with_ftp_servers)
attachment.should_receive(:after_flush_writes).with(no_args)

attachment.flush_writes

attachment.queued_for_write.should == {}
end

it "stores the files on every server" do
original_file = double("original_file", :path => "/tmp/original/foo.jpg")
thumb_file = double("thumb_file", :path => "/tmp/thumb/foo.jpg")
Expand All @@ -105,6 +116,16 @@
end

context "#flush_deletes" do
it "doesn't connect to the servers if there is nothing to delete" do
attachment.instance_variable_set(:@queued_for_delete, [])

attachment.should_not_receive(:with_ftp_servers)

attachment.flush_deletes

attachment.instance_variable_get(:@queued_for_delete).should == []
end

it "deletes the files on every server" do
attachment.instance_variable_set(:@queued_for_delete, [
"/files/original/foo.jpg",
Expand Down

0 comments on commit ae291a9

Please sign in to comment.