Search code examples
ruby-on-railsrubymongodbmongoidrake-task

Are there Mongo Admin fsync + lock commands available in Mongoid?


If I wanted to call the fsync + lock methods on my database, is there a way to do this with Mongoid in a Rails app? Is there also a way to only specify the replica node that I want to perform this operation on?

I'm trying to create a rake task to perform backups nightly using cron.


Solution

  • Mongoid 2 uses the 10gen supported driver. Mongoid::Config.master.connection corresponds to the connection object of class Mongo::MongoClient (was Mongo::Connection). This class has an instance method lock! which does the fsyncLock command, and unlock! is its mate.

    http://api.mongodb.org/ruby/current/Mongo/MongoClient.html#lock!-instance_method

    http://api.mongodb.org/ruby/current/Mongo/MongoClient.html#unlock!-instance_method

    There are no options to these methods to specify member/s of a replica set, only by socket which is essentially for internal use. So if you need to fsyncLock a specific replica set member, I recommend that you connect to it explicitly via an explicit connection, e.g., Mongo::MongoClient.new(host, port).

    client = Mongo::MongoClient.new(host, port)
    client.lock!
    # ...
    client.unlock!
    client.close
    

    Mongoid 3 uses Moped and not the 10gen driver. But you can still use the 10gen driver independently for your rake tasks even if you move to Mongoid 3.

    I'm interested in your results and any followup questions.