How can I schedule task in future?

NodeBB Development
  • GNU/Linux

    I would know if it exists some method (API) that let me to schedule task in future in nodebb, or I must use setTimeout()?

  • GNU/Linux

    @Doppy No, don't use setTimeout, you'll want to use something like cron

  • GNU/Linux

    @julian I have just implemented my solution with setTimeout(). I think that is a good solution:

    1. I create timeout=setTimeout() and I put it in arrayTimers in my library.js
    2. I save in my database that I have a "timeout"
    3. I use "{ "hook": "static:app.load", "method": "loadTimer" }" to load timer in my arrayTimers when the server crash or it restarts.
    4. When the timeout is schedulated, I delete all value in db and I clearTimeout(myTimer).

    Julian can explain me if my solution can be good or not for you? and can you explain the problem to use setTimeout()?

  • GNU/Linux

    Hi @Doppy -- there's nothing inherently wrong with using setTimeout in the manner you've described, however it is not done simply because there is a better way to do it.

    For example, you're setting the timer, and saving a reference in the db so if the app restarts, it is re-loaded. That is fine, you'd do something similar with node-cron.

    However, when you call setTimeout, the function reference is held in memory and cannot be garbage collected if needed since at some arbitrary point in the future it will be called upon. I don't believe the same happens with cron but I may be mistaken.

  • Global Moderator Plugin & Theme Dev

    @julian the function reference has to be kept in memory for cron too, otherwise cron wouldn't know what to do.

  • GNU/Linux

    I think you're right... in the case of an anonymous function, the function itself would be in memory too.

    Then in that case, it's probably just easier to set up cron in general 😝

Suggested Topics

| | | |