Job scheduler not working

Job scheduler is potentially a great feature for us, enabling us to execute tasks on a scheduled basis. It seems like it should be very simple. However, I can’t seem to get it to work. I closely followed the instructions here: https://support.wix.com/en/article/corvid-scheduling-recurring-jobs

I created a jobs.config that contains:
{
“jobs” : [
{
“functionLocation” : “/jobtest.jsw” ,
“functionName” : “jobtester” ,
“description” : “describe your job” ,
“executionConfig” : {
“time” : “16:30”
}
}

]
}

I also created jobtest.jsw in the backend (not in a folder) which contains:

import wixData from ‘wix-data’ ;
export function jobtester(){

let toInsert = {
“title” : “job1” ,
“field1” : “jobfieldtest1” ,
“field2” : “jobfieldtest2”
};

return wixData.insert( “test1” , toInsert);

}

Nothing happens at the scheduled UTC time. Is there something I am doing wrong?

I used the cron expression instead and it now seems to be working fine.

You have to take into account that times are expressed in UTC and not in your local time zone.
I got it working your time notation after correcting for this.

In case anyone comes across this and hasn’t found an answer. Some things that i think were tripping me up.

  • seems like the page needs to be published for the changes to work
  • the script name can only have letters. I was using an underscore and seemed like this affected it.
  • the js file needed all the parts below
  • output the time in javscript with the code below to check what time your job should shedule:

var d = new Date();
console.log(d.toLocaleTimeString());

my final working jobs config code. runs every hour:

{
 "jobs": [    
    {
 "functionLocation": "/binance/binance.start",
 "executionConfig": {
 "cronExpression": "0 * * * *"
      }      
    }
  ]
}

the js file located at the backend/binance/binance.js has the following that seem to be needed:

export function start() {
  // do your stuff here 
}
/* 
$w.onReady(function () {
  start();
});
*/

Even though it is commented out, to avoid further confusion: the $w.onReady function will not work in server side code.

Trying to run daily recurring job to send birthday mail to contacts saved in collection.
the happyBirthday() function runs well in debugging mode only.
Please help to get this solve.

I created a jobs.config that contains:

jobs.config

{
“jobs”: [{
// Option 2: Ddefine execution interval by setting time, day of week, and day of month
“functionLocation”: “/backend/birthdayMail.js(w)”, // Relative to Backend folder, started by slash
“functionName”: “happyBirthday”,
“description”: “Send Birthday mail to contacts”, // Optional
“executionConfig”: {
// “time”: “10:20” // “hh:mm” 24h format, UTC timezone (e.g. 13:00)
“cronExpression”: “13 20 * * *” // Optional: Uncomment section below for more complex intervals
}
},

] 

}

I also created happyBirthday.jsw in the backend (not in a folder) which contains:

import wixData from ‘wix-data’;
import { getJSON } from ‘wix-fetch’;
import { sendEmail } from ‘backend/sendEmail.jsw’;

import sgMail from ‘@sendgrid/mail’;
import wixSecretsBackend from ‘wix-secrets-backend’;

export async function sendEmail1(recipient, subject, body) {

const sendGridSecret = JSON.parse(await wixSecretsBackend.getSecret('sendGridSecret')); 
const key = sendGridSecret.key; 
const senderEmail = sendGridSecret.senderEmail; 

sgMail.setApiKey(key); 
happyBirthday(); 

const msg = { 
    from: senderEmail, 
    to: mailId, 
    subject: subject, 
    text: body 
}; 

try { 
    return await sgMail.send(msg); 
} catch (error) { 
    console.error('Error sending the email: ' + error.message); 
    return false; 
} 

}

// send birthday mail

export async function happyBirthday() {
let now = new Date();
let todayMonth = now.getMonth() + 1;
let todayDate = now.getDate();
console.log(now, “Today’s date”);

console.log(todayDate, "Today date"); 
console.log(todayMonth, "Today Month"); 

let birthDate 
let birthMonth 
let monthDiff 
let datediff 
// let mailId 
// let subject 
// let body 

wixData.query("Team") 
    .find() 
    .then((results) => { 
        if (results.items.length > 0) { 
            let contacts = results.items[0]; 
            let bdate = contacts.birthdate; 
            // let dbate= "2022-11-30" 
            let mailId = contacts.email; 
            // let mailId = "keer.anu23@gmail . com"; 

            let subject = "Happy Birthday to you!"; 
            let body = "Wishing You a Very Happy Birthday,Have a good day!"; 
            console.log(contacts); 
            console.log(bdate, "User Birthdate"); //user birthdate 
        
            let birthDate = bdate.getDate()+1; 
            // let newBirthDate = new Date(); 
            // newBirthDate.setDate(birthDate + 1); 
            // let birthDate = 3; 
            let birthMonth = bdate.getMonth() + 1; 
            // let birthMonth = 12; 
            console.log(results) 
            console.log(birthDate, "user bdate"); 
            console.log(birthMonth, "user bMonth"); 
            let monthDiff = todayMonth - birthMonth; 
            let datediff = todayDate - birthDate; 
            console.log(monthDiff); 
            console.log(datediff); 
            // console.log("dates matched, Happy Birthday") 

            // sendEmail(mailId, body, subject); 
            // const emailResult = sendEmail(mailId, body, subject); 
            // function (params) { 
                
            // } 
            if (monthDiff === 0 && datediff === 0) { 
                console.log(mailId); 
                console.log(body); 
                console.log(subject); 
                const emailResult = sendEmail(mailId, body, subject); 
                // clearFields(); 
                // displayMessage1('Happy Birthday! Mail has been sent successfully!'); 
                console.log("dates matched, Happy Birthday") 

            } else { 
                // displayMessage1('Your Birthday is coming soon'); 
                console.log("Your Birthday is coming soon") 

            } 

        } else { 
            console.log("error") 
        } 

    }) 
// happyBirthday(); 

// function sendMail() { 
//     // console.log("function called") 
//     if (monthDiff === 0 && datediff === 0) { 
//         // happyBirthday(); 
//         console.log("dates matched, Happy Birthday") 

//     } else { 
//         console.log("Your Birthday is coming soon") 
//     } 

}

// }