D
D
Danio2021-04-22 11:43:59
Node.js
Danio, 2021-04-22 11:43:59

Setting on application restart error on node js?

Hello.
I am a beginner programmer. And I know that when programming there are pitfalls and they need to be dealt with. But here the problem is interesting, maybe someone will tell you or share a link to another resource where I can find the answer.

So, closer to the topic. I have my own telegram bot reminder. Programmatically, the bot is implemented in js with the telegraf.js library. He sends reminders a couple of times a day. Everything works perfectly. But, as always, there is a word - but.

I need the bot to work 24 hours a day. To do this, I selected a system unit on which I configured power-on in the BIOS. The launch of the bot was placed in a bat file, it says:

start npm run start
cmd cd D:\ folder with the bot - in which app.js is located

I threw this bat file into Windows autoload. That is, when the power is turned off and on, the system unit starts, then Windows and autoloading the bot. As if everything is fine. But it was not there.
When you start the bot, there is not always the Internet, it may appear after 10-20 minutes after the start of the bot. Accordingly, at startup, cmd is launched in which the npm window is launched in which the bot is running:
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`

and it gives 3 errors:
( node:10496 ) UnhandledPromiseRejectionWarning: FetchError: request to https://api.telegram.org/bot.....
( node:10496 ) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_reje... ). (rejection id: 1)
( node:10496) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_reje... ). (rejection id: 2)

Most likely, the program should connect to the telegram and to node. She needs the Internet to register some modules.
The problem is that when the Internet appears, the program hangs with an error and does not work. That is, you need to configure the application to restart (well, for example, once every 15 minutes) and if there is Internet, then it will start and everything will work. So the question is how can I restart the application programmatically?

I would be very grateful for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danio, 2021-04-29
@Danio

In general, I tried both pm2 and forever, but if the computer starts up and there is no Internet, then the applications with an error. And when connecting to the Internet, it hangs with an error.
But I found solutions. As it turned out, to check Internet connections, you can use js itself, and then run applications.
So. What I found and improved: A function that checks the Internet connection every 10 seconds (you can set any time) by settimeout. If there is a connection, then the condition is executed and the entire code is run. If there is no connection, then after a set timeout, a function is called after a certain time that does all the same. But if there are connections, then the code will run. That is, the application will not be launched until the Internet appears. This is basically what I need. Now, when power comes on 1. The system unit turns on. 2. Entering Windows. 3. Application startup. 4. Which waits for an Internet connection and works.
You also need to install npm install internet-available - module
Maybe someone will look for solutions to this problem here is the code:
function f333 () {internetAvailable().then(function(){
// program code
console.log("Internet");
}).catch(function(){
console.log("No internet"); // When there is no internet
setTimeout (f333, 10000); // Time after which the function will work again
});
}f333();
Thanks to everyone who tried to help :)

U
user_of_toster, 2021-04-22
@user_of_toster

fetchTry to wrap all requests in so that if an error occurs, a timer is set, after which the request is repeated again.try\catch

function async fetchSomething(url) {
    try {
         fetchAgent.fetch(url);
    } catch(error) {
         setTimeOut(() => fetchSomething(url), 1000000);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question