I am trying to write a Perl5 script that checks the status of the MongoDB server every minute and notifies me if it does not work. Any suggestions are welcome for this task. I am currently using the "MongoDB" and "MongoDB :: MongoClient" modules to establish a connection to find out if the server is accessible. Here is the basic idea:
while(1) { my $conn = connectMongoDB(); if($conn){ sleep 60; } else{ sendMail(); last; } } sub connectMongoDB { my $client; eval{ $client = MongoDB::MongoClient->new( host => "mongodb://:\@$server");}; return ($client)?1:0; }
The main problem is that there is no way to disconnect from the server. Here is what he says on the cpan page:
"It is not possible to explicitly disconnect from the database. However, the connection will be automatically closed and cleared if there are no references to the MongoDB :: MongoClient object that occurs when $ client leaves the scope (or earlier if you did not define it using undef).
I tried 'undef' and routines. None of them terminate the connection. Due to the cycle, the number of connections continues to grow. Is there any other method that I can try to control the number of connections?
Any other suggestion to solve this problem is welcome if it is not related to crontab.
Thanks.
EDIT:
This is the conclusion when the sleep time is set to 2 seconds, where the version is db v2.4.4 and the version of the MongoDB module is 0.701.4. The problem persists after upgrading the MongoDB module to 0.702.1.
Fri Aug 16 20:33:06.986 [initandlisten] connection accepted from 127.0.0.1:51031 #3 (1 connection now open) Fri Aug 16 20:33:08.989 [initandlisten] connection accepted from 127.0.0.1:51033 #4 (2 connections now open) Fri Aug 16 20:33:10.991 [initandlisten] connection accepted from 127.0.0.1:51034 #5 (3 connections now open) Fri Aug 16 20:33:12.994 [initandlisten] connection accepted from 127.0.0.1:51035 #6 (4 connections now open) Fri Aug 16 20:33:14.996 [initandlisten] connection accepted from 127.0.0.1:51036 #7 (5 connections now open) Fri Aug 16 20:33:16.999 [initandlisten] connection accepted from 127.0.0.1:51038 #8 (6 connections now open) Fri Aug 16 20:33:19.003 [initandlisten] connection accepted from 127.0.0.1:51039 #9 (7 connections now open) Fri Aug 16 20:33:21.006 [initandlisten] connection accepted from 127.0.0.1:51040 #10 (8 connections now open) Fri Aug 16 20:33:23.009 [initandlisten] connection accepted from 127.0.0.1:51042 #11 (9 connections now open) Fri Aug 16 20:33:25.013 [initandlisten] connection accepted from 127.0.0.1:51043 #12 (10 connections now open) Fri Aug 16 20:33:27.016 [initandlisten] connection accepted from 127.0.0.1:51044 #13 (11 connections now open) Fri Aug 16 20:33:29.019 [initandlisten] connection accepted from 127.0.0.1:51045 #14 (12 connections now open) Fri Aug 16 20:33:31.022 [initandlisten] connection accepted from 127.0.0.1:51047 #15 (13 connections now open)
EDIT 2: SOLVED !!
I believe the problem is with the auto_connect option. Once it is disabled, the code works fine. Below is something that works now.
sub connectMongoDB { my $client; eval{ $client = MongoDB::MongoClient->new( host => "mongodb://:\@$server", auto_connect => 0); $client->connect; }; return ( !$@ )?1:0; }
The best
source share