Check mongo status on Meteor? - mongodb

I'm trying to create an alarm system for my application, that will trigger when one of the services (e.g. MongoDB) is not working.
What I'm doing is, once the application is started, I shut down my MongoDB server and try to connect to it, but instead of receiving an error my application just gets stuck into the execution of the method. The server console looks like something is in execution.
My current code (coffeescript) is:
checkMongoService: ()->
mongo = Npm.require 'mongodb'
assert = Npm.require 'assert'
url = 'mongodb://....'
mongo.connect url, (err, db) ->
assert.equal null, err
console.log 'Connected correctly to server'
db.close()
return
I've also been trying by doing a simple
Meteor.users.find().count();
or using MongoInternals with
testConnection = new MongoInternals.RemoteCollectionDriver("mongodb://...);
but still same issue, when mongo is not running no error is thrown and the console stops to work. If then I start Mongo again, it will just return the result (in this case the log 'Connected correctly to server')
Something that I've noticed is if I try with meteor shell to execute testConnection = new MongoInternals.RemoteCollectionDriver("mongodb://...); I get an error "Error: failed to connect to [127.0.0.1:27017]"
TL;DR
Do you might have an idea on how I can check if mongo is reachable or do you know if I'm doing something wrong with the code above?

Try setting the timeouts to be a bit shorter than the default 30 seconds:
mongo.connect(url, {
connectTimeoutMS: 1000,
socketTimeoutMS: 1000,
reconnectTries: 1
}, function(err, db) {...}
(Full set of connection params are here)

Meteor.status().status
from the docs
This method returns the status of the connection between the client and the server. The return value is an object with the following fields:
connected (Boolean)
True if currently connected to the server. If false, changes and method invocations will be queued up until the connection is reestablished.
status (String)
Blockquote
Describes the current reconnection status. The possible values are connected (the connection is up and running), connecting (disconnected and trying to open a new connection), failed (permanently failed to connect; e.g., the client and server support different versions of DDP), waiting (failed to connect and waiting to try to reconnect) and offline (user has disconnected the connection).
https://docs.meteor.com/api/connections.html

Related

How to test mongodb save() method failure?

I have a working script, and in my node code, I have instances of save(), like the following:
user.remove(function(error, removedMeasurement) {
if (error) {...} else {...}
});
I would like to confirm that something is done when handling an error during the save(). I tried killing my connection to the db after I started the process, but then the process just hangs without error.
My connection to the db is similar to
db = mongoose.createConnection(connectionToDb, options);
and the options include:
reconnectTries:60,
reconnectInterval:2000
If I restart the db connection right away, the script will continue, so I think the above attribute options have something to do with this, and so killing the db connection does not help trigger the error to confirm I am handling it correctly.

Connection to mongoose causes ECONNREFUSE [duplicate]

I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB.
I try to connect on an remote server, but its throws me error
Here's my code (there's no user/password set in the mongoDB) :
var url = "mongodb://192.168.1.5:27017/DB"
MongoClient.connect(url, function(err, db) {
console.log("test")
if (!err) {
console.log("test");
}
else {
console.dir(err)
throw err
}
// db.close();
});
And here's when I launch the server and I tried to launch the app in a navigator :
Server listening at port 80:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error
at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19)
at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9)
at g (events.js:180:16)
at emit (events.js:98:17)
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
at g (events.js:180:16)
This error is returned for several errors like :
server is not running
you need to authenticate user
this database does not exists
the mongodb port is not the default port
Check this. Normaly your problem is just one of these causes
For this to work you have to make the changes in /etc/mongod.conf
comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only.
One of the solutions is to change 127.0.0.1 to public ip or whatever the router provided you with in mongodb config file located in /etc
There could be few reasons for ECONNREFUSED error.
Checkpoints
Check if your port is not serving any other process.
Check if your mongod is running.
Check if you localHost is configured correctly with your id or not.
for more you can see this

Unable to execute mongo:find function in erlang

Using mongodb erlang driver my program able to connect mongodb database server.
After successful connection I tried to all the documents present in the collection.
I tried the below code:
mongo:do (safe, master, Conn, graph, fun() ->
process(mongo:find (pedgedata0, {src,1})) end),
here 'graph' is dbname and 'pedgedata0' is collection.
I tried with different options like
mongo:do (safe, master, Conn, 'graph', fun() ->
process(mongo:find ('pedgedata0', {})) end),
* here I tried with single quotes for db and collection name and also specified an empty condition.
However I am getting below error:
Conn is : <0.104.0>
** exception exit: undef
in function bson_binary:put_cstring/1
called as bson_binary:put_cstring(<<"graph.pedgedata0">>)
in call from mongo_protocol:put_message/3
in call from mongo_connection:encode_request/2
in call from mongo_connection:handle_call/3
in call from gen_server:handle_msg/5
in call from proc_lib:init_p_do_apply/3
I am running this program from erlide(Eclipse addon for erlang).
Please help me to resolve this problem
Please use the newer version of driver: https://github.com/comtihon/mongodb-erlang
There are renewed interfaces for mongo operations.

Connecting to a MongoDb in Node.js Error

Quite simply, I'm trying to connect to a MongoDB via Node.js:
Db = require('../v2/node_modules/mongodb').Db
Connection = require('../v2/node_modules/mongodb').Connection
Server = require('../v2/node_modules/mongodb').Server
console.log "before"
DbServer = new Server("localhost", 27017, {})
db = new Db("twitter", DbServer, {native_parser:true})
console.log "after"
return
That's my code and it's as simple as it gets. My output, however, seems to stop at the db = new Db... line.
It never gets to the after. It doesn't give an error either. I know I have a DB running and when I fire up MongoHub, it's there along with the twitter database
Just remove the native_parser=true would be ok
Native bson parser not compiled, please compile or avoid using native_parser=true

nodejs chat example does not work

I've come across a node chat example on github, When I try to run it, I see the following error:
Error connecting to mongo perhaps it isn't running ?
I've installed mongo 0.9.2, nodejs 5.2 pre, npm 3.0 and other dependencies. The example can be found here: https://github.com/gregstewart/chat.io
I can not determine whether if the example not really works or I didn't run it right. Please help.
Did you install and start mongo-db on your system? This error is mostly because of a missing mongo instance running on the local machine.
Check out the follwing code excerpts from chat.io.
main.js:
/**
* Configure the user provider (mongodB connection for user data storage)
*/
var userProvider = new UserProvider('localhost', 27017);
Creates a new UserProvider object using host and port for database (localhost:27017, mongo-db default).
UserProvider.js:
UserProvider = function(host, port) {
this.db = new mongo.Db('node-mongo-chat', new Server(host, port, {auto_reconnect: true}, {}));
this.db.addListener('error', function(error) {
console.log('Error connecting to mongo -- perhaps it isn\'t running?');
});
this.db.open(function() {
});
};
Opening the connection to the server, printing out an error on failure (the error you mentioned above).
Consider reading up on the mongo-db docs concerning installation and setup here