Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registered functions running immediately #1

Open
IslandRhythms opened this issue Dec 22, 2024 · 3 comments
Open

Registered functions running immediately #1

IslandRhythms opened this issue Dec 22, 2024 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@IslandRhythms
Copy link

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.

const db = require('../db');
const initTasks = require('@mongoosejs/task');

module.exports = async function tasks(bot) {
  try {
    const conn = await db().asPromise();
    const setup = { db: conn }
    initTasks(null, setup.db);
    const { Task } = setup.db.models;

    Task.registerHandler('september', function quirkMessage() {
      return september(bot);
    });

    await Task.startPolling();

    await Task.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);
  }
}

function earthWindAndFire() {
  const date = new Date()
  const year = date.getFullYear();
  const septemberTwentyFirst = new Date(year, 8, 21, 19);
  return septemberTwentyFirst;
}
@IslandRhythms IslandRhythms added the help wanted Extra attention is needed label Dec 22, 2024
@jgafnea
Copy link

jgafnea commented Dec 22, 2024

Hi, maybe something with the event being scheduled for a date in the past?

@IslandRhythms
Copy link
Author

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.

@vkarpov15
Copy link
Member

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 run
await Task.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants