fix(esx/server): race condition

Use await on queries during startup for less nesting / callback hell, and to delay the paycheck and save intervals from starting too early.
This commit is contained in:
Linden
2022-02-14 15:29:42 +11:00
parent 31b7252101
commit a1cfd5f84e
2 changed files with 55 additions and 34 deletions
+51 -34
View File
@@ -29,45 +29,62 @@ local function StartDBSync()
end
MySQL.ready(function()
MySQL.query('SELECT * FROM items', function(result)
for k,v in ipairs(result) do
ESX.Items[v.name] = {
label = v.label,
weight = v.weight,
rare = v.rare,
canRemove = v.can_remove
}
end
end)
local items = MySQL.query.await('SELECT * FROM items')
for k, v in ipairs(items) do
ESX.Items[v.name] = {
label = v.label,
weight = v.weight,
rare = v.rare,
canRemove = v.can_remove
}
end
local Jobs = {}
MySQL.query('SELECT * FROM jobs', {}, function(jobs)
for k,v in ipairs(jobs) do
Jobs[v.name] = v
Jobs[v.name].grades = {}
local jobs = MySQL.query.await('SELECT * FROM jobs')
for _, v in ipairs(jobs) do
Jobs[v.name] = v
Jobs[v.name].grades = {}
end
local jobGrades = MySQL.query.await('SELECT * FROM job_grades')
for _, v in ipairs(jobGrades) do
if Jobs[v.job_name] then
Jobs[v.job_name].grades[tostring(v.grade)] = v
else
print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name))
end
end
MySQL.query('SELECT * FROM job_grades', {}, function(jobGrades)
for k,v in ipairs(jobGrades) do
if Jobs[v.job_name] then
Jobs[v.job_name].grades[tostring(v.grade)] = v
else
print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name))
end
end
for _, v in pairs(Jobs) do
if ESX.Table.SizeOf(v.grades) == 0 then
Jobs[v.name] = nil
print(('[^3WARNING^7] Ignoring job ^5"%s"^0due to no job grades found'):format(v2.name))
end
end
for k2,v2 in pairs(Jobs) do
if ESX.Table.SizeOf(v2.grades) == 0 then
Jobs[v2.name] = nil
print(('[^3WARNING^7] Ignoring job ^5"%s"^0due to no job grades found'):format(v2.name))
end
end
ESX.Jobs = Jobs
print('[^2INFO^7] ESX ^5Legacy^0 initialized')
StartDBSync()
StartPayCheck()
end)
end)
if not Jobs then
-- Fallback data, if no jobs exist
ESX.Jobs['unemployed'] = {
label = 'Unemployed',
grades = {
['0'] = {
grade = 0,
label = 'Unemployed',
salary = 200,
skin_male = {},
skin_female = {}
}
}
}
else
ESX.Jobs = Jobs
end
print('[^2INFO^7] ESX ^5Legacy^0 initialized')
StartDBSync()
StartPayCheck()
end)
RegisterServerEvent('esx:clientLog')
+4
View File
@@ -16,6 +16,8 @@ loadPlayer = loadPlayer..' FROM `users` WHERE identifier = ?'
if Config.Multichar then
AddEventHandler('esx:onPlayerJoined', function(src, char, data)
while not next(ESX.Jobs) do Wait(50) end
if not ESX.Players[src] then
local identifier = char..':'..ESX.GetIdentifier(src)
if data then
@@ -28,6 +30,8 @@ if Config.Multichar then
else
RegisterNetEvent('esx:onPlayerJoined')
AddEventHandler('esx:onPlayerJoined', function()
while not next(ESX.Jobs) do Wait(50) end
if not ESX.Players[source] then
onPlayerJoined(source)
end