Add larger timeouts for caching

This commit is contained in:
Jim Shield
2025-06-30 23:19:23 +01:00
parent 1ed227080b
commit 1ff01526bc
2 changed files with 40 additions and 18 deletions

View File

@@ -41,7 +41,17 @@ local cache = {
Jobs = {}, Jobs = {},
Gangs = {}, Gangs = {},
} }
local cacheReady = false
local timers = {}
local function startTimer(label)
timers[label] = GetGameTimer()
end
local function endTimer(label)
timers[label] = GetGameTimer() - (timers[label] or GetGameTimer())
timers[label] = timers[label] / 1000
end
startTimer("Cache")
-- Helper function to check if resource exists in server (instead of if it is already started) -- Helper function to check if resource exists in server (instead of if it is already started)
local function checkExists(resourceName) local function checkExists(resourceName)
return GetResourceState(resourceName):find("start") or GetResourceState(resourceName):find("stopped") return GetResourceState(resourceName):find("start") or GetResourceState(resourceName):find("stopped")
@@ -78,6 +88,7 @@ end
---- Load Items ----- ---- Load Items -----
--------------------- ---------------------
-- Items initialization based on detected inventory system -- Items initialization based on detected inventory system
startTimer("Items")
if checkExists(Exports.OXInv) then if checkExists(Exports.OXInv) then
-- Wait for OX Inventory to start if it's not already started -- Wait for OX Inventory to start if it's not already started
while GetResourceState(Exports.OXInv) ~= "started" do Wait(100) end while GetResourceState(Exports.OXInv) ~= "started" do Wait(100) end
@@ -155,10 +166,12 @@ elseif checkExists(Exports.RSGExport) then
itemResource = Exports.RSGExport itemResource = Exports.RSGExport
cache.Items = exports[Exports.RSGExport]:GetCoreObject().Shared.Items cache.Items = exports[Exports.RSGExport]:GetCoreObject().Shared.Items
end end
endTimer("Items")
--------------------- ---------------------
--- Load Vehicles --- --- Load Vehicles ---
--------------------- ---------------------
startTimer("Vehicles")
-- Vehicle loading depending on framework -- Vehicle loading depending on framework
if checkExists(Exports.QBXExport) then if checkExists(Exports.QBXExport) then
vehResource = Exports.QBXExport vehResource = Exports.QBXExport
@@ -197,10 +210,12 @@ elseif checkExists(Exports.RSGExport) then
cache.Vehicles = exports[Exports.RSGExport]:GetCoreObject().Shared.Vehicles cache.Vehicles = exports[Exports.RSGExport]:GetCoreObject().Shared.Vehicles
end end
endTimer("Vehicles")
--------------------- ---------------------
----- Load Jobs ----- ----- Load Jobs -----
--------------------- ---------------------
startTimer("Jobs")
-- Jobs loading based on framework -- Jobs loading based on framework
if checkExists(Exports.QBXExport) then if checkExists(Exports.QBXExport) then
jobResource = Exports.QBXExport jobResource = Exports.QBXExport
@@ -267,6 +282,7 @@ elseif checkExists(Exports.RSGExport) then
cache.Jobs, cache.Gangs = exports[Exports.RSGExport]:GetCoreObject().Shared.Jobs, exports[Exports.RSGExport]:GetCoreObject().Shared.Gangs cache.Jobs, cache.Gangs = exports[Exports.RSGExport]:GetCoreObject().Shared.Jobs, exports[Exports.RSGExport]:GetCoreObject().Shared.Gangs
end end
endTimer("Jobs")
-- Fallback if nil or empty -- Fallback if nil or empty
if cache.Items == nil or not next(cache.Items) then if cache.Items == nil or not next(cache.Items) then
@@ -292,6 +308,8 @@ end
-- Forcefully load the the specified config file from inventory scripts -- Forcefully load the the specified config file from inventory scripts
-- This allows to get information required for certain functions that need to detect how much space is left in a players inventory -- This allows to get information required for certain functions that need to detect how much space is left in a players inventory
-- This is born from too many tickets of me needing to explain that they need to change "InventoryWeight" to match their inv setting -- This is born from too many tickets of me needing to explain that they need to change "InventoryWeight" to match their inv setting
startTimer("InvWeight")
startTimer("InvSlots")
local function getInventoryConfig(resource, data) local function getInventoryConfig(resource, data)
if data.convars then if data.convars then
return function(path) return function(path)
@@ -327,7 +345,6 @@ local function getInventoryConfig(resource, data)
end end
end end
-- Inventory table -- Inventory table
local invWeightTable = { local invWeightTable = {
[Exports.OXInv] = { convars = { [Exports.OXInv] = { convars = {
@@ -373,7 +390,8 @@ for script, data in pairs(invWeightTable) do
break break
end end
end end
endTimer("InvWeight")
endTimer("InvSlots")
CreateThread(function() CreateThread(function()
local counts = { local counts = {
@@ -383,15 +401,18 @@ CreateThread(function()
if type(v) ~= "number" then for count in pairs(v) do counts[k] += 1 end end if type(v) ~= "number" then for count in pairs(v) do counts[k] += 1 end end
end end
if cache.InventoryWeight then if cache.InventoryWeight then
print("^6FrameWorkCache^7: ^4"..invResource.."^2 InventoryWeight^7: ^3"..cache.InventoryWeight.."^7 (^3"..(cache.InventoryWeight / 1000).."kg^7)") print("^6FrameWorkCache^7: ^4"..invResource.."^2 InventoryWeight^7: ^3"..cache.InventoryWeight.."^7 (^3"..(cache.InventoryWeight / 1000).."kg^7) ^7("..timers["InvWeight"].."s)")
end end
if cache.InventorySlots then if cache.InventorySlots then
print("^6FrameWorkCache^7: ^4"..invResource.."^2 InventorySlots^7: ^3"..cache.InventorySlots.."^7") print("^6FrameWorkCache^7: ^4"..invResource.."^2 InventorySlots^7: ^3"..cache.InventorySlots.." ^7("..timers["InvSlots"].."s)")
end end
print("^6FrameworkCache^7: ^4"..itemResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Items).."^2 Items^7") print("^6FrameworkCache^7: ^4"..itemResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Items).."^2 Items ^7("..timers["Items"].."s)")
print("^6FrameworkCache^7: ^4"..vehResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Vehicles).."^2 Vehicles^7") print("^6FrameworkCache^7: ^4"..vehResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Vehicles).."^2 Vehicles ^7("..timers["Vehicles"].."s)")
print("^6FrameworkCache^7: ^4"..jobResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Jobs).."^2 Jobs^7") print("^6FrameworkCache^7: ^4"..jobResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Jobs).."^2 Jobs ^7("..timers["Jobs"].."s)")
print("^6FrameworkCache^7: ^4"..jobResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Gangs).."^2 Gangs^7") print("^6FrameworkCache^7: ^4"..jobResource:gsub("-", "^7-^4"):gsub("_", "^7_^4").."^2 Loaded ^3"..tostring(counts.Gangs).."^2 Gangs ^7("..timers["Jobs"].."s)")
endTimer("Cache")
print("^6FrameworkCache^7: ^2Cache Ready ^7("..timers["Cache"].."s)")
cacheReady = true
end) end)
RegisterNetEvent("jim_bridge:requestCache", function() RegisterNetEvent("jim_bridge:requestCache", function()
@@ -400,15 +421,16 @@ RegisterNetEvent("jim_bridge:requestCache", function()
end) end)
exports("GetSharedData", function() exports("GetSharedData", function()
-- Wait for data to be ready before returning it -- Wait indefinitely (with periodic checks) until cacheReady is true
local timeout = GetGameTimer() + 5000 local timeout = GetGameTimer() + 20000 -- 20 second failsafe timeout
while ( while not cacheReady and GetGameTimer() < timeout do
not cache or Wait(100)
(not cache.Items or next(cache.Items) == nil) or
(not cache.Vehicles or next(cache.Vehicles) == nil) or
(not cache.Jobs or next(cache.Jobs) == nil)
) and GetGameTimer() < timeout do
Wait(50)
end end
if not cacheReady then
print("^1ERROR^7: jim_bridge cache timed out waiting for data.")
return nil -- Signal clearly if cache never became ready
end
return cache return cache
end) end)

View File

@@ -34,7 +34,7 @@ end
if IsDuplicityVersion() then if IsDuplicityVersion() then
local cache = nil local cache = nil
local timeout = GetGameTimer() + 5000 -- 5 seconds max wait local timeout = GetGameTimer() + 20000 -- 20 seconds max wait
-- Wait until jim_bridge is started and export is available -- Wait until jim_bridge is started and export is available
while not cache and GetGameTimer() < timeout do while not cache and GetGameTimer() < timeout do