You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a discord bot where one of the features is that on September 21st, it will send the youtube link the song "September". For some reason, these past two days it was running the task at noon. When trying to debug, it would also run on application restart. I'm wondering if there is an explanation to this behavior as I don't see why the code in its current state is allowing this behavior.
constdb=require('../db');constinitTasks=require('@mongoosejs/task');module.exports=asyncfunctiontasks(bot){try{constconn=awaitdb().asPromise();constsetup={db: conn}initTasks(null,setup.db);const{ Task }=setup.db.models;Task.registerHandler('september',functionquirkMessage(){returnseptember(bot);});awaitTask.startPolling();awaitTask.findOneAndUpdate({name: 'september',status: 'pending'},{scheduledAt: earthWindAndFire(),repeatAfterMS: millisecondsInYear},{upsert: true,returnDocument: 'after'});}catch(error){console.log('something went wrong registering all the handlers',error);}}functionearthWindAndFire(){constdate=newDate()constyear=date.getFullYear();constseptemberTwentyFirst=newDate(year,8,21,19);returnseptemberTwentyFirst;}
The text was updated successfully, but these errors were encountered:
The earthWindAndFire function is scheduling 9/21/2024 but its past that date so I don't see why it would be running, especially now since all that time has passed.
If a task is pending, then startPolling() will run the task no matter how far in the past the task was.
If your task runner restarts, the await Task.findOneAndUpdate({ name: 'september', status: 'pending' }) will schedule a new task for 9/21, and then that task will immediately run. I think what you're trying to do would be better expressed as the following:
// If there isn't a 'september' task scheduled at the correct time, create one. But ignore 'september'// tasks that have already runawaitTask.findOneAndUpdate({name: 'september',scheduledAt: earthWindAndFire()},{$setOnInsert: {status: 'pending',repeatAfterMS: millisecondsInYear}},{upsert: true,returnDocument: 'after'});
Also you should be careful to make earthWindAndFire() function timezone independent. new Date(year, 8, 21, 19); will create date in your local timezone, so you may still end up with duplicate tasks if your task runner runs in a different timezone.
I have a discord bot where one of the features is that on September 21st, it will send the youtube link the song "September". For some reason, these past two days it was running the task at noon. When trying to debug, it would also run on application restart. I'm wondering if there is an explanation to this behavior as I don't see why the code in its current state is allowing this behavior.
The text was updated successfully, but these errors were encountered: