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?
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
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!