Tues 29 Aug 2017
Reading time: (~ mins)
If you are interested this will give you some insight on how programmers debug old/unfamiliar code.
Here's a link to the repo that I'll be reviving.
My first deep dive of accepting ruby into my heart involved building many Sinatra apps. One of my favourite projects which I used to showcase my skills was Snappy, an online screenshot tool. It had an api and everything! I was proud of building it. The ecosystem of working with a single file application and hosting it free in a matter of minutes on Heroku was awesome! Unfortunately every few months I would get nostalgic and try to visit the site only to be reminded that it was flatly serving an Internal server error.
I've finally had enough and decided to pull a Lazarus and raise this from its grave. Attempt 1: start the damn thing
rackup
All I get in return for visiting this friend is a complaint that he's feeling old. Attempt 2: get latest gems
bundle update
As gems whiz by I realize this brings with it the significant Sinatra 2.0 update, hopefully nothing explodes too violently...Still there is a complaint. Snappy tells me that it wants sqlite gems.
File: snappy.rbrequire 'data_mapper' require 'json' DataMapper::setup(:default, ENV['HEROKU_POSTGRESQL_TEAL_URL'] || "sqlite3://#{Dir.pwd}/gallery.db")File: Gemfile
gem 'data_mapper' gem 'dm-postgres-adapter', :group => :production gem 'phantomjs'
Somehow I only have the gem listed here that allows it to work with Heroku (since they use postgres). Seems like an easy fix. Attempt 3: add missing gems
gem 'data_mapper' gem 'dm-sqlite-adapter', :group => :development # gem 'dm-postgres-adapter', :group => :production gem 'phantomjs'
Upon start up it seems my ports are occupied so I
killall rackup
and rackup up again. Now the app hangs trying to work with non-existent files.
File: snappy.rbafter do s = Snapshot.all :created_at.lt => Time.now - 2 * 60 * 60 * 24 * 7 JSON.parse(s.to_json).each { |s| File.delete "./public/#{s['name']}.png" # failing on this line begin File.delete "./public/2#{s['name']}.png" rescue 'file not found' end } s.destroy end
In this after request hook I access the db and try to delete stale screenshots. I definitely don't source control user's screenshots so it seems my db had lingering test data. That's an easy fix, Attempt 4: delete db and start fresh
rm gallery.db
It works(locally...)! Amazing, though I get a chuckle at seeing phantomjs getting installed for its first time use on this laptop. Since the boat is no longer under water, let's get it back online at Heroku. Heroku quickly complains about ruby versions. It seems like it was stuck on whatever version I used years ago but the latest gems needed < ruby2.3.x. Attempt 5: Update Heroku's ruby
File: Gemfilesource 'https://rubygems.org' ruby "2.3.0" # gem 'sinatra' gem 'json'
Unfortunately Heroku still serves me an Internal server error. Time to dive into Heroku's guts:
heroku logs
That gives me a tail of the server logs and I see it's a stale db data issue again, similar to what I had on my local. Same solution then, Attempt 6: Reset Heroku db
heroku pg:reset HEROKU_POSTGRESQL_TEAL_URL --confirm snppy
More errors, some complaint about the method :name not existing for env['sinatra.error']. I look at the latest Sinatra README which confirms that this indeed has changed. Attempt 7: replace :name with :message
File: snappy.rberror do halt 500, 'Sorry there was a nasty error - ' + env['sinatra.error'].message # end
FINALLY IT WORKS! Not too much trouble to bring a beloved friend back on his feet. Try it out and feel free to leave any feedback. My next post will be about quickly revamping this old beginner project using all the knowledge I've gained over the last few years.