System Information
- Strapi Version: v4.25.3
- Operating System: MacOS
- Database: MYSQL
- Node Version: v20.15.0
- NPM Version: 10.7.0
- Yarn Version: -
I added a Cron job to my backend to reset all users specific tables at specific times based on my computer’s time. That’s why I didn’t include the tz variable. However, it seems my Cron job is not working. Can you help identify the problem?
I enabled my cron at server.js file
const cronTasks = require("./cron-tasks");
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
webhooks: {
populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
},
cron: {
enabled: true,
tasks: cronTasks,
},
});
And here is my code:
module.exports = {
/**
* Cron job to reset dailyFocusTime.
* Every day at 00:00 local time.
*/
resetDailyFocusTime: {
task: async ({ strapi }) => {
console.log('Executing daily reset for dailyFocusTime');
try {
await strapi.query('plugin::users-permissions.user').updateMany({
data: {
dailyFocusTime: 0,
},
});
console.log('Successfully reset dailyFocusTime');
} catch (error) {
console.error('Failed to reset dailyFocusTime:', error);
}
},
options: {
rule: '0 0 * * *', // At 00:00 every day
},
},
/**
* Cron job to reset weeklyFocusTime.
* Every Monday at 00:00 local time.
*/
resetWeeklyFocusTime: {
task: async ({ strapi }) => {
console.log('Executing weekly reset for weeklyFocusTime');
try {
await strapi.query('plugin::users-permissions.user').updateMany({
data: {
weeklyFocusTime: 0,
},
});
console.log('Successfully reset weeklyFocusTime');
} catch (error) {
console.error('Failed to reset weeklyFocusTime:', error);
}
},
options: {
rule: '0 0 * * 1', // At 00:00 every Monday
},
},
/**
* Cron job to reset monthlyFocusTime.
* On the 1st of every month at 00:00 local time.
*/
resetMonthlyFocusTime: {
task: async ({ strapi }) => {
console.log('Executing monthly reset for monthlyFocusTime');
try {
await strapi.query('plugin::users-permissions.user').updateMany({
data: {
monthlyFocusTime: 0,
},
});
console.log('Successfully reset monthlyFocusTime');
} catch (error) {
console.error('Failed to reset monthlyFocusTime:', error);
}
},
options: {
rule: '0 0 1 * *', // At 00:00 on the 1st of every month
},
},
};
console.log('Cron tasks have been defined');
As you can see I cannot see anything on console
