Mercurial Hosting > chat
diff src/heartbeat.js.luan @ 30:f4708943f29e
add heartbeat
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 09 Nov 2024 19:17:21 -0700 |
parents | |
children | e2b7f6393dab |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/heartbeat.js.luan Sat Nov 09 19:17:21 2024 -0700 @@ -0,0 +1,30 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local String = require "luan:String.luan" +local to_number = String.to_number or error() +local Time = require "luan:Time.luan" +local time_now = Time.now or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local User = require "site:/lib/User.luan" +local current_user = User.current or error() +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "heartbeat.js" + + +return function() + local last_update = Http.request.parameters.last_update or error() + last_update = to_number(last_update) or error() + local user = current_user() or error() + local user_last_update = user.last_update() + local now = time_now() + if now - user_last_update < 10000 or last_update >= user_last_update then + logger.info "ok" + return + end + logger.info "update" + Io.stdout = Http.response.text_writer() +%> + resync(<%=user_last_update%>); +<% +end