samedi 28 septembre 2019

Is there a way to make changes to a node application without reloading the whole application?

I built an application that downloads a pdf from a certain URL and emails it to certain people based on a certain schedule. For this, I used the request, nodemailer and node-schedule packages. I created a file called Jobs.js where all jobs are held. Its structure looks like below:

module.exports = { 
    Jobs: [
    {
        URL: http://something.com, 
        Schedule: "6 * * * *",
        Sender: me@something.com, 
        Reciever: me2@something.com, 
        Subject: "periodic update", 
        SuccessMessage: "Please find attached this pdf",
        FailedMessage: function(){
            return `Unable to send email but you can download from {this.URL}`
        }
    },
    {
        URL: http://something.com, 
        Schedule: "7 * * * *",
        Sender: me3@something.com, 
        Reciever: me4@something.com, 
        Subject: "periodic update", 
        SuccessMessage: "Please find attached this pdf",
        FailedMessage: function(){
            return `Unable to send email but you can download from {this.URL}`
        }
    }
  ]
}

Right now, I am running the application through nodemon, so when a new job is added and the file saved, it restarts the whole application automatically. I don't like this as what if the job is added when the one of them was running ! It would restart the whole app and that job would not complete. I want that when a job is added the application is aware and just adds that job to schedule. The scheduling code with node-schedule is pretty straightforward:

Jobs.forEach((job) => {
    schedule.scheduleJob(job.schedule, function(){
        downloadPDF(job); 
        sendEmail(job);                 
}); 

I am currently studying design patterns and wondering if there could be one that solves this problem. What I think I might have done correctly so far is that I have isolated what varies.

Aucun commentaire:

Enregistrer un commentaire