I am trying to create a daily task that runs a backend function to check for expired plans.
Classic Editor. In Dev mode, I selected the “Add scheduled jobs” to create a new backend/jobs.config file and followed its instructions and template exactly, using the chron expression for a daily 1:00AM local time execution; then published the site.
Days have passed and the jobs.config has never fired.
Or is it simply the ScheduledTasks.jsw in the backend? Like this:
At the moment, your functionLocation is following the first example - which I imagine might not be the case.
Side note - since .jsw is deprecated, I’d recommend moving to .web.js if the function needs to be called from the frontend, or if it’s just backend, then .js
This is helpful tool for working with Scheduled Jobs (both creating and validating) - Jobs Config | Velo by Wix
After troubleshooting, I got scheduled jobs working. Here’s what worked for me, and what I learned:
File path:
functionLocation in jobs.config is set to /backend by default (as mentioned), but (contrary to the job config tool and Wix instructions) the job won’t run until you remove the forward slash from its path.
File format:
Both .js and .jsw files can be run from jobs.config (tested both successfully). However, if you need to call the same function from a frontend page (In my case, I’d built a function to be called from the frontend with a manual trigger button, but also wanted it automated to trigger daily), you’ll need to wrap it in a .jsw or .js.web file and pass it along to ScheduledTasks.js in the backend.
Weird. Works perfectly fine with the slash before the function name on all my sites that are running scheduled jobs. Not writing backend/functionName.js, just the name with the slash, like so: /functionName.js
Used the same approach when I needed manual triggers as well.
It gets weirder: For nearly a week jobs.config only triggered if I removed the leading forward slash. Then today I get this error: Failed to parse job configuration for site: functionLocation: ‘ScheduledTasks.js’ is of unexpected format.
Presumably, now it’s decided it needs the slash.
Key takeaway: Try with the slash, and if it doesn’t work, try without the slash.