{"action":"created","issue":{"url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/34","repository_url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb","labels_url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/34/labels{/name}","comments_url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/34/comments","events_url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/34/events","html_url":"https://github.com/holidayextras/jsonapi-store-mongodb/issues/34","id":164739405,"number":34,"title":"Mongo timeouts not handled","user":{"login":"jbockerstette","id":16734533,"avatar_url":"https://avatars.githubusercontent.com/u/16734533?v=3","gravatar_id":"","url":"https://api.github.com/users/jbockerstette","html_url":"https://github.com/jbockerstette","followers_url":"https://api.github.com/users/jbockerstette/followers","following_url":"https://api.github.com/users/jbockerstette/following{/other_user}","gists_url":"https://api.github.com/users/jbockerstette/gists{/gist_id}","starred_url":"https://api.github.com/users/jbockerstette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbockerstette/subscriptions","organizations_url":"https://api.github.com/users/jbockerstette/orgs","repos_url":"https://api.github.com/users/jbockerstette/repos","events_url":"https://api.github.com/users/jbockerstette/events{/privacy}","received_events_url":"https://api.github.com/users/jbockerstette/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-10T23:19:18Z","updated_at":"2016-07-12T00:33:37Z","closed_at":null,"body":"For some reason, I am not sure why, but the Centos boxes I am installing on have a 2 minute timeout on mongo connections. When mongo times out, the rest api cannot recover and just hangs. I spent hours trying to fix this problem by adjusting various timeouts to no avail. The way I fixed it is by listening for the timeout and then closing and reconnecting. You must close any open db connections or you will leak them. You cannot just forget about them. I am providing my fix. Here you go....\r\n\r\nIn mongoHandler.js\r\n\r\n```\r\n/**\r\n Initialise gets invoked once for each resource that uses this handler.\r\n */\r\nMongoStore.prototype.initialise = function(resourceConfig) {\r\n  var self = this;\r\n  if (!self._config.url) {\r\n    return console.error(\"MongoDB url missing from configuration\");\r\n  }\r\n  self.resourceConfig = resourceConfig;\r\n  self.relationshipAttributeNames = MongoStore._getRelationshipAttributeNames(resourceConfig.attributes);\r\n  mongodb.MongoClient.connect(self._config.url, {\r\n    server: {\r\n      reconnectTries: 999999999,\r\n      reconnectInterval: 5000\r\n    }\r\n  }).then(function(db) {\r\n    self._db = db;\r\n    self._db.on(\"timeout\", function() {\r\n      self.ready = false;\r\n      self._db.close();\r\n      console.error(\"mongodb connection timeout.\");\r\n      self.initialise(resourceConfig);\r\n    });\r\n    self._db.on(\"close\", function(err) {\r\n      console.error(\"mongodb connection closed: \", err === undefined ? \"timeout\" : err.message);\r\n      self.ready = false;\r\n      self._db.collection(\"Nope\").findOne({ _id: 0 }, { _id: 0 }, function() {\r\n        console.error(\"mongodb connection is back\");\r\n        self.ready = true;\r\n      });\r\n    });\r\n  }).catch(function(err) {\r\n    console.error(\"mongodb connection failed:\", err.message);\r\n    setTimeout(function() {\r\n      self.initialise(resourceConfig);\r\n    }, 5000);\r\n  }).then(function() {\r\n    var resourceName = resourceConfig.resource;\r\n    var collection = self._db.collection(resourceName);\r\n    self._createIndexesForRelationships(collection, self.relationshipAttributeNames);\r\n    self.ready = true;\r\n  });\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/comments/231905813","html_url":"https://github.com/holidayextras/jsonapi-store-mongodb/issues/34#issuecomment-231905813","issue_url":"https://api.github.com/repos/holidayextras/jsonapi-store-mongodb/issues/34","id":231905813,"user":{"login":"pmcnr-hx","id":10811943,"avatar_url":"https://avatars.githubusercontent.com/u/10811943?v=3","gravatar_id":"","url":"https://api.github.com/users/pmcnr-hx","html_url":"https://github.com/pmcnr-hx","followers_url":"https://api.github.com/users/pmcnr-hx/followers","following_url":"https://api.github.com/users/pmcnr-hx/following{/other_user}","gists_url":"https://api.github.com/users/pmcnr-hx/gists{/gist_id}","starred_url":"https://api.github.com/users/pmcnr-hx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pmcnr-hx/subscriptions","organizations_url":"https://api.github.com/users/pmcnr-hx/orgs","repos_url":"https://api.github.com/users/pmcnr-hx/repos","events_url":"https://api.github.com/users/pmcnr-hx/events{/privacy}","received_events_url":"https://api.github.com/users/pmcnr-hx/received_events","type":"User","site_admin":false},"created_at":"2016-07-12T00:33:37Z","updated_at":"2016-07-12T00:33:37Z","body":"Handling the timeout is the right thing to do. I'll look into creating a PR to include this. Thank you for your report."}}
{"actor":{"display_login":"pmcnr-hx"}}