节点计划无限期运行

时间:2019-03-19 11:54:43

标签: node.js

我正在使用节点计划在特定日期发送电子邮件。从数据库中获取日期。如果我运行该应用程序,则邮件将连续循环发送出去。即使日期中指定的时间已经过去或调用了任何一项作业,这两个作业似乎都可以连续运行。

以防万一,我不从数据库中获取日期,而是明确提供它运行良好。请帮忙。

如果日期也像提醒1一样明确提及,则为new Date(2019,2,19,11,20,0);在getDate函数中,应用程序运行良好,但是如果我在getDate()函数中提到数据库中的日期,则不会执行任何操作。

const schedule = require('node-schedule');
const nodemailer = require('nodemailer');
const {
    poolPromise,
    sqlInstance
} = require('./database')
const mailer = require('../routes/initiateCertification');

const month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

const res1 = [];
let reminder = [];
let year1, year2, month1, month2, date1, date2;
let reminder1, reminder2;
let res3 = [];
let reminders;
exports.getDate = async (req, res) => {
    try {
        const pool = await poolPromise
        const result = await pool.request().query("SELECT TOP 1 [Reminder1],[Reminder2] FROM [dbo].[InitiateCertification] ORDER BY [ID] DESC");
        res1.push(result.recordset);
        reminder = res1[0][0]['Reminder1'].toString().split(" ");
        for (let i = 0; i < month.length; i++) {
            if (reminder[1] == month[i]) {
                month1 = Number(i);
                break;
            }
        }
        year1 = Number(reminder[3]);
        date1 = Number(reminder[2]);
        reminder1 = new Date(year1, month1, date1, 13, 30, 0);
        // If i work with below line it work and runs only once
        //reminder1 = new Date(2019, 2, 19, 12, 45, 0);
        console.log(reminder1.toString())
        reminder = res1[0][0]['Reminder2'].toString().split(" ");
        for (let i = 0; i < month.length; i++) {
            if (reminder[1] == month[i]) {
                month2 = Number(i);
                break;
            }
        }
        year2 = Number(reminder[3]);
        date2 = Number(reminder[2]);
        console.log(year1, year2, date1, date2, month1, month2);
        reminder2 = new Date(year2, month2, date2, 16, 58, 0);
        console.log(reminder2)
        reminders = {
            'reminder1': reminder1,
            'reminder2': reminder2
        };
        console.log(reminders)

    } catch (err) {
        console.log(err.message);
    }
}

function getEmailId = async (req, res) => {
    try {
        const pool = await poolPromise
        let result = await pool.request().query("SELECT [EmailId], [ManagerId] FROM [dbo].[EmployeeData]");
        let result2 = await pool.request().query("SELECT [EmailId] FROM [dbo].[CertificationForm]");
        let res1 = result.recordset;
        let res2 = result2.recordset;
        let flag = true;
        for (let i = 0; i < res1.length; i++) {
            for (let j = 0; j < res2.length; j++) {
                if (res1[i]['EmailId'] == res2[j]['EmailId']) {
                    flag = false;
                }
            }
            if (flag == true) {
                res3.push(res1[i]);
            }
            flag = true;
        }
        console.log(res1);
        console.log(res2);
        console.log(res3);
        //res.json(res3);
        sendMail1(res3);
    } catch (err) {
        console.log(err.message);
        // res.status(500)
        // res.send(err.message)
    }
}

function sendMail1(res) {
    //...
}


console.log(year1);
// with these two lines its running continously.
reminder2 = new Date(year2, month2, date2, 17, 1, 55);
reminder1 = new Date(year1, month1, date1, 11, 20, 0)
let rem1 = schedule.scheduleJob(reminder1, function() {
    console.log("rem1");
    getEmailId();

});
let rem2 = schedule.scheduleJob(reminder2, function() {
    getEmailId();
    console.log(reminder2)
    console.log("rem2");

});


//app.js

app.listen(PORT, function() {

    scheduled.getDate();
    scheduled.rem2;
    scheduled.rem1;
    console.log('Node.js server is running on port ' + PORT);
});

2 个答案:

答案 0 :(得分:0)

我认为正在发生的事情是,当您创建计划作业时,reminder1reminder2变量的值不是您所期望的。创建reminder1reminder2变量后,它们将传递到当时未定义的year2month2date2变量中。该代码随后在getDate函数中分配这些值。在创建作业之前,需要调用getDate函数。

尝试此代码:

const schedule = require('node-schedule');
const nodemailer = require('nodemailer');
const {
    poolPromise,
    sqlInstance
} = require('./database')
const mailer = require('../routes/initiateCertification');

const month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

const res1 = [];
let reminder = [];
let year1, year2, month1, month2, date1, date2;
let reminder1, reminder2;
let res3 = [];
let reminders;
exports.getDate = async (req, res) => {
    try {
        const pool = await poolPromise
        const result = await pool.request().query("SELECT TOP 1 [Reminder1],[Reminder2] FROM [dbo].[InitiateCertification] ORDER BY [ID] DESC");
        res1.push(result.recordset);
        reminder = res1[0][0]['Reminder1'].toString().split(" ");
        for (let i = 0; i < month.length; i++) {
            if (reminder[1] == month[i]) {
                month1 = Number(i);
                break;
            }
        }
        year1 = Number(reminder[3]);
        date1 = Number(reminder[2]);
        reminder1 = new Date(year1, month1, date1, 13, 30, 0);
        // If i work with below line it work and runs only once
        //reminder1 = new Date(2019, 2, 19, 12, 45, 0);
        console.log(reminder1.toString())
        reminder = res1[0][0]['Reminder2'].toString().split(" ");
        for (let i = 0; i < month.length; i++) {
            if (reminder[1] == month[i]) {
                month2 = Number(i);
                break;
            }
        }
        year2 = Number(reminder[3]);
        date2 = Number(reminder[2]);
        console.log(year1, year2, date1, date2, month1, month2);
        reminder2 = new Date(year2, month2, date2, 16, 58, 0);
        console.log(reminder2)
        reminders = {
            'reminder1': reminder1,
            'reminder2': reminder2
        };
        console.log(reminders)

    } catch (err) {
        console.log(err.message);
    }
}

function getEmailId = async (req, res) => {
    try {
        const pool = await poolPromise
        let result = await pool.request().query("SELECT [EmailId], [ManagerId] FROM [dbo].[EmployeeData]");
        let result2 = await pool.request().query("SELECT [EmailId] FROM [dbo].[CertificationForm]");
        let res1 = result.recordset;
        let res2 = result2.recordset;
        let flag = true;
        for (let i = 0; i < res1.length; i++) {
            for (let j = 0; j < res2.length; j++) {
                if (res1[i]['EmailId'] == res2[j]['EmailId']) {
                    flag = false;
                }
            }
            if (flag == true) {
                res3.push(res1[i]);
            }
            flag = true;
        }
        console.log(res1);
        console.log(res2);
        console.log(res3);
        //res.json(res3);
        sendMail1(res3);
    } catch (err) {
        console.log(err.message);
        // res.status(500)
        // res.send(err.message)
    }
}

function sendMail1(res) {
    //...
}


console.log(year1);

let rem1, rem2;
let createJobs = () => {

    // with these two lines its running continously.
    reminder2 = new Date(year2, month2, date2, 17, 1, 55);
    reminder1 = new Date(year1, month1, date1, 11, 20, 0)
    rem1 = schedule.scheduleJob(reminder1, function() {
        console.log("rem1");
        getEmailId();

    });
    rem2 = schedule.scheduleJob(reminder2, function() {
        getEmailId();
        console.log(reminder2)
        console.log("rem2");

    });
}
//app.js

app.listen(PORT, function() {

    scheduled.getDate();

    createJobs();

    scheduled.rem2;
    scheduled.rem1;
    console.log('Node.js server is running on port ' + PORT);
});

答案 1 :(得分:0)

下一个答案中发布的代码以及app.js文件中的async / await对我来说都是有效的。

                let scheduler = async() => {
            //   console.log("shdhd");
                await scheduled.getDate();
                scheduled.createJobs();
            }


            app.listen(PORT, function () {
              scheduler();
              console.log('Node.js server is running on port ' + PORT);
            });