I have this Node app here, which makes a request to an api to get some stock data, loops through that stock data and then writes it to a Mongo DB every 5 minutes. I am running it on a Digital Ocean server with PM2 as a service. However, after letting it run a for a day I don't see any data in the DB. I don't see any signs that the service wasn't running or had any errors so I'm wondering if maybe it is because a PM 2 service can't write to the DB? Maybe a ports issue? Any guidance would be awesome!
// -- 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 Logs:
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
I have pm2 running on a server with mongoose/MongoDB and no problems. It looks like it might be with how you are handling your creation of a mongoose model.
Try doing:
StockMoment.create({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
https://mongoosejs.com/docs/models.html
You shouldn't have to use the save when you initially create a document.
Also, I don't know how familiar you are with mongo, but make sure you select the database with use stock-test
when in the console. On the off change that you are using python to read your database, you might also run into an issue with the "-" due to how python handles that character in its dictionaries.