Timers in hibernation

I’m trying to create a restart script in lua, and I would need to use timers while server is empty. However, timer functions don’t seem to work when the server is hibernating.

I’ve also tried CurTime() and hooking to Think and Tick, but with no results.

What I’m basically trying to do:


local function DoRestartTimer(data)
	local time = CurTime()
	hook.Add("Tick", "RestartTimerTick", function()
		if CurTime() == time + 120 then
			time = CurTime()
			hook.Add("Tick", "RestartTimerTick2", function()
				if CurTime() == time + 60 then
					if #player.GetAll() < 1 then
						data.lastrestart = date
						data.shouldrestart = false
						file.Write("restartdata.txt", util.TableToJSON(data))
						RunConsoleCommand("_restart")
					end
				end
			end)
			hook.Remove("Tick", "RestartTimerTick")
		end
	end)
end

Anybody got some ideas to get it working?

Pretty sure that both Tick and Think don’t run if the server is in hibernation.

That’s kind of what I figured out :v:

If you absolutely need stuff running while players aren’t online, spawn a bot and kick it when someone joins. Just be aware it gets punted when the last person disconnects, too.

Timers started with timer.Create should still continue to run even when the server is empty.

You either had something that was preventing them from running, or the initial code you used was broken.

No, it’s because the server is in hibernation.

You can wake the server by spawning a bot and kicking it. (if the server doesn’t do it automatically, that is)

Then I’m experiencing a weird bug.

This is on a fresh install on Ubuntu 64-bit. I’m only running this bit of custom code while on gm_construct and sandbox because I never had issues with this kind of thing. I always just assumed that’s how it worked. I didn’t realize it’s NOT supposed to do this.



timer.Create("Am I just crazy", 5, 0, function() 
    print("CT:"..CurTime()..", Players: "..#player.GetAll()..", Timer is still running.")
end)


http://puu.sh/k8NEX/c87e73f3a3.png

Does it still work if you create the timer with a player on, then have that player leave?

Tested with both the PlayerConnect and PlayerInitialSpawn hooks to see if it mattered if the client initialized.



hook.Add("PlayerInitialSpawn", "Test Thing", function()
    if !timer.Exists("Test Timer") then
        timer.Create("Test Timer", 5, 0, function() 
            print("CT:"..CurTime()..", Players: "..#player.GetAll()..", Timer is still running.")
        end)
    end
end)


http://puu.sh/k8YVF/2631ed312b.png

The server doesn’t wake until a player joins after a restart so it’s not running from the get-go, it just doesn’t seem to hibernate after that initial connect.

This should work perfectly well. Thank you! :slight_smile: