I have a react application, and I need a thread/process outside of the main thread to run some functions every hour, or even every day.
I don't know how to implement this, whether create a new API using Java or other app, or use js' setInterval() technique. I found nodejs Worker Threads too. But I don't know what is the best way to implement it.
The process itself is getting a data (an array) from public API, and process the data periodically, it will be a 'stay online' process since I want to run this in a cloud and stay online for a long time.
So, can I have any suggestion of how to implement this?
Thank you very much.
I have a react application, and I need a thread/process outside of the main thread to run some functions every hour, or even every day.
I don't know how to implement this, whether create a new API using Java or other app, or use js' setInterval() technique. I found nodejs Worker Threads too. But I don't know what is the best way to implement it.
The process itself is getting a data (an array) from public API, and process the data periodically, it will be a 'stay online' process since I want to run this in a cloud and stay online for a long time.
So, can I have any suggestion of how to implement this?
Thank you very much.
Share asked Dec 6, 2019 at 10:59 Evan GunawanEvan Gunawan 4291 gold badge6 silver badges18 bronze badges 1-
1
if your server supports cron and crontab, adding your script there (
node /path/to/script.js
) would be the simplest solution. – georg Commented Dec 6, 2019 at 11:14
2 Answers
Reset to default 4You can use this lib Node Schedule
or you can use node.js built in lib child_process
spawn(), exec(), execFile(), and fork()
If you want to use thread then latest version of node.js has support for worker_threads. If you want to use a separate process then child_process is what you are looking for.
Here is an article about how to parse json using worker_thread. Using same method you can do any CPU intensive task in a separate thread without blocking the main thread.