2011-08-10

How to Kill All Connections to MongoDB

This post is similar to the previous one I have posted before for MySQL. Only this time, it's for MongoDB.
What's so nice about MongoDB is that the command line is actually a fully fledged javascript console. So you can do this:
(the embedded script is not visible from the RSS feed though, so click through to the post itself)
The function accepts two variables, both optional.
The first one is the namespace for which you'd like the connections to be working with. It's optional, but you'd usually want to specify it. If you don't, you'll kill all the connections, including the ones that perform replication duties.
The second one is a "wet" run parameter, that if set to true actually does the killing. The default is false, thus not doing anything but print the connections to be killed to your console.

The whole thing is a work in progress, plus my javascript skills are far from perfect, so comments here or on github are welcome.
A potential issue I've already noticed is that when some operations are in a yeild state, the namespace's first letter is prefixed with a question mark "?". In that case, the simple namespace filter will probably miss it. But, I didn't want to make the function any more complex than it already is.

3 comments:

  1. 1. The second line (wet_run check) looks redundant. Just use "if (wet_run)" later on instead of "if (wet_run == true)" - it works with undefined values (interpreted as false).

    2. The first line looks puzzling - why does 'ns' depend on whether 'wet_run' was passed? Did you mean to write 'typeof(ns)' instead of 'typeof(wet_run)'?

    ReplyDelete
  2. Hey Ron :)
    You're right on both accounts, of course. Bugs fixed.

    ReplyDelete
  3. Well, one of them wasn't a bug, but still...

    ReplyDelete