Hello,
The last week I try this code to work. It is not possible to update the collections inside a loop. It will insert the data to de Tags collections, but it is not possible to update the collections Monitor. I try the code with Fiber and promise, but it didn’t work.
This is the code
import psi from 'psi';
import ping from 'ping'
import { Meteor } from 'meteor/meteor';
// Get the PageSpeed Insights report
Meteor.startup(function() {
console.log("Minitor gestart");
Meteor.setInterval(task, 10000);
});
function task() {
Monitor.find().forEach(function(item) {
console.log(item.url);
ping.sys.probe(item.url, function(isAlive) {
if (isAlive) {
psi(item.url, {
key: 'xxxxxxxxxxxxxxxx',
strategy: 'mobile'
}).then(data => {
console.log(data.ruleGroups);
inserttig(item, 1, data.ruleGroups);
updateok(item, 1);
});
} else {
inserttig(item, 0, null);
}
});
});
}
function updateok(item, status) {
Monitor.update({
_id: item._id
}, {
$set: {
status: status,
nextcheck: moment().add(item.interval, 'minutes').toDate()
}
}, function(error, result) {
if (error) reject(error)
else resolve(result)
});
};
function inserttig(item, status, data) {
Tigs.insert({
monitor: item._id,
status: status,
value: data,
datum: new Date()
}, function(error, result) {
if (error) console.log(error);
//updateok(item,status);
});
}