qbcore-framework / qb-weathersync

Weather System Used With QB-Core :sun_behind_rain_cloud:
GNU General Public License v3.0
23 stars 142 forks source link

Fix: "Failed to retrieve real time from API, falling back to local time" #84

Open MerlinisaG opened 1 week ago

MerlinisaG commented 1 week ago

Replaced http with https inside of retrieveTimeFromApi function.

Describe Pull request This is to fix the error: "Failed to retrieve real time from API, falling back to local time"

https://github.com/qbcore-framework/qb-weathersync/issues/83

Questions (please complete the following information):

MerlinisaG commented 1 week ago

After more testing, I found that the error would still occur at random, just not after every restart, as in the results before the change above.

I added to the failedCount and Wait because it seems the actual deeper issue stems from the API that is being used, as it sometimes randomly can have a longer than usual delay to load the location and time information, here is where I did further testing to get the error to go away entirely:

-- THREAD LOOPS
CreateThread(function()
    local previous = 0
    local realTimeFromApi = nil
    local failedCount = 0

    while true do
        Wait(0)
        local newBaseTime = os.time(os.date("!*t")) / 2 + 360 --Set the server time depending of OS time
        if Config.RealTimeSync then
            newBaseTime = os.time(os.date("!*t")) --Set the server time depending of OS time
            if realTimeFromApi == nil then
                retrieveTimeFromApi(function(unixTime)
                    realTimeFromApi = unixTime -- Set the server time depending on real-time retrieved from API
                end)
            end
            while realTimeFromApi == nil do
                if failedCount > 60 then --DEFAULT 10 -- TESTING HERE
                    print("Failed to retrieve real time from API, falling back to local time")
                    break
                end
                failedCount = failedCount + 1
                Wait(1000) --DEFAULT 100 -- TESTING HERE
            end
            if realTimeFromApi ~= nil then
                newBaseTime = realTimeFromApi
            end
        end
        if (newBaseTime % 60) ~= previous then --Check if a new minute is passed
            previous = newBaseTime % 60 --Only update time with plain minutes, seconds are handled in the client
            if freezeTime then
                timeOffset = timeOffset + baseTime - newBaseTime
            end
            baseTime = newBaseTime
        end
    end
end)