Utility: Scheduled Tasks - Victorianuonuo/nudge_bot GitHub Wiki

This is an introduction of how to set up scheduled tasks such as nudging users to make weekly goals.

Prerequisites

npm install Cron to set time and schedule tasks. Use cron schedule expressions convert to convert time into cron expression.

Scheduled tasks

Start dailyCheck(is_print) function at 7:00 am everyday.

const startCronJob = function(time, is_print=false){
    var job = new CronJob({
        cronTime: '00 00 '+time+' * * *',
        onTick: function() {
            console.log('startCronJob tick!');
            dailyCheck(is_print);
        }
    });
    job.start();
}

Last 30 minutes at 01 or 31 in every hour calling distractionCheck()

const startDistractionCheck = function(){
    var job = new CronJob({
        cronTime: '00 1,31 * * * 1-5',
        onTick: function() {
            console.log('startDistractionCheck tick!');
            distractionCheck();
        }
    });
    job.start();
}

Every Monday at 7:30 am call weeklyPlanner()

const startWeeklyPlanner = function(){
    var job = new CronJob({
        cronTime: '00 30 07 * * 1',
        onTick: function() {
            console.log('startWeeklyPlanner tick!');
            weeklyPlanner();
        }
    });
    job.start();
}

function dailyReminder() executed at 7:30 from Tuesday to Saturday

const startDailyReminder = function(){
    var job = new CronJob({
        cronTime: '00 30 07 * * 0,2-6',
        onTick: function() {
            console.log('startDailyReminder tick!');
            dailyReminder();
        }
    });
    job.start();
}

function shareLinksDaily() executed at 1:00

const startShareLinksDaily = function(){
    var job = new CronJob({
        cronTime: '00 01 00 * * *',
        onTick: function() {
            console.log('startShareLinksDaily tick!');
            shareLinksDaily();
        }
    });
    job.start();
}

function shareLinksDailyReport() executed at 7:15

const startShareLinksDailyReport = function(){
    var job = new CronJob({
        cronTime: '00 15 07 * * *',
        onTick: function() {
            console.log('startShareLinksDailyReport tick!');
            shareLinksDailyReport();
        }
    });
    job.start();
}