Availability Rake Tasks
To write useful rake tasks to manage MongoDB and Redis connectivity.
development: sessions: default: database: blog_development hosts: - localhost:27017
Here is a rake task to create mongo development database:
desc 'Create mongo development database' namespace :db do require 'mongoid' task :create do Mongoid.load!('mongoid.yml', :development) end end
desc 'Check mongo development database connectivity' namespace :db do require 'mongoid' task :test do session = Moped::Session.new([ "127.0.0.1:27017"]) session.use :blog_development puts session.command(ping: 1) end end
Here is a rake task to check redis connection:
desc 'Redis connection tester' task :redis do require "redis" redis = Redis.new begin redis.ping puts "Redis connection established" rescue Exception => e puts e.message puts "Failed to establish connection to redis server" end end
In MongoDB the database is created when you start using it. Sometimes, you need to create a database, in order to make sure that everything is setup properly. You can use the following script to force the creation of a database.
require 'date' require 'moped' hosts = [ "127.0.0.1:27017" ] database = 'bugs_bunny' session = Moped::Session.new(hosts) database = Moped::Database.new(session, database) session.use(database) session.with(safe: true) do |safe| safe[:artists].insert(name: "Bugs Bunny Calling Earth") end
To list databases:
To list all the collections:
In this article, we wrote useful rake tasks to create MongoDB database and check connectivity to MongoDB and Redis.
Software Compatibility Best Practices
I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.
You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.