After researching the Ruby Mongo driver source code and some blog posts, I developed a set of database administration tasks to run following certain rake tasks. The resulting code looks like this:
script = open("./scripts/update_stats.js", &:read)
database = Mongoid::Config::master
result = database.command({:$eval => script})
logger.debug result
logger.warn "Finished updating stats"
Within the script are multiple functions, with one final call like so:
r = update_stats();
print("update result:");
print (r);
Although everything runs smoothly via the command line, I want to extract the result value and store it in my logs for better insight. Instead of storing and retrieving from the database, there must be a more efficient solution. Currently, the log output only shows:
DEBUG 2012-01-03 22:27:03 -0800 (21392) {"retval"=>nil, "ok"=>1.0}
This message doesn't provide much information beyond confirming that nothing went wrong. So, the question remains - how can I capture and log the return value of update_stats
within my Ruby code?