Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   c++ check if service is running (http://www.velocityreviews.com/forums/t953242-c-check-if-service-is-running.html)

Mick 10-10-2012 10:51 PM

c++ check if service is running
 
Hi there,

I posted this in mongodb-user, but I think it's more of a C++ question, so sorry for the double-post...

I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6 database.

If I try to get a ScopedDbConnection, but mongod is not running for some reason, it throws

"First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++ exception: mongo::SocketException at memory location 0x0057ead8"

Is there any way to check if either of those 2 are running (mongod or MongoDB) before trying to get a ScopedDbConnection?

Thank you in advance.

Melzzzzz 10-10-2012 11:01 PM

Re: c++ check if service is running
 
On Wed, 10 Oct 2012 15:51:14 -0700 (PDT)
Mick <m.d.berceanu@gmail.com> wrote:

> Hi there,
>
> I posted this in mongodb-user, but I think it's more of a C++
> question, so sorry for the double-post...
>
> I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6
> database.
>
> If I try to get a ScopedDbConnection, but mongod is not running for
> some reason, it throws
>
> "First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++
> exception: mongo::SocketException at memory location 0x0057ead8"
>
> Is there any way to check if either of those 2 are running (mongod or
> MongoDB) before trying to get a ScopedDbConnection?


You already have it. With exception you know it's not running.
So catch exception, report and exit.




goran.pusic@gmail.com 10-11-2012 02:11 PM

Re: c++ check if service is running
 
On Thursday, October 11, 2012 12:51:14 AM UTC+2, Mick wrote:
> Hi there,
>
>
>
> I posted this in mongodb-user, but I think it's more of a C++ question, so sorry for the double-post...
>
>
>
> I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6 database.
>
>
>
> If I try to get a ScopedDbConnection, but mongod is not running for some reason, it throws
>
>
>
> "First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++ exception: mongo::SocketException at memory location 0x0057ead8"
>
>
>
> Is there any way to check if either of those 2 are running (mongod or MongoDB) before trying to get a ScopedDbConnection?


Maybe, but it's a bad idea. It's akin to going to a supermarket to see whether there's milk, going back home to get the money, then going to the supermarket again to buy it.

What you should do instead is to catch the exception, inspect it, and report the error. E.g. (I don't know anything about mongo, so I might be way offbase here).

try
{
ScopedDbConnection db(...);
use(db); // lots of code here ;-)
}
catch(const mongo::SocketException& e)
{
// inspect e._type here to see what to do next.
}

Another reason why trying to see whether a "service is running" (not mongo,ANYTHING) is that it is surprisingly hard to do well. It's hard because there's literally thousands of reasons why you can't connect. If that is so, it is by far the best to actually try to connect, and report to the user anything you have in the line of info (in this case e.g. toString() and the address you tried to reach if it isn't in exception object already).

Note that the approach like this: (don't know if possible here, but have seen people doing this):

auto_ptr<ScopedConnection> P;
try
{
P = auto_ptr<ScopedConnection>(new ScopedConnection(...));
}
catch(whatever)
{
reporterror;
}

if (P)
{ // mongo alive, yay!
use(*P);
}

is an abomination. It's ugly, and you still need another try/catch to report errors that will eventually come from use.

So... Don't detect anything. Fail eagerly, loudly and clearly instead.

HTH,

Goran.


All times are GMT. The time now is 10:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.