Error when running firebase node app on Heroku - node.js

The code (server.js) is as following following the Firebase documentation example
var firebase = require("firebase");
firebase.initializeApp({
databaseURL: "https://mydatabaseName.firebaseio.com",
});
var db = firebase.database();
var ref = db.ref("/Users");
ref.on("value", function(snapshot) {
console.log(snapshot.val());
});
but I'm running into the error
Error waiting for process to terminate: No child processes
State changed from starting to crashed
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Stopping process with SIGKILL
Process exited with status 22
After googling this issue, I tried adding to the Procfile
worker: node server.js
heroku ps:scale worker=1 web=0
None of these seemed to work. Any suggestions?

Related

ExpressJS Node Process won't end "normally"

NodeJS v 16.15.1 Windows 10.
Since a few days, neither nodemon nor VSCode can end node processes which use app.listen() of express. When the code changes we see:
[nodemon] restarting due to changes...
But nothing happens after that. We have to kill the processes manually in task manager.
'use strict';
const express = require('express');
const app = express();
const PORT = 8040;
// The PROBLEMATIC line:
app.listen(PORT, () => {
console.log(`Started on http://localhost:${PORT} (PID: ${process.pid})`);
});
If I remove "The PROBLEMATIC line" then nodemon/vscode can restart the process.
There is NO error message, the app just does not exit. For example by entering rs:
Nothing happens, no matter how long I wait.
Using nodemon --signal SIGTERM makes no difference, the process never sees the SIGTERM.
Using the package why-is-node-running we see:
There are 5 handle(s) keeping the process running
# TCPSERVERWRAP
C:\services\overview\node_modules\express\lib\application.js:635 - return server.listen.apply(server, arguments);
C:\services\overview\src\tmp2.js:6 - app.listen(PORT, () => {
# TTYWRAP
C:\services\overview\src\tmp2.js:7 - console.log(`Started on http://localhost:${PORT} (PID: ${process.pid})`);
# HTTPINCOMINGMESSAGE
(unknown stack trace)
# HTTPINCOMINGMESSAGE
(unknown stack trace)
# TickObject
(unknown stack trace)
You want end the node/nodemon process? Why not using ctrl + c instead

my discord bot works well for a while but then "failed to bind to $PORT within 60 seconds of launch"

I've tried different approaches and looked at all the other replies but i dont know how to adress it
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-03-19T09:15:18.022903+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-03-19T09:15:18.059850+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2022-03-19T09:15:18.210022+00:00 heroku[web.1]: Process exited with status 22
2022-03-19T09:15:18.331083+00:00 heroku[web.1]: State changed from starting to crashed
the code:
require('dotenv').config();
const OpenAI = require('openai-api');
const openai = new OpenAI(process.env.OPENAI_API_KEY);
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
let prompt =`text here`;
client.on("message", function (message) {
if (message.author.bot) return;
prompt += `You: ${message.content}\n`;
(async () => {
const gptResponse = await openai.complete({
engine: 'davinci',
prompt: prompt,
maxTokens: 80,
temperature: 0.7,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0,
bestOf: 1,
n: 1,
stream: false,
stop: ['\n', '\n\n']
});
message.reply(`${gptResponse.data.choices[0].text.substring(5)}`);
prompt += `${gptResponse.data.choices[0].text}\n`;
})();
});
client.login(process.env.BOT_TOKEN);
my procfile is empty but it still works for 60 seconds
any ideas?
edit: THINGS I TRIED THAT DONT WORK
i tried changing procfile to contain
worker: node index.js
procfile to worker: java -jar build/libs/*.jar
Use the name Procfile instead of procfile, otherwise heroku doesn't learns that you need worker process and not the default web process.

Despite app.listen, express app does not bind to port

Exactly what it sounds like.
I use ExpressJS for my Node app, which is hosted on Heroku.
Despite using app.listen, it consistently is getting / causing heroku R10 errors, which are caused by a web app not binding to process.env.PORT in time.
The relevant code:
const app = express();
var isRoot = (process.getuid && (process.getuid() === 0));
var port;
if (isRoot) {
port = 80;
} else {
port = process.env.PORT | 8000;
}
const server = app.listen(port, onStartup);
function onStartup() {
console.log("Started webserver on port "+port);
}
Now the odd thing is, I'm getting the "Started webserver on port [foo]" message, it's just not binding to the port.
Logs:
2020-03-30T19:50:39.434302+00:00 app[web.1]: > foo-bar#1.0.0 start /app
2020-03-30T19:50:39.434303+00:00 app[web.1]: > node scrape2.js
2020-03-30T19:50:39.434303+00:00 app[web.1]:
2020-03-30T19:50:39.829882+00:00 app[web.1]: Verbose mode OFF
2020-03-30T19:50:39.830782+00:00 app[web.1]: Started webserver on port 8052
2020-03-30T19:51:37.415192+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-30T19:51:37.293060+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-03-30T19:51:37.293142+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-03-30T19:51:37.391762+00:00 heroku[web.1]: Process exited with status 137
Help!
I did a stupid and accidentally used the bitwise OR operator, which caused it to bind to a different port than process.env.PORT. Changed it from | to || and it works fine now.

My express server is not exiting with both ctrl + c and process.exit(1)

I am getting an error message:
listen EADDRINUSE: address already in use :::3000.
When I tried after removing the server starting code(i.e app.listen part) nothing is happening
const path = require('path')
const express = require('express')
//var publicPathDirectory = path.join(__dirname,"../public")
const app = express()
app.listen(3000,()=>{
console.log('server started')
})
process.on('SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(1);
});
I've had this happen to me before, where even though I quit the node server with CTRL+C, it still is hogging port 3000. You can kill node with:
pkill -f node
Potentially related to Node.js Port 3000 already in use but it actually isn't?
Use number 1 inside exit only when you have an exit with failure. To force exit do not use number inside exit().
process.exit()
Please take a look at here. May be you can understand. link

Bluemix Node js app push Start unsuccessful

I am using watson conversation service on node js application.
while trying to push application to bluemix. (through command prompt)
After uploading all the files..
0 of 1 instance running, 1 starting
0 of 1 instance running, 1 starting
0 of 1 instance running, 1 starting
0 of 1 instance running, 1 starting
0 of 1 instance running, 1 crashed
FAILED
Start unsuccessful
Kindly help what's the issue..
command prompt
'My coding
var watson=require('watson-developer-cloud');
var conversation =watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation/api',
username:' ',
password:' ',
version:'v1',
version_date:'2017-06-20'
});
var context={};
context.hour=-1;
function prompt(question,callback){
var stdin=process.stdin,
stdout=process.stdout;
stdin.resume();
stdout.write(question);
stdin.once('data',function(data){
callback(data.toString().trim());
});
}
function tConvert(time){
time=time.toString().match(/^([01]\d2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/)||[time];
if(time.length>1){
time=time.slice(1);
time[5]=+time[0]<12?'AM':'PM';
time[0]=+time[0]%12||12;
}
return time.join('');
}
function convMessage(message){
var d=new Date();
var n=tConvert(d.getHours() + ':' + d.getMinutes() + ':00');
context.hour=(n.split(':'))[0];
context.minute=(n.split(':'))[1];
conversation.message({
workspace_id:'09ee7558-0d3e-4af3-8429-14e60be348d7',
input:{'text':message},
context:context
},function(err,response){
if(err){
console.log('error:',err);
}else {
console.log('Watson: ' + response.output.text[0])
prompt('You: ', function(input){
convMessage(input);
});
context=response.context;
}
});
}
convMessage('Hi.');
Your program might run locally. However, to run as Bluemix Node.js app on Cloud Foundry it needs to meet certain requirement. A web app is expected and the health manager checks on the expected port whether your app is alive. If the app cannot be detected it is considered "dead" and the logs will show it as "crashed".
Take a look at the sample app "Conversation Simple" and the main file "server.js" for how the port info is handled.
As an alternative for your code, you could consider setting a health check type of process. It would indicate Bluemix / Cloud Foundry that you don't deploy a regular (Web) app, but something running in the background or executed once.

Resources