Recently I ran into an terrible bug. While working in my Radiant site
on my local machine and navigating thru my Radiant site, suddenly the
after one click too many the styling of the site went away. After a few
minutes I discovered that the contents of my entire “public” folder were
deleted. This behavior was almost virus like.
Ugh.
If you’re like me when you get the time to develop your own personal
projects, its a flurry of effort and you’re changing all kinds of
things. My point here is I had my hand in so many pies I hardly knew
where to look or what I might have done to create the issue. I was
setting up Capistrano which uses delete commands, so that seemed a
possibility. I installed the back_door extension which allows for
embedded ruby, so I wondered if I might have coded something in a ruby
tag inadvertently. There were a number of rocks to look under. As far
as I knew I hadn’t written any code related to deleting files, so I was
perplexed.
Eventually when the problem reappeared it was on the host. After some
effort I was able to identify the exact action that resulted in the
issue. From the admin interface, I accessed the users, selected a user,
updated that user and saved that user. Anytime I took these steps, sure
enough, my entire public folder got wiped out. Thank the programmer
gods for SVN. If not for this backup, it would have been disaster.
These disappearing files weren’t even ending up in my trash bin.
After looking under numerous rocks an idea occurred about what it might
have been that I had done. Recently, in order to allow for more dynamic
pages, I had to turn off caching. Whether documented or not, I fished
thru the code and ended up coding this in my environment.rb to turn off
caching:
ResponseCache.instance.perform_caching = false # the culprit
ResponseCache.defaults[:directory] =
ActionController::Base.page_cache_directory
ResponseCache.defaults[:logger] = ActionController::Base.logger
It seemed a logical assumption. Plus it actually worked, after testing
I could see that there was no caching. Problem solved, on my merry way.
The trouble is, this ended up in a discrete error that didn’t surface
until later, as described earlier. I didn’t figure out exactly why
updating a user caused this to happen but I did determine the actual
code that destroyed my public directory could be found in Radiant’s
response_cache.rb.
Expires the entire cache.
def clear
Dir["#{directory}/*"].each do |f|
FileUtils.rm_rf f
end
end
Now mind you, if you use Radiant properly this chunk of code won’t bake
your cookies. I made a mistaken assumption about how to turn caching
off. (Actually, the line simply appeared too early; it should have
appeared a few lines down.) Later, I discovered the lines of caching
code were already available to me in the config/environment/
subdirectory.
Oftentimes, I hear programmers tell you the best documentation is the
code itself. They recommend that you read the code. Granted, I did a
pretty boneheaded thing, but when you’re in flow programming away and
you want to do something which, at heart, seems quite simple, you don’t
want to spend the in-depth time learning all the facets of that thing,
by inspecting every line of code. You want a quick answer.
Ruby is a language that has so much power and complexity related to its
dynamic nature, that it’s quite easy to miss some significant things a
chunk of code is doing. After more than 6 months, I still spot chunks
of Ruby code that are mind-numbing.
All I can say is the read-the-code form of documentation is not ranked
highly in my book. If Ruby had a dynamic IDE debugger on par with
Visual Studio, it might be much easier to learn from the code. But as
it stands I usually resort to dumping displays to the console in
punchcard-programmer fashion.
As a final note: we might want to put some safegaurds in the above
routine to avoid bonehead blunders like my own. Deleting everything in
a directory is quite a drastic action!