Pearware Blog : Tag rake, everything about rake /tag/rake.rss en-us 40 agile web development Reworking Net::SFTP to handle large file downloads <p>I&#8217;m writing an application that downloads access logs from our production servers and runs the <a href="http://awstats.sourceforge.net">AWStats</a> package against them to create the statistics web pages. This process is setup as a Rake task that uses the Net::SFTP library used by <a href="http://www.capify.org">Capistrano</a>, written by Jamis Buck. There is also a front-end Rails application to manage each of the applications to be retrieved. Everything was working great until I tried to grab a 550MB file from one of our servers. Net::SFTP chocked as it ran out of memory.</p> <p>It turns out that the command:</p> <pre><code> sftp.get_file log_file, local_file</code></pre> <p>ends up putting the whole file into memory, which is fine for small files, but not the large one that I was trying to download. Luckily it wasn&#8217;t too bad to refactor my class. Here&#8217;s the new code to achieve the same effect as the above <em>sftp.get_file</em> command.</p> <div class="CodeRay"><pre> stat = sftp.stat( log_file ) offset = 0 file_length = stat.size length = 64 * 1024 * 1024 File.open(local_file, File::CREAT|File::TRUNC|File::RDWR, 0644) do |f| while (offset &lt; file_length) sftp.open_handle(log_file) do |handle| data = sftp.read(handle, :length =&gt; length, :offset =&gt; offset) f.write(data) offset += data.length end end end</pre></div> <p>This downloads the file in 64MB increments, using only that much memory at any time.</p> Tue, 26 Jun 2007 14:13:00 -0500 urn:uuid:ec2657e4-c48c-4060-aa19-59894f59df7c http://blog.pearware.org/2007/06/26/reworking-net-sftp-to-handle-large-file-downloads#comments ruby rails ruby rails capistrano net::sftp rake http://blog.pearware.org/2007/06/26/reworking-net-sftp-to-handle-large-file-downloads Error working with large YAML files <p>As part of my <a href="http://blog.pearware.org/articles/2006/08/13/rails-migrations-dropping-default-constraint-on-column">Application migration project</a>, I need to pre-populate the new database with zip code data. The <a href="http://pragmaticprogrammer.com/titles/fr_rr/">Rails Recipes book</a> (very useful) has a nice recipe on extracting fixtures from live data. So, I&#8217;ve created a zips.yml file that contains all the zip code data that I can insert into the new database. However, when I try to load the fixture using this <a href="http://rails.techno-weenie.net/tip/2006/6/8/bootstrapping_your_database">very cool rake task from Technoweenie</a>, the <span class="caps">YAML</span> library throws the following exception: <strong>SystemStackError: stack level too deep</strong>.</p> <p>It is possible to work-around the error by increasing the stack limit on the command-line. on Mac <span class="caps">OSX</span> (and probably Linux/Unix), the following command can be run before running the rake task: <strong>ulimit -s 32768</strong>. This increases the default stack limit to 32MB, which should be enough, unless the yaml file is really large, I suppose.</p> <p>Does anyone know if somebody is working on fixing the <span class="caps">YAML</span> library to be nicer to the stack?</p> Sun, 13 Aug 2006 13:34:00 -0500 urn:uuid:5fdcea08-eeac-48bf-976c-5b5c89f34597 http://blog.pearware.org/2006/08/13/error-working-with-large-yaml-files#comments ruby rails yaml error bootstrap rake http://blog.pearware.org/2006/08/13/error-working-with-large-yaml-files