我这里有这个Node应用程序,它向api发出请求以获取一些库存数据,循环遍历该库存数据,然后每5分钟将其写入Mongo数据库。我正在将PM2作为服务在Digital Ocean服务器上运行它。但是,让它运行了一天之后,我在数据库中看不到任何数据。我没有看到任何迹象表明该服务未运行或有任何错误,所以我想知道是否可能是因为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
答案 0 :(得分:0)
我在具有mongoose / MongoDB的服务器上运行pm2,没有问题。看起来可能与如何处理猫鼬模型有关。
尝试做:
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
选择数据库。在使用python读取数据库的更改中,由于python如何在其字典中处理该字符,因此您也可能会遇到“-”问题。