admin管理员组文章数量:1414939
I have a little web app which serves data from a Mongo database. I have configured 2 cron jobs (through Heroku scheduler) to run everyday and manipulate a remote db. The problem is that I need those jobs to conclude and not keep running after finishing, that means closing my connection, otherwhise my function keeps running.
When I call mongoose.disconnect()
in one of my files I get Mongo error: Connection pool closed
.
This is the problematic file:
require('dotenv').config();
const malagaCulturaScrapper = require("../scrappers/malagaCultura");
const Event = require("../schemas/eventSchema");
const mongoose = require("mongoose");
(async () => {
try {
await mongoose.connect((process.env.DEV_MODE === "true") ? process.env.SAMPLEDB_URL:process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
} catch (err) {
console.log('Error: ' + err)
}
})()
const scrapeAndSave = async() =>{
await Event.deleteMany({}).exec();
await malagaCulturaScrapper.malagaCulturaScrapper();
mongoose.disconnect();
}
scrapeAndSave();
Which calls this function:
const malagaCulturaScrapper = async() =>{
await scrapeIt("/", {
events: {
listItem: "article",
data: {
title: "h4",
time: ".mec-event-time",
date: {
closest: "li",
attr: "id"
},
event_img: {
selector: ".attachment-thumbnail",
attr: "src"
},
event_link:{
selector: "h4 a",
attr: "href",
},
location: ".mec-event-loc-place",
}
}
}).then(({ data, response }) => {
for(const index in data.events){
let newEvent = new Event({
_id: new mongoose.Types.ObjectId(),
title: data.events[index].title,
dateTime: createDateObject(data.events[index].date.substring(59), data.events[index].time),
event_img: data.events[index].event_img,
event_link: data.events[index].event_link,
location: data.events[index].location,
})
Event.addEvents(newEvent);
}
})
}
And this is my adding function on the schema:
Event.addEvents = async(newEvent) => {
await Event.findOne({
location: newEvent.location || {
'$regex': newEvent.location, $options: 'i',
},
dateTime: { $in: newEvent.dateTime }
}, (err, event) => {
if(err) throw err;
if (!event && !helpers.isPastEvent(newEvent.dateTime[0], Date.now()) && new Date(newEvent.dateTime[0]).getFullYear() === new Date(Date.now()).getFullYear()) {
newEvent.save((err) => {
if (err) throw err;
})
}
})
}
Lastly, the stacktrace I get when running the file:
/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/helpers/promiseOrCallback.js:19
throw error;
^
MongoError: connection pool closed
at ConnectionPool.close (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection_pool.js:300:34)
at Server.destroy (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:213:17)
at destroyServer (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:806:10)
at eachAsync (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:353:25)
at eachAsync (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/utils.js:131:5)
at s.sessionPool.endAllPooledSessions (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:351:7)
at topology.endSessions (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sessions.js:592:13)
at mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:501:45)
at cb (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:681:26)
at fn (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection_pool.js:350:13)
at Object.handleOperationResult [as cb] (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:558:5)
at Connection.write (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection.js:378:26)
at _mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/wireprotocol/mand.js:120:10)
at Objectmand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/wireprotocol/mand.js:28:5)
at Connectionmand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection.js:171:8)
at s.pool.withConnection (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:285:12)
Emitted 'error' event at:
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/model.js:4844:13
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/helpers/promiseOrCallback.js:16:11
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/model.js:4865:21
at (anonymous function).call (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/query.js:4411:18)
at Immediate.Query.base.findOne.call (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/query.js:2145:7)
at Immediate.<anonymous> (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mquery/lib/utils.js:116:16)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
I would really appreciate any guidance or help on the subject. Thanks in advance.
I have a little web app which serves data from a Mongo database. I have configured 2 cron jobs (through Heroku scheduler) to run everyday and manipulate a remote db. The problem is that I need those jobs to conclude and not keep running after finishing, that means closing my connection, otherwhise my function keeps running.
When I call mongoose.disconnect()
in one of my files I get Mongo error: Connection pool closed
.
This is the problematic file:
require('dotenv').config();
const malagaCulturaScrapper = require("../scrappers/malagaCultura");
const Event = require("../schemas/eventSchema");
const mongoose = require("mongoose");
(async () => {
try {
await mongoose.connect((process.env.DEV_MODE === "true") ? process.env.SAMPLEDB_URL:process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
} catch (err) {
console.log('Error: ' + err)
}
})()
const scrapeAndSave = async() =>{
await Event.deleteMany({}).exec();
await malagaCulturaScrapper.malagaCulturaScrapper();
mongoose.disconnect();
}
scrapeAndSave();
Which calls this function:
const malagaCulturaScrapper = async() =>{
await scrapeIt("https://malagadecultura./agenda/", {
events: {
listItem: "article",
data: {
title: "h4",
time: ".mec-event-time",
date: {
closest: "li",
attr: "id"
},
event_img: {
selector: ".attachment-thumbnail",
attr: "src"
},
event_link:{
selector: "h4 a",
attr: "href",
},
location: ".mec-event-loc-place",
}
}
}).then(({ data, response }) => {
for(const index in data.events){
let newEvent = new Event({
_id: new mongoose.Types.ObjectId(),
title: data.events[index].title,
dateTime: createDateObject(data.events[index].date.substring(59), data.events[index].time),
event_img: data.events[index].event_img,
event_link: data.events[index].event_link,
location: data.events[index].location,
})
Event.addEvents(newEvent);
}
})
}
And this is my adding function on the schema:
Event.addEvents = async(newEvent) => {
await Event.findOne({
location: newEvent.location || {
'$regex': newEvent.location, $options: 'i',
},
dateTime: { $in: newEvent.dateTime }
}, (err, event) => {
if(err) throw err;
if (!event && !helpers.isPastEvent(newEvent.dateTime[0], Date.now()) && new Date(newEvent.dateTime[0]).getFullYear() === new Date(Date.now()).getFullYear()) {
newEvent.save((err) => {
if (err) throw err;
})
}
})
}
Lastly, the stacktrace I get when running the file:
/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/helpers/promiseOrCallback.js:19
throw error;
^
MongoError: connection pool closed
at ConnectionPool.close (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection_pool.js:300:34)
at Server.destroy (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:213:17)
at destroyServer (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:806:10)
at eachAsync (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:353:25)
at eachAsync (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/utils.js:131:5)
at s.sessionPool.endAllPooledSessions (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:351:7)
at topology.endSessions (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sessions.js:592:13)
at mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:501:45)
at cb (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/topology.js:681:26)
at fn (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection_pool.js:350:13)
at Object.handleOperationResult [as cb] (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:558:5)
at Connection.write (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection.js:378:26)
at _mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/wireprotocol/mand.js:120:10)
at Object.mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/wireprotocol/mand.js:28:5)
at Connection.mand (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/cmap/connection.js:171:8)
at s.pool.withConnection (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongodb/lib/core/sdam/server.js:285:12)
Emitted 'error' event at:
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/model.js:4844:13
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/helpers/promiseOrCallback.js:16:11
at /home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/model.js:4865:21
at (anonymous function).call (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/query.js:4411:18)
at Immediate.Query.base.findOne.call (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mongoose/lib/query.js:2145:7)
at Immediate.<anonymous> (/home/roma/Escritorio/Programming-projects/mlg-events/node_modules/mquery/lib/utils.js:116:16)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
I would really appreciate any guidance or help on the subject. Thanks in advance.
Share Improve this question edited Nov 18, 2020 at 0:24 Carlos Molero asked Nov 17, 2020 at 23:23 Carlos MoleroCarlos Molero 731 gold badge2 silver badges9 bronze badges 4-
The example code has
mongoose.connection.close()
. Have you triedawait mongoose.disconnect()
as well? – Matt Commented Nov 18, 2020 at 0:17 - Just tried, same result, weird thing since I have another file which is for sending daily mails which uses a connection to the db and closes fine. My guess is something is happening in my scrapping or event saving function. Because leaving deleteMany() alone won't cause the error. – Carlos Molero Commented Nov 18, 2020 at 0:23
-
This is likely caused by what appears to be a long running process. In time, TCP times out and the connection is closed. Use the
keepAlive
option in connection settings object to fix this. – Randy Casburn Commented Nov 18, 2020 at 0:47 - Oh yeah, you're mixing callbacks and async in addEvents, and also not waiting for it in malagaCulturaScrapper – Matt Commented Nov 18, 2020 at 9:21
1 Answer
Reset to default 4The code is trying to disconnect before the queries have pleted.
Use the Promise API to mongoose by not supplying any callback functions and make sure you await
promises so the disconnect
run's after everything has pleted.
const malagaCulturaScrapper = async() =>{
const { data, response } = await scrapeIt("https://malagadecultura./agenda/", {
...eventsInfo
})
for(const index in data.events){
let newEvent = new Event({
_id: new mongoose.Types.ObjectId(),
title: data.events[index].title,
dateTime: createDateObject(data.events[index].date.substring(59), data.events[index].time),
event_img: data.events[index].event_img,
event_link: data.events[index].event_link,
location: data.events[index].location,
})
await Event.addEvents(newEvent);
}
}
Event.addEvents = async(newEvent) => {
const event = await Event.findOne({
location: newEvent.location || {
'$regex': newEvent.location, $options: 'i',
},
dateTime: { $in: newEvent.dateTime }
})
if (!event && !helpers.isPastEvent(newEvent.dateTime[0], Date.now()) && new Date(newEvent.dateTime[0]).getFullYear() === new Date(Date.now()).getFullYear()) {
await newEvent.save()
})
}
本文标签: javascriptConnection pool is closed using MongooseStack Overflow
版权声明:本文标题:javascript - Connection pool is closed using Mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745176741a2646274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论