Running node-cron blocks my terminal until I CTRL + C

Viewed 17

I writing a script to execute some POST methods using Axios. To execute it, I'm using node-cron and creating a job to execute it.

However, when I "node script.js", I see that my terminal isn't available, like it is still running or in a infinite loop.

The schedule task is working good, but I'm worried about the terminal thing. I'm not sure if when you create a task, it will run until the machine is rebooted or when you end the task using CTRL + C.

I'm not DEV, so this is the code that I did:

const axios = require('axios');
const cron = require('node-cron');

let channelID = 'ID-HERE';

// let slackAPItopic = "https://letsdeel.slack.com/api/conversations.setTopic";
// let tokenSlack = 'token-here';
// let slackAPIsendMessage = "https://slack.com/api/chat.postMessage";



const postInTopic = async (displayID) => {

    await axios.post(
        `https://letsdeel.slack.com/api/conversations.setTopic`, 
        {
            channel: channelID,
            topic: 'Shift Watcher from .JS:' + ' ' + '<@' + displayID + '>'
            
        },
        {
            headers: {
                'Authorization': "Bearer token-here"
            }
        }
    );

};

const privateMessage = async (displayID) => {
    await axios.post(
        `https://slack.com/api/chat.postMessage`,
        {
            channel: displayID,
            text: "Hi! Just a reminder that you are on Shift Watch - From .JS"
        },
        {
            headers: {
                'Authorization': "Bearer token-here"
            }
        }
    );
};


var cronFirstHour = cron.schedule('0 1 * * 1,2,3,4,5', () => {
    
    let displayID = 'OADHWAD23';
    postInTopic(displayID);
    privateMessage(displayID);
    
},{
    scheduled: true,
    timezone: "Atlantic/Azores"
}
);

Is it happening because that's how node-cron works? Like, a forever task? I was expecting to see the cron in the crontab or anything like.

I'm developing in MacOS at the moment, but the plan is moving to AWS. Or should I make it list like a service in Linux?

About the script: It will set the topic in Slack and send a message at the same time in a specific scheduled hour.

0 Answers
Related