Jump to content

[NEED HELP] Saving all data before shutdown & Catching crash(es)


Atalyce

Recommended Posts

Hi, I've tried browsing through forums but still haven't found the right solution.

Currently, I have a command to save data:
 

Spoiler
command.add(["savedata"], (player, fullText, playerb, level) => {
  if (player.getVariable("account:adminLevel") < 5)
    return chat.error(player, `You don't have permission to use this command.`);

  mp.players.forEach((_player) => {
    if (_player.getVariable("character:loggedIn") === true) {
      _player.saveCharacter();
      _player.saveAccount();
      _player.saveItems();
    }
  });
  mp.vehicles.forEach((vehicle) => {
    if (vehicle.getVariable("vehicle:ownerID") != 0) veh.savePrivate(vehicle);
  });
  for (let i = 0; i < factions.length; i++) {
    factions[i].save();
  }
  for (let i = 0; i < houses.length; i++) {
    houses[i].save();
  }
  for (let i = 0; i < businesses.length; i++) {
    businesses[i].save();
  }
  for (let i = 0; i < industries.length; i++) {
    industries[i].save();
  }
  for (let i = 0; i < trashes.length; i++) {
    trashes[i].save();
  }
});

 

This works fine.

 

I also created this same code during the serverShutdown event, but every time I shut down the server, no data is saved.

Spoiler
mp.events.add({
    "serverShutdown" : () => {
        mp.events.delayShutdown = true;
        logger('Database', 'Saving server data...', 'info');
        mp.players.forEach(_player => {
            if(_player.getVariable('character:loggedIn') === true) {
                _player.saveCharacter();
                _player.saveAccount();
                _player.saveItems();
            }
        });
        mp.vehicles.forEach(vehicle => {
            if(vehicle.getVariable('vehicle:ownerID') != 0) veh.savePrivate(vehicle);
        });
        for(let i = 0; i < factions.length; i++)  {
            factions[i].save();
        }
        for(let i = 0; i < houses.length; i++)  {
            houses[i].save();
        }
        for(let i = 0; i < businesses.length; i++) {
            businesses[i].save();
        }
        for(let i = 0; i < industries.length; i++) {
            industries[i].save();
        }
        for (let i = 0; i < trashes.length; i++) {
            trashes[i].save();
        }
        logger('Database', 'All data has been saved.', 'info');
        logger('Server', 'Shutting down server.', 'info');
        mp.events.delayShutdown = false;
    }
});

 


I perform shutdown using a simple shell script that I created here:
 

Spoiler
#!/bin/bash

if systemctl is-active --quiet rageserv.service; then
    echo "Turning RageMP server off..."

    sudo systemctl kill --kill-who=main --signal=SIGINT rageserv.service

    echo "Shutdown complete."
else
    echo "RageMP service is not currently running."
fi

 

I'm confused about how to call the "serverShutdown" event. Can anyone provide me with insight?

 

Additionally, is there some kind of event that can capture a crash? Because currently, my server encountered an error:

Quote

** stack smashing detected **: terminated


then the ragemp I'm running as a service gets a signal kill

Quote

rageserv.service: Main process exited, code=killed, status=6/ABRT

As far as my current research goes, the possible source of the problem is within the server I'm using. However, while looking for its replacement, I'm curious, can we get a crash event? Something like the "serverShutdown" event, but this is for crashes, so that it can be handled to save data before the ragemp service needs to restart.

Thank you in advance.

Link to comment
Share on other sites

I have conducted several tests and found that actually when sending a message to stop, the eventShutdown process has been called. All the logs I created came out, but for some reason, when looping to perform the save, Ragemp doesn't want to wait, however. I have tried mp.events.delayTermination = true & mp.events.delayShutdown = true, but there's no change. Ragemp immediately terminates its service.

I also tried to modify the code like this

Spoiler

async function savePlayerData(player) {
    if (player.getVariable("character:loggedIn") === true) {
        await player.saveCharacter();
        await player.saveAccount();
        await player.saveItems();
    } else {
        throw new Error('Something not right.');
    }
}

mp.events.add("serverShutdown", async () => {
    mp.events.delayTermination = true;
    mp.events.delayShutdown = true;

    logger('Database', 'Saving server data...', 'info');
    const playerSavePromises = [];
    mp.players.forEach((_player) => {
        playerSavePromises.push(savePlayerData(_player));
    });
    await Promise.all(playerSavePromises);


    logger('Database', 'All players data has been saved.', 'info');

    mp.events.delayShutdown = false;
    mp.events.delayTermination = false;
}
);


btw, thank you for your advice, I have already saved the data with some rules. Right now, I've hit a dead end in saving the data during the shutdown process.

Edited by Atalyce
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...