Can you help me with cron function in my app? I’m stuck with it(
Here’s my “track.controller.js” and “server.js” with “multiTrack” function. multiTrack() function starts re-crawling process by users URL’s, it’s starts manually via button in users dashboard, but i want to add cron job to this function (multiTrack).
Imported “multiTrack” function in server.js file return “req is not defined”…
Thank u!
/———–server.js————/
const cron = require('node-cron');
const multiTrackfunc = require('./controllers/track.controller.js');
cron.schedule("*/60 * * * * *", () => {
try {
multiTrackfunc.multiTrack(req, res, next);
console.log(`Re-crawling starts via cron`);
}
catch (err) {
console.log(`${err} - in cron`);
}
});
/———–server.js————/
/———–track.controller.js————/
const cron = require('node-cron')
exports.multiTrack = async (req, res, next) => {
try {
const { userId, createdTracks } = req.body;
const trackIds = createdTracks.map((createdTrack) => createdTrack._id);
const user = await User.findById(userId);
if (!user) {
return res.status(401).json({
success: false,
error: "User does not exist",
});
}
try {
// loop through each track START
await new Promise((resolve, reject) => {
createdTracks.forEach(async (createdTrack) => {
const existingTrack = await Track.findById(createdTrack._id);
if (!existingTrack) {
reject();
}
// crawl Amazon product
console.log(`${createdTrack.name} re-crawling starts`);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(createdTrack.productUrl, {
waitUntil: "networkidle2",
});
const crawledProduct = await page.evaluate(() => {
let actualPrice = 0;
const prepOurPrice = document.querySelector("span.woocommerce-Price-amount.amount").innerText;
const image = document.querySelector(".woocommerce-product-gallery__image a img").src;
const ourPrice = parseFloat(prepOurPrice.replace(/[^0-9.-]+/g, ""));
const salePrice = document.querySelector("#priceblock_saleprice");
const dealPrice = document.querySelector("#priceblock_dealprice");
///parseFloat(actualPrice.replace(/[^0-9.-]+/g, ""))
if (ourPrice) {
actualPrice = ourPrice;
} else if (salePrice) {
actualPrice = salePrice.innerText;
} else if (dealPrice) {
actualPrice = dealPrice.innerText;
}
return {
image,
actualPrice,
};
});
console.log(`${createdTrack.name} re-crawling ends`);
await browser.close();
const { image, actualPrice } = crawledProduct;
if (existingTrack.image !== image) {
existingTrack.image = image;
await existingTrack.save();
}
if (existingTrack.actualPrice !== actualPrice) {
existingTrack.actualPrice = actualPrice;
await existingTrack.save();
}
resolve();
});
});
// loop through each track END
} catch {
return res.status(401).json({
success: false,
error: "Found invalid track id",
});
}
const tracks = await Track.find({ _id: { $in: trackIds } });
return res.status(201).json({
success: true,
data: tracks,
});
} catch (err) {
console.log("crawling failed");
return res.status(500).json({ error: err.message });
}
};
/———–track.controller.js————/