Jump to content

Recommended Posts

Posted
12 minutes ago, Nutter said:

This would work too

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

	const vehicles = mp.vehicles.toArray();
	for(let i = 0; i < vehicles.length; i++) {
		const vehicle = vehicles[i];
		if(!vehicle || !mp.vehicles.exists(vehicle)) {
			continue;
		}

		let x = vehicle.position.x;
		let y = vehicle.position.y;
		let z = vehicle.position.z;
		let rx = vehicle.rotation.x;
		let ry = vehicle.rotation.y;
		let rz = vehicle.rotation.z;
		let plate = vehicle.numberPlate;
		let volume = vehicle.getVariable("Volume");
		connection.query("SELECT Garage FROM vehicledata WHERE Plate = ?", [plate], function (err, res) {
			let garage = res[0].Garage;
			if (garage < 1) {
				connection.query("UPDATE vehicledata SET PosX = ?, PosY = ?, PosZ = ?, Volume = ?, RotX = ?, RotY = ?, RotZ = ? WHERE Plate = ?", [x, y, z, volume, rx, ry, rz, plate], function (err,res) {
					if(err) {
						console.log(err);
					}
				});
			}
		});
	}

	mp.events.delayTermination = false;
});

 

Is there a reason to check if a vehicle exists when you're getting the vehicle pool? like it'll 100% be a vehicle that exists wouldn't it

Posted (edited)
8 hours ago, MrPancakers said:

Is there a reason to check if a vehicle exists when you're getting the vehicle pool? like it'll 100% be a vehicle that exists wouldn't it

Since it's a sequential loop, there is a possibility that another vehicle can be destroyed in other code because there is enough time to destroy the vehicle if the save logic was to take half a second to run for each vehicle.

So when that other vehicle is then being "saved", you will most likely get "Expired multiplayer object" exceptions if you don't check if it's in the pool.

It's especially important to check this for players as they can leave the server whenever they want.

I suppose another method would be to utilise Promise.all() so you can have some parallelism.

Edited by Nutter
  • 7 months later...
Posted

I'm not sure if the original poster had success with this or not because he never replied back, but I'm running into the same issue with the server shutdown/termination not running a MySQL query while the shutdown/termination is paused by the event. I've tested the latest example given, and it still doesn't seem to work for me in RAGE MP 1.1. The output for the query is fine, and it also works for me on player quit, but server shutdown/termination seems to have some kind of issue preventing MySQL connections from actually running. The server acts like it executes the code as it continues on, and throws no errors, but nothing is actually happening with it. All the code after it is also executed fine. Is it possible that server shutdown/termination silently unloads the packages before the delay is applied, and doesn't throw errors for them if a reference to them breaks? My only other theory is that maybe it blocks all outgoing connections silently during this process, leading to the issue both I and the original poster are having. Beyond that, I'm at a loss on how to proceed here.

  • 2 years later...
Posted (edited)

So as an update, this messy set of code seems to have finally fixed this for me. (Posting for anyone else running into this issue, as it is surprisingly hard to figure out).
 

mp.events.add("serverShutdown", async () =>
{
  mp.events.delayShutdown = true;
    /* Your code here */
  mp.events.delayShutdown = false;
});

if (process.platform === 'win32') {
  require('readline')
    .createInterface({
      input: process.stdin,
      output: process.stdout
    })
    .on('SIGINT', function () {
      process.emit('SIGINT');
    });
}

process.on('SIGTERM', async () => {
  /* Your code here */
  process.exit();
});

process.on('SIGKILL', async () => {
  /* Your code here */
  process.exit();
});

process.on('SIGINT', async () => {
  /* Your code here */
  process.exit();
});

 

This seems to cover both Windows and other OS use cases, as well as the original RAGE MP server function and other process ending methods. This is specific to Node.js, but I'm guessing it could also be adapted to C# and other languages too.

Edited by SugarD-x

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...