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

runOrRetry() method in JobProcessor class "leaks" timers #47

Open
ghost opened this issue Oct 17, 2023 · 1 comment
Open

runOrRetry() method in JobProcessor class "leaks" timers #47

ghost opened this issue Oct 17, 2023 · 1 comment

Comments

@ghost
Copy link

ghost commented Oct 17, 2023

the method creates a promise race between the execution of the actual job and a new timer, to check wheater the job is still running/expired after a set amount of time.

but this timer is never cleared after the job fails/succeeds, or after the agenda.stop() method is called, so each new job will "leak" a new timer that will be cleared only after it is expired, and these can stack up pretty quickly.

link to code

@riddlewiggler
Copy link

riddlewiggler commented Oct 27, 2023

I'm not sure to have grasped the issue completely @volpd, however I guess you can tackle this issue sideways, by leveraging on proxies:

const { setTimeout, clearTimeout } = ((globalContext) => {
  const timeouts = new Set();

  const setTimeout = new Proxy(globalContext.setTimeout, {
    apply(target, thisArg, args) {
      const tm = target.apply(thisArg, args);
      timeouts.add(tm);
      return tm;
    },
  });

  const clearTimeout = new Proxy(globalContext.clearTimeout, {
    apply(target, thisArg, args) {
      if (!args.length) {
        return clearAllTimeouts(target, thisArg);
      }
      const tm = args[0];
      return clearTimeoutById(target, thisArg, tm);
    },
  });

  function clearAllTimeouts(target, thisArg) {
    for (const tm of timeouts) {
      target.call(thisArg, tm);
    }
    timeouts.clear();
  }

  function clearTimeoutById(target, thisArg, tm) {
    if (timeouts.has(tm)) {
      target.call(thisArg, tm);
      timeouts.delete(tm);
    }
  }

  return {
    setTimeout,
    clearTimeout,
  };
})(global);



module.exports = { setTimeout, clearTimeout };

Execute clearTimeout without an argument to clear all the timeouts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant