Home > Net >  Cron job is getting marked as completed instead of error on k8s, even though there are errors
Cron job is getting marked as completed instead of error on k8s, even though there are errors

Time:09-13

I am trying to write a cron job which takes an excel file from SFTP server and upload the data on mongo DB.

Now, suppose there are errors in cronjob like sftp connection failure due to some credentials issue or path from where the file needs to be picked up from is not present, then the control is going inside catch in the below code snippet but instead of marking the cron as Error, it is showing as completed on kubernetes.

I have attached a basic sample code below of the cron job to get an idea about what I am trying to say.

exports.fsmOverallPerformanceData = async (req, res) => {
    let sftp = new Client;
    const fileName = "FSM_Performance_Data.xlsx"
    const remote = "/home/SI_MARCOM_TOPS/SI_JHDSFTP/SND/"
    const remotePath = remote   fileName
    const localePath = "./fsmperformance.csv";
    sftp.connect(config.sftpSetting, 'once').then(() => {
        sftp.fastGet(remotePath, localePath, {}).then(() => {

        }).catch((err) => {
            console.log(err, 'FSM Performance fastGet method error'); // this is getting printed
        })
    }).catch((err) => {
        console.log(err, 'SFTP Connect method error'); // this is getting printed
    });
    setTimeout(() => {
        process.exit();
    }, 300000);
}

Thanks in advance for any suggestions or help.

CodePudding user response:

You must ensure that the process exits with a non-zero exit code. Your catch blocks could use a process.exit(1) and throw Error.

  • Related