aboutsummaryrefslogtreecommitdiff
path: root/174bg/database/volumes/pocketbase/pb_hooks
diff options
context:
space:
mode:
Diffstat (limited to '174bg/database/volumes/pocketbase/pb_hooks')
-rw-r--r--174bg/database/volumes/pocketbase/pb_hooks/main.pb.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/174bg/database/volumes/pocketbase/pb_hooks/main.pb.js b/174bg/database/volumes/pocketbase/pb_hooks/main.pb.js
new file mode 100644
index 0000000..b7d385d
--- /dev/null
+++ b/174bg/database/volumes/pocketbase/pb_hooks/main.pb.js
@@ -0,0 +1,29 @@
+routerAdd("GET", "/heartbeat", (e) => {
+ try {
+ const result = new DynamicModel({
+ "value": nullString(),
+ })
+
+ let dateNow = Date.now();
+
+ try {
+ $app.db()
+ .newQuery(`SELECT value FROM key_value_store WHERE key = "heartbeat"`)
+ .one(result);
+ } catch (error) {
+ return e.json(404, { "error": "Heartbeat not found" });
+ }
+
+ let heartbeatValue = parseInt(result.value);
+ let timeDifference = dateNow - heartbeatValue;
+ let withinThreshold = timeDifference <= 60000; // 60 seconds threshold
+
+ return e.json(200, { "timeDifference": timeDifference, "withinThreshold": withinThreshold });
+ } catch (error) {
+ return e.json(500, { "error": "Internal Server Error", "details": error.message });
+ }
+});
+
+cronAdd("heartbeat", "* * * * *", () => {
+ $app.db().newQuery(`UPDATE key_value_store SET value = ${Date.now()} WHERE key = "heartbeat"`).execute()
+}) \ No newline at end of file