G
G
German Shestak2020-12-01 23:21:30
Node.js
German Shestak, 2020-12-01 23:21:30

How to make tasks deferred (scheduler) on nodejs?

Are there any handy libraries? An example of what I want to implement is a delayed sending of a message, or an alarm clock. There is a base (json) where there is a timestamp, you need to do some action when the server time matches the times in the database. The only implementation I came up with is an infinite loop that compares the server time and the time in the database + some kind of error, for the duration of the condition. Is this correct if in theory there will be many such records in the database? Or is there some nice solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Israfil22, 2020-12-01
@Israfil22

If you are interested in ready-made solutions, maybe this package will be useful.
But in theory, if frequent database accesses are undesirable, you can go through the database once every N minutes and collect all the records that must be completed in the interval from Now to Now + N*2 , and then create settimeouts with the specified interval. However, you must be aware of the memory consumption when creating a large number of pending calls.
Also, keep in mind that structured data is easier to work with. If the storage architecture allows, you can store the data in sorted form, and then you can just get all the records one by one.
Suppose one task should be completed in 10000 seconds from the epoch, the second is 10100. Get
the first record, set the execution timeout (10000) on it, read the execution time from the next one - set it as the next iteration of such a "cycle" (10100 minus delta) . (planning execution and planning the creation of a new task)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question