节点应用程序可以在作为 PM2 服务 运行 时连接/写入 MongoDB 吗?
Can a node app connect / write to MongoDB while being run as PM2 service?
我这里有这个 Node 应用程序,它向 api 发出请求以获取一些股票数据,循环遍历该股票数据,然后每 5 分钟将其写入 Mongo 数据库。我 运行 在 Digital Ocean 服务器上使用 PM2 作为服务。但是,让它 运行 一天后,我在数据库中看不到任何数据。我没有看到任何迹象表明该服务未 运行ning 或有任何错误,所以我想知道是否可能是因为 PM 2 服务无法写入数据库?也许是端口问题?任何指导都会很棒!
// -- Dependancies --
// Request and MongoDB
const request = require('request');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Connect to database
mongoose.connect('mongodb://127.0.0.1:27017/stock-test')
.then(() => {
console.log('connected');
})
.catch(err => {
console.error(err);
});
// Setup stockMoment Schema
const StockMomentSchema = new Schema({
symbol: String,
price: Number,
size: Number,
time: {type: Date, default: Date.now}
});
// Create stockMoment model
StockMoment = mongoose.model('stockMoment', StockMomentSchema, 'stockPriceData');
// Setup Variables
const d = new Date();
let day = d.getDay()
let hour = d.getHours();
// Check time, run getMarketData function if market is open
function makeRequest() {
if(day >= 1 && day <= 5) {
if(hour >= 11 && hour <= 16) {
getMarketData();
}
}
}
// Run request to get data, store data in MongoDB database
function getMarketData() {
request({
url: 'https://api.iextrading.com/1.0/tops/last',
json: true
}, (err, res, body) => {
if(err) {
return console.error(err);
}
for(let i = 0; i < body.length; i++) {
const stockMoment = new StockMoment({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
stockMoment.save((err) => {
if(err) return handleError(err);
console.log('Saved!', i);
});
console.log(body[i]);
}
});
}
setInterval(makeRequest, 300000);
PM2 日志:
PM2 | [2018-08-27T23:05:42.936Z] PM2 log: RPC socket file : /home/evadmin/.pm2/rpc.sock
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: BUS socket file : /home/evadmin/.pm2/pub.sock
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Application log path : /home/evadmin/.pm2/logs
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Process dump file : /home/evadmin/.pm2/dump.pm2
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Concurrent actions : 2
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: SIGTERM timeout : 1600
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: ===============================================================================
PM2 | [2018-08-27T23:05:42.972Z] PM2 log: Starting execution sequence in -fork mode- for app name:priceTimeSeries id:0
PM2 | [2018-08-27T23:05:42.977Z] PM2 log: App name:priceTimeSeries id:0 online
PM2 | [2018-08-27T23:08:02.026Z] PM2 log: Process 0 in a stopped status, starting it
PM2 | [2018-08-27T23:08:02.027Z] PM2 log: Stopping app:priceTimeSeries id:0
PM2 | [2018-08-27T23:08:02.042Z] PM2 log: App [priceTimeSeries] with id [0] and pid [1927], exited with code [0] via signal [SIGINT]
PM2 | [2018-08-27T23:08:02.139Z] PM2 log: pid=1927 msg=process killed
PM2 | [2018-08-27T23:08:02.140Z] PM2 log: Starting execution sequence in -fork mode- for app name:priceTimeSeries id:0
PM2 | [2018-08-27T23:08:02.145Z] PM2 log: App name:priceTimeSeries id:0 online
/home/evadmin/.pm2/logs/priceTimeSeries-error.log last 15 lines:
0|priceTim | (node:1927) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
0|priceTim | (node:2001) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/evadmin/.pm2/logs/priceTimeSeries-out.log last 15 lines:
0|priceTim | connected
0|priceTim | connected
我在 mongoose/MongoDB 的服务器上安装了 pm2 运行ning,没有问题。看起来这可能与您处理创建 mongoose 模型的方式有关。
尝试做:
StockMoment.create({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
https://mongoosejs.com/docs/models.html
最初创建文档时不必使用保存。
此外,我不知道您对 mongo 的熟悉程度,但请确保您在控制台中使用 use stock-test
select 数据库。如果您正在使用 python 读取数据库,您可能还会 运行 遇到“-”的问题,因为 python 在其字典中处理该字符的方式。
我这里有这个 Node 应用程序,它向 api 发出请求以获取一些股票数据,循环遍历该股票数据,然后每 5 分钟将其写入 Mongo 数据库。我 运行 在 Digital Ocean 服务器上使用 PM2 作为服务。但是,让它 运行 一天后,我在数据库中看不到任何数据。我没有看到任何迹象表明该服务未 运行ning 或有任何错误,所以我想知道是否可能是因为 PM 2 服务无法写入数据库?也许是端口问题?任何指导都会很棒!
// -- Dependancies --
// Request and MongoDB
const request = require('request');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Connect to database
mongoose.connect('mongodb://127.0.0.1:27017/stock-test')
.then(() => {
console.log('connected');
})
.catch(err => {
console.error(err);
});
// Setup stockMoment Schema
const StockMomentSchema = new Schema({
symbol: String,
price: Number,
size: Number,
time: {type: Date, default: Date.now}
});
// Create stockMoment model
StockMoment = mongoose.model('stockMoment', StockMomentSchema, 'stockPriceData');
// Setup Variables
const d = new Date();
let day = d.getDay()
let hour = d.getHours();
// Check time, run getMarketData function if market is open
function makeRequest() {
if(day >= 1 && day <= 5) {
if(hour >= 11 && hour <= 16) {
getMarketData();
}
}
}
// Run request to get data, store data in MongoDB database
function getMarketData() {
request({
url: 'https://api.iextrading.com/1.0/tops/last',
json: true
}, (err, res, body) => {
if(err) {
return console.error(err);
}
for(let i = 0; i < body.length; i++) {
const stockMoment = new StockMoment({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
stockMoment.save((err) => {
if(err) return handleError(err);
console.log('Saved!', i);
});
console.log(body[i]);
}
});
}
setInterval(makeRequest, 300000);
PM2 日志:
PM2 | [2018-08-27T23:05:42.936Z] PM2 log: RPC socket file : /home/evadmin/.pm2/rpc.sock
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: BUS socket file : /home/evadmin/.pm2/pub.sock
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Application log path : /home/evadmin/.pm2/logs
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Process dump file : /home/evadmin/.pm2/dump.pm2
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: Concurrent actions : 2
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: SIGTERM timeout : 1600
PM2 | [2018-08-27T23:05:42.937Z] PM2 log: ===============================================================================
PM2 | [2018-08-27T23:05:42.972Z] PM2 log: Starting execution sequence in -fork mode- for app name:priceTimeSeries id:0
PM2 | [2018-08-27T23:05:42.977Z] PM2 log: App name:priceTimeSeries id:0 online
PM2 | [2018-08-27T23:08:02.026Z] PM2 log: Process 0 in a stopped status, starting it
PM2 | [2018-08-27T23:08:02.027Z] PM2 log: Stopping app:priceTimeSeries id:0
PM2 | [2018-08-27T23:08:02.042Z] PM2 log: App [priceTimeSeries] with id [0] and pid [1927], exited with code [0] via signal [SIGINT]
PM2 | [2018-08-27T23:08:02.139Z] PM2 log: pid=1927 msg=process killed
PM2 | [2018-08-27T23:08:02.140Z] PM2 log: Starting execution sequence in -fork mode- for app name:priceTimeSeries id:0
PM2 | [2018-08-27T23:08:02.145Z] PM2 log: App name:priceTimeSeries id:0 online
/home/evadmin/.pm2/logs/priceTimeSeries-error.log last 15 lines:
0|priceTim | (node:1927) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
0|priceTim | (node:2001) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/evadmin/.pm2/logs/priceTimeSeries-out.log last 15 lines:
0|priceTim | connected
0|priceTim | connected
我在 mongoose/MongoDB 的服务器上安装了 pm2 运行ning,没有问题。看起来这可能与您处理创建 mongoose 模型的方式有关。
尝试做:
StockMoment.create({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
https://mongoosejs.com/docs/models.html
最初创建文档时不必使用保存。
此外,我不知道您对 mongo 的熟悉程度,但请确保您在控制台中使用 use stock-test
select 数据库。如果您正在使用 python 读取数据库,您可能还会 运行 遇到“-”的问题,因为 python 在其字典中处理该字符的方式。