Thanks, process.on('beforeExit', ? :

Hello every one. Yesterday i started typing a call for help with websocket on node-express, coz it worked on localhost but deployed to fly wouldnt.
This is how i found out in the ‘your topic is similar to…’ that there’s a ready made solution i kinda dreamt about, that’s called ‘express-ws’ . and now it all works and i am grateful to you guys for this thing that you doing here all.

so i got the ws working . is all good apart from a db load that i kinda want to avoid - it is that i save unsent wsMessages in db and then i fetch them at the server start. And well i save every new unsent message and i do delete from db each successfully sent one.
and i got wondering if there’s a way to just keep all messages on the server in a map and then save them to the db on server dozing away procedure. that happens. u kno

so the question is can i use -
process.on(‘beforeExit’, async () => {
send stringified map to mongoDB?

and one more question, quite a broad one and it’s got an intro:
so i started coding almost 4 moths ago. so i’m not familiar with a lot of things and solutions. for instance it took me quite some time (couple of weeks) till i figured that in order to get unique filenames i can use a Date.now timestamp… i know…

so the question/call is: what should ido?? (i hope you get this one) so i kinda need an advice in general things like databases and stuff. like i tried to befriend babel but i ditched it when started deploying to Fly. it was crashing and crashing… and,but express did the job. but i still wonder maybe babel is a better solution… oh, the goal… right. the goal is a networking solution for readers and writers. see? so it’s… u know. lot’s of big an little robots sorting out sht that no one ever will even try to imagine. i mean not us like. we do imagine. but not the most of not us i mean. so i’d appreciate (good) advices and suggestions. like: why the hell you use mongo instead of redit?? …

oh and i wanna say: "Fly, you rock! i love you. i gonna signup for a paid plan as son as i get some cash. now iam so involved with my creation that i don’t care much about the income part. so today i just gonna say, that what you do is great, important, adorable , and so on. you know yourselves. my best wishes to each of you, which are : rock the hellasht, make your crazy dreams come true! And if all bugs stand against you - that means you are the chosen ones.
cheers

1 Like

I think that’s only executed on a “normal” exit. What you want instead is to catch the signal:

process.on('SIGINT', async () => {
  // send stringified map to mongoDB
  process.exit()
})
1 Like

Hey, thanks! This does work in local environment. but for some reason it doesn’t at Fly.

process.on(‘SIGINT’, async () => {
console.log(‘DOOOOOOOOOOOOOOOES’);
const testMessage = new WsMessage({
userId: userId,
message: is a test message ,
});
await testMessage.save();
process.exit()
})

in console i see the :
from 1 machines to 0 machines. Automatically stopping machine …
[info] INFO Sending signal SIGINT to main child process w/ PID 255
[info] INFO Sending signal SIGTERM to main child process w/ PID 255
[info] INFO Main child exited with signal (with signal ‘SIGTERM’, core dumped? false)
[info] INFO Starting clean up.
[info] WARN hallpass exited, pid: 256, status: signal: 15 (SIGTERM)
listening on […
reboot: Restarting system

so there’s no log. and the message wasn’t sent.
is there something that can be done ?

it does show initial logs like: MongoDB session store connected. and stuff.
but the SIGINT doesn’t do.
weird huh?

Can you share the CMD and ENTRYPOINT (if any) in your Dockerfile?

I read the quoted lines above to indicate that SIGINT was sent to the main process, that process ignored the request so SIGTERM was sent. What that generally means is that the main process isn’t your node/express process.

solved.
so i made the same thing for SIGTERM
and it works!
SIGINT didn’t do tho:

INFO Sending signal SIGINT to main child process w/ PID 255
[info] INFO Sending signal SIGTERM to main child process w/ PID 255
[info] DOES SIGTERM

oh! thats thrilling!
sec.
i found:
CMD [ “npm”, “run”, “start” ]
no ENTRYPOINT found.

these i don;t know what do:
FROM base
COPY --from=build /app /app
just in case

OK, I did some testing, and indeed SIGINT is passed down. If you look in your logs you will find a line that looks like:

INFO Preparing to run `...` as root

The part in quotes is the actual command that runs. It often will be a combination of things like docker-entrypoint.sh, bash, and finally npm. And then if you look in package.json you will find a script named “start” that says something like node server.js. That’s layer upon layer of scripts.

You can eliminate the layers by changing your Dockerfile to look something like this:

ENTRYPOINT ["node"]
CMD ["server.js"]

If you do that, you can capture SIGINT.

1 Like

i see this:
[info] INFO Preparing to run: docker-entrypoint.sh npm run start

I’ve changed theh docker to:
ENTRYPOINT [“node”]
CMD [“server.js”]

and now it logs SIGINT !

"
DOES SIGINT! //log
INFO Sending signal SIGINT to main child process w/ PID 255
INFO Main child exited normally with code: 0
[info] INFO Starting clean up.
ams [info] WARN hallpass exited, pid: 256, status: signal: 15 (SIGTERM)
listening on [fdaa:2:70a1:a7b:23c2:8780:3958:2]:22 (DNS: [fdaa::3]:53)

So it works! Thanks for help!

===
on the way i found these 2 lines in logs that look suspicious to me:

Spectre V2 : WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!
[info] [ 0.039086] PCI: Fatal: No config space access function found

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.