How to turn off app from inside (& prevent glitch to restart over and over)

hello,

I have this glitch app made of an express server and a discord bot, and I would like to be able to turn it off without archiving it, from a local variable.

I managed to turn off the discord bot by sending no API_KEY, and if I let the express app run the app keeps going. But I want to be able to turn off the express server too!

If I disable both the discord bot and the express server, the app terminates, then restarts continuously. That could do the trick, but I’m not happy with the idea of glitch trying to turn on an app that is not supposed to run.

here is the project but this idea could be useful for another project that is only a discord bot.

What I have tried, if local variable says to disable the app, it to call process.exit(0) because I believed a finished process with no error would not restart. I was wrong

Does anyone know a way to let the app run for the contractual 5 minutes without being restarted over and over ?
I guess glitch still makes the app sleeps event if it restarts this way, but it’s to keep things clean.

Thank you !

Hi,

As far as I’m aware, there’s no way to turn off a Glitch project for a certain amount of time without archiving it.

But I do have an idea, I’m not sure if it’s gonna work.

You can create an env variable (like RUN_PROJECT) which defines the state of your project. Then at the top of the server.js file (after defining dotenv), you could add an if statement:

if (process.env.RUN_PROJECT === false) {
   // don't run code
   return;
}

So if you set RUN_PROJECT to false, your main file won’t execute but otherwise, it gets executed.

// edit

I’m doubtful about this, and I think you should use process.exit(0) but that could keep restarting the Glitch project.

// edit 2

Maybe turning off projects using an environmental variable could be requested as a feature! #feature-ideas

// edit 3

I’m sure there’s a better way to do this…

3 Likes

Thanks @khalby786 , I use ./src/CONFIG.yaml file for these variables since they are not critical and they’re open to all.

Your first idea is interresting, but it doesn’t work (return outside of function)

The best option I believe is your idea of a feature request. That would allow us to turn off our apps properly !

Another solution, which is dirty but works :

if (config.TURN_OFF) {
  const app = express() // create an empty express server
  app.listen(process.env.PORT) // make it listen
}
else {
  // run project
}

with an empty express app, the glitch program keeps running and nothing is served… but that’s ugly to my eyes

edits

  • corrected mistakes in code and path of Config file
1 Like

Indeed.

So I guess a #feature-ideas is the way to go, hope I helped! :slight_smile:

2 Likes

Ok i’ll post it. Thanks for the tip, i’m marking you as the answer ??? (i’m new to this discourse) until someone comes with a better idea

2 Likes

You can go into the terminal and send a kill signal to the bot process (have the bot print out it’s own pid since both the express server and the bot are in node I’m assuming). Usually when I do that glitch won’t restart the process

Thanks @javaarchive so the way i could do it is

  • to print the PID from inside with console.log(process.id)
  • then in terminal something like kill -9 {id} ?

isn’t that … a little brutal ?
You say that you do that and glitch doesn’t restart, but it will restart on edit, right ?

I’m a bit afraid :scream: of your solution

You can shift the ugliness around: have the program run, e.g.:

setTimeout(() => {}, 1000000000);
return;

and not actually listen. Then you’ll get your warm fuzzies with not having a blank site be served. You’ll perpetually get Glitch’s “Starting …” interstitial. Also the little animation on the app status bar in the editor will spin forever. Less ugly in some places, profoundly uglier in others.

I personally do this in the package.json scripts.start configuration, where I set it to something like sleep inf, rather than in the js.

@gui3 if you’re worried about problems, you can try setting process.exit() to run on a different signal number. A little googling should be able to tell you how to trigger a function to run on a specific signal. I believe you use a variant of the kill command to send the other signal (not exactly a good name)

Your point makes perfectly sense.

I believe, if I want things to be clean as possible, I would use a minimal server that shows the application is currently disabled on any request.

Your suggestion of sleep inf is kind of what I was originally looking for. It disables the application and may prevent glitch to restart continuously. (sorry i’m not used to linux systems and shell scripts in general)

The way glitch recognize an application and “knows” how to launch it is still opaque to me, I guess I will gather info about it.
This automatic no-configuration magik is really cool, but I hope there is a way to have hands on it, like a config file of some kind. Maybe it already exists, i’ll dive into it, and if someone has info or useful links to share, that’s really cool

1 Like