00001 // @file multicmd.h 00002 00019 #pragma once 00020 00021 #include "../../util/background.h" 00022 #include "connections.h" 00023 00024 namespace mongo { 00025 00026 struct Target { 00027 Target(string hostport) : toHost(hostport), ok(false) { } 00028 Target() : ok(false) { } 00029 string toHost; 00030 bool ok; 00031 BSONObj result; 00032 }; 00033 00034 /* -- implementation ------------- */ 00035 00036 class _MultiCommandJob : public BackgroundJob { 00037 public: 00038 BSONObj& cmd; 00039 Target& d; 00040 _MultiCommandJob(BSONObj& _cmd, Target& _d) : cmd(_cmd), d(_d) { } 00041 00042 private: 00043 string name() const { return "MultiCommandJob"; } 00044 void run() { 00045 try { 00046 ScopedConn c(d.toHost); 00047 d.ok = c.runCommand("admin", cmd, d.result); 00048 } 00049 catch(DBException&) { 00050 DEV log() << "dev caught dbexception on multiCommand " << d.toHost << rsLog; 00051 } 00052 } 00053 }; 00054 00055 inline void multiCommand(BSONObj cmd, list<Target>& L) { 00056 list<BackgroundJob *> jobs; 00057 00058 for( list<Target>::iterator i = L.begin(); i != L.end(); i++ ) { 00059 Target& d = *i; 00060 _MultiCommandJob *j = new _MultiCommandJob(cmd, d); 00061 j->go(); 00062 jobs.push_back(j); 00063 } 00064 00065 for( list<BackgroundJob*>::iterator i = jobs.begin(); i != jobs.end(); i++ ) { 00066 (*i)->wait(); 00067 } 00068 } 00069 }