annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local String = require "luan:String.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local to_number = String.to_number or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Time = require "luan:Time.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local time_now = Time.now or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local User = require "site:/lib/User.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local current_user = User.current or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Logging = require "luan:logging/Logging.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local logger = Logging.logger "heartbeat.js"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 return function()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local last_update = Http.request.parameters.last_update or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 last_update = to_number(last_update) or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local user = current_user() or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local user_last_update = user.last_update()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local now = time_now()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 if now - user_last_update < 10000 or last_update >= user_last_update then
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 logger.info "ok"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 end
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 logger.info "update"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 Io.stdout = Http.response.text_writer()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 %>
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 resync(<%=user_last_update%>);
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 <%
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 end