Need a help with jobs.config file

Question:

Greetings fellow developers!
I’m having an issue scheduling a task on Wix.
In the jobs config file, I need it to execute a task two days before the end of the current month.

Example: For December with 31 days, it should trigger on the 29th.

For November with 30 days, it should trigger on the 28th.

It needs to be dynamic. Can any of you help me with this?

Product:
Wix Editor

Code example
{ "functionLocation": "/directory/file.jsw", "functionName": "functionName", "description": "Description here", "executionConfig": { "time": "03:01", "dateInMonth": 1 } }

Interesting question!

While I don’t believe there is a way to calculate this elegantly using a cron job expression, I have a suggestion:

0 0 26-29 * *

The following cron expression runs a script every midnight between the days 26 - 29 (anticipating for months ending with day 28, 29, 30, 31).

You can then add a guard clause in the function the job calls, and make sure it is actually two days before the end of the month via Javascript. If it is not then the function should stop (through a early return).

There may be other ways to accomplish this as well :slightly_smiling_face:

Another option is to simply call the function daily and check for the day of the month within the function called.

Say the function is called runAtEndOfMonth():

//In jobs

{ "jobs": [
  { "functionLocation": "/directory/file.jsw", "functionName": "functionName", "executionConfig": { "cronExpression": "0 8 * * *" } },
] }

//In /directory/file.jsw

const month_length_normal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] 
const month_length_leapyear = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] 

function checkLeapYear (year) {
    if (years/400) { return true }  else if (years/100) { return false } else if (years/4) { return true } else { return false }
 }

export function runAtEndOfMonth () {
   let arr
   let current_time = new Date();
   let leapyear = checkLeapYear( current_time.getFullYear() )
   if (leapyear) { arr = month_length_leapyear } else { arr = month_length_normal }
   let date = current_time.getDate()
   let days_in_month = arr[ current_time.getMonth() ] 
   if (days_in_month - date !== 2) { return }
   //Rest of your function here since you are 2 days from the end of the month
}

Thank you for the responses, everyone. I did something similar to the suggestion from anneyNorton7’s response, but it was a bit different. I created a generic auxiliary function where we pass the day of the last current month and subtract the number of days we want the function to be triggered. Then we perform a check – if it is satisfied, it will return true; if false, it will do nothing. This is done every day. I’ll share the code here with some changes to protect the privacy of the company for anyone who might need help in the future.

Regarding the response from our colleague Thomasj, it’s another approach that I can also try to see if I can solve this problem. I’m sorry I didn’t see the topic earlier; there was a lot going on at the end of the year. I wish all of you a Happy New Year!

Jobs config

{
            "functionLocation": "/location/file.jsw",
            "functionName": "functionName",
            "description": "the description",
            "executionConfig": {
                "time": "12:00",
            }
        },

Auxiliary function

function verifyDayOfEndMonth(nbr) {
    let currentDate = new Date();
    var lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0).getDate();
    var currentDay = currentDate.getDate();
    if (currentDay === lastDayOfMonth - nbr) {
        return true
    } else {
        return false;
    }
}
export async function sendEmailForRememberRealeseHoursAndJobs() {
    try {
        const isFinalMonth = verifyDayOfEndMonth(5);
        if (isFinalMonth === true) {
           
           list.forEach(async (worker) => {
                const options = {
                    variables: {
                        name: worker.member.name,
                        items1: worker.one,
                       items2: worker.two,
                        id: worker.id
                    }
                };
                await sendEmail('templateId, worker.member.id, options);
                console.log('Envio de email');
            });
        } else {
            return null;
        }
    } catch (error) {
        console.log('Erro no disparo de email', error);
    }
}

If there’s any syntax error, I apologize; English is not my native language. Thank you!