Cleanup Code to run on exit

So I’m using nodejs to build an epic discord bot, and so far it’s been a success. However I’ve been wanting to introduce currency to my server. To do this I need a way to save and load info into files. The loading portion I’ve figured out, but I’m not sure how to make some code run when glitch shuts down or restarts my application. I’m thinking of a equavilent of the python atexit.register function. Basically I want a piece of code to run before the process gets killed/control-c/etc

This topic seems to cover the situations you’ve described

1 Like

Well, to save and load information I highly recommend using MySQL. It is easy to use, just access this website and you will quickly learn how to use it.MySQL Guide

There an exemple to save an info:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  var sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log(result.affectedRows + " record(s) updated");
  });
});
1 Like