diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 278bcb0..cf39691 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -211,6 +211,24 @@ module Mongo end end + # Fsync, then lock the mongod process against writes. Use this to get + # the datafiles in a state safe for snapshotting, backing up, etc. + # + # @return [BSON::OrderedHash] the command response + def lock! + cmd = BSON::OrderedHash.new + cmd[:fsync] = 1 + cmd[:lock] = true + self['admin'].command(cmd) + end + + # Unlock a previously fsync-locked mongod process. + # + # @return [BSON::OrderedHash] command response + def unlock! + self['admin']['$cmd.sys.unlock'].find_one + end + # Apply each of the saved database authentications. # # @return [Boolean] returns true if authentications exist and succeeed, false diff --git a/test/connection_test.rb b/test/connection_test.rb index eee2794..84b4868 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -143,6 +143,24 @@ class TestConnection < Test::Unit::TestCase end end + def test_fsync_lock + @mongo.lock! + assert_equal 1, @mongo['admin']['$cmd.sys.inprog'].find_one['fsyncLock'], "Not fsync-locked" + assert_equal "unlock requested", @mongo.unlock!['info'] + unlocked = false + counter = 0 + while counter < 5 + if @mongo['admin']['$cmd.sys.inprog'].find_one['fsyncLock'].nil? + unlocked = true + break + else + sleep(1) + counter += 1 + end + end + assert unlocked, "mongod failed to unlock" + end + context "Saved authentications" do setup do @conn = Mongo::Connection.new