Ways to comunicate with glitch


#1

It would be cool to have:

  • A way to execute code just before your project halts
  • A way to halt your project early

#2

Hi @SvizelPritula,

  1. apps receive a SIGTERM signal before being halted, you can use it in this way:
process.on("SIGTERM", function () {
  // put here your commands
  process.exit(0); // THIS IS IMPORTANT!
});
  1. You can stop your projects with
curl -X POST https://api.glitch.com/projects/{project-id}/stop?authorization={user-token}

#3

Thanks! By the way, what happens if I omit process.exit(0)?


#4

You’re welcome :slight_smile:

process.exit(0) makes your app actually stop and exit. When you override a signal handler, like in this case, the “default behavior” gets overridden: SIGTERM would normally just make your app quit, but if you override it, you make it do something else. If you omit process.exit, your app would keep running. In this case we apply a grace period of 5-10 seconds before killing your app forcefully.


#5

What happens when I use process.exit(0) early?


#6

your app exits as soon as you call it


#7

So why would I use the API?


#8

I think the API is designed for shutting the project down, even if it’s frozen, while the process.exit(0) won’t run if your project isn’t working properly.


#9

@j-f is correct: process.exit(0) just stops your application, but your project is still “active”: in fact, it will be automatically restarted immediately after it gets stopped. The API call operates at a lower level: it makes your project “go to sleep”, freeing resources from the Glitch server until a new call to the project or to the editor wakes it up again.