From dae665f77ad8a98ebffe78bcc9a21bef85bafebf Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Wed, 12 Feb 2020 10:59:01 +0100 Subject: [PATCH] Cron job use async to not kill server paying rent --- fxmanifest.lua | 1 + server/main.lua | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index e4fe09c4..5f171a6d 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,6 +7,7 @@ description 'ESX Vehicle Shop' version '1.1.0' server_scripts { + '@async/async.lua', '@mysql-async/lib/MySQL.lua', '@es_extended/locale.lua', 'locales/br.lua', diff --git a/server/main.lua b/server/main.lua index 6ee336f6..3955bd95 100644 --- a/server/main.lua +++ b/server/main.lua @@ -234,7 +234,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:buyCarDealerVehicle', function(sourc TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account) if account.money >= modelPrice then account.removeMoney(modelPrice) - + MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', { ['@vehicle'] = model, ['@price'] = modelPrice @@ -311,7 +311,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function(source, c ['@vehicle'] = vehicle, ['@price'] = basePrice }) - + RemoveOwnedVehicle(plate) cb(true) end) @@ -420,24 +420,36 @@ AddEventHandler('esx_vehicleshop:setJobVehicleState', function(plate, state) end) function PayRent(d, h, m) - MySQL.Async.fetchAll('SELECT * FROM rented_vehicles', {}, function(result) + local tasks, timeStart = {}, os.clock() + print('[esx_vehicleshop] [^2INFO^7] Paying rent cron job started') + + MySQL.Async.fetchAll('SELECT owner, rent_price FROM rented_vehicles', {}, function(result) for i=1, #result, 1 do - local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner) + table.insert(tasks, function(cb) + local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner) - if xPlayer then -- message player if connected - xPlayer.removeAccountMoney('bank', result[i].rent_price) - xPlayer.showNotification(_U('paid_rental', ESX.Math.GroupDigits(result[i].rent_price))) - else -- pay rent by updating SQL - MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', { - ['@bank'] = result[i].rent_price, - ['@identifier'] = result[i].owner - }) - end + if xPlayer then -- message player if connected + xPlayer.removeAccountMoney('bank', result[i].rent_price) + xPlayer.showNotification(_U('paid_rental', ESX.Math.GroupDigits(result[i].rent_price))) + else -- pay rent by updating SQL + MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', { + ['@bank'] = result[i].rent_price, + ['@identifier'] = result[i].owner + }) + end - TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account) - account.addMoney(result[i].rent_price) + TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account) + account.addMoney(result[i].rent_price) + end) + + cb() end) end + + Async.parallelLimit(tasks, 5, function(results) end) + + local elapsedTime = os.clock() - timeStart + print(('[esx_vehicleshop] [^2INFO^7] Paying rent cron job took %s seconds'):format(elapsedTime)) end) end