I have a tweet();
function in a cronjob that executes every hour, and in one of my files used to get data for that function I have an if else statement. What I want to do is; if the statement is true, I want the tweet()
function not to execute at that time, and since this is an hourly cronjob it should just skip execution for that hour but the app should still be running, it shouldn't stop or crash or anything. How would I do this?
index.js (Where the tweet/cron is executed)
// MODULES
const rwClient = require("./TwitterClient.js");
const cronjob = require("cron").CronJob;
const priceModule = require("./price");
const nameModule = require("./name");
const dateModule = require("./date");
const stockModule = require("./stock");
const numShares = require("./numShares.js");
(async () => {
// Async function that creates the Tweet
const tweet = async () => {
try {
await rwClient.v2.tweet(
//
"New insider trade! (form 4 filed)" '\n' '\n'
await nameModule() " bought " await numShares() " shares at " "$" await priceModule() '\n' '\n'
"Total Amount Purchased: " "$" await numShares() * await priceModule() '\n'
"Stock: " await stockModule() '\n'
"Date: " await dateModule() '\n'
);
} catch (error) {
console.error(error);
}
}
// CronJob, starts from 10 am to 8pm EST
const job = new cronjob("0 14-23 * * *", () => {
tweet();
console.log("Tweet executed");
});
job.start();
})();
price.js, file with the if statement
// MODULES
const puppeteer = require("puppeteer");
// Url where we get and scrape the data from
const url = "https://www.sec.gov/edgar/search/#/dateRange=30d&category=custom&forms=4";
let browser;
module.exports = () => (async () => {
browser = await puppeteer.launch();
const [page] = await browser.pages();
//Set User Agent
const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
await page.setUserAgent(ua);
//Go to page from URL and reload
await page.goto(url, {waitUntil: "domcontentloaded", timeout: 0});
await page.reload({waitUntil: "domcontentloaded"});
//Get success response and check if url ends with .xml
const responseP = page.waitForResponse(res =>
res.status() === 200 && res.url().endsWith(".xml")
);
//Click on Form 4 link to the left
const a = await page.waitForSelector(".filetype .preview-file");
await a.click();
const html = await (await responseP).text();
await page.evaluate(html => document.body.outerHTML = html, html);
//Get the info about the stocks price
const price = await page.$$eval(".FormText", els =>
els.find(e => e.textContent.trim() === "$")//Search for a node with a "$" symbol
.parentNode
.textContent
.replace("$", "")
.replace("(1)", "")
.trim()
);
const Price = parseInt(price); //Convert price from string to number
await page.reload({waitUntil: "domcontentloaded"});
await page.close();
await browser.close();
// -------------------- IF STATEMENT --------------------
// If Price is less than $0.1, throw exception
if (Price < 0.1 || null || undefined) {
throw "Price is 0"
} else {
return Price;
}
// -------------------- IF STATEMENT --------------------
})()
.catch(err => console.error(err))
.finally(() => browser?.close());
PS: The price
is exported as a module to the index.js as priceModule
and I made an if statement that throws an error when the conditions are true, but that doesn't produce the effect I wanted, it just prints the error onto the terminal.
CodePudding user response:
I don't have a lot of experience with cronjobs, but I would guess that the if statement needs to be inside the cronjob:
const job = new cronjob("0 14-23 * * *", () => {
if( !condition ) {
tweet();
console.log("Tweet executed");
}
});
CodePudding user response:
The easiest way I can think of is to call it just before the tweet and based on the output use try-catch. Something like
const job = new cronjob("0 14-23 * * *", () => {
try{
price()
} catch (error) {
tweet();
console.log("Tweet executed");
}
});
Or a better way if your price.js file is modifiable if price is 0 then rather than throw Price is 0
do return null
and in your cron
const job = new cronjob("0 14-23 * * *", () => {
let price = price()
if(price === null){
tweet();
console.log("Tweet executed");
}
});