@volter/teams 0.1.30 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server/core.mjs +25 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volter/teams",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.32",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
|
|
6
6
|
"license": "MIT",
|
package/server/core.mjs
CHANGED
|
@@ -57,16 +57,13 @@ export async function bootstrapServer(api,{initTeam,bootstrap}){
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/** Expire cursors, idempotency records, staged captures and uploads, and old journal rows. */
|
|
60
|
-
export async function maintain(api){const now=api.clock.nowMs();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
{sql:'DELETE FROM uploads WHERE committed=0 AND created_at+86400000<=?',params:[now]},
|
|
68
|
-
{sql:'DELETE FROM journal WHERE created_at+86400000<=?',params:[now]},
|
|
69
|
-
]);
|
|
60
|
+
export async function maintain(api){const now=api.clock.nowMs();
|
|
61
|
+
// Deletes go first, each in a transaction of its own: on a full database an insert fails, and a run that begins
|
|
62
|
+
// with one never reaches the deletes that would make room.
|
|
63
|
+
// A committed capture's parts are its messages in transit; once committed, the session's version holds them, so
|
|
64
|
+
// they go (a backlog of them filled the database).
|
|
65
|
+
const parts=await api.store.query(`SELECT p.team_id,p.capture_id,p.part_index FROM capture_parts p JOIN captures c ON c.team_id=p.team_id AND c.id=p.capture_id WHERE c.state='committed' LIMIT ${PART_PRUNE_BATCH}`,[]);
|
|
66
|
+
for(let at=0;at<parts.length;at+=50)await api.store.transaction(parts.slice(at,at+50).map(({team_id,capture_id,part_index})=>({sql:'DELETE FROM capture_parts WHERE team_id=? AND capture_id=? AND part_index=?',params:[team_id,capture_id,part_index]})));
|
|
70
67
|
// Every capture writes its session a new version with a full copy of its messages, so versions pile up (hundreds
|
|
71
68
|
// per session) until listing them outgrew a Worker's memory. Kept: each session's current version, the newest
|
|
72
69
|
// version from each of its sources (the next capture and a correlation read that one), and every version an
|
|
@@ -77,13 +74,23 @@ export async function maintain(api){const now=api.clock.nowMs();await api.store.
|
|
|
77
74
|
AND NOT EXISTS (SELECT 1 FROM cursors c, json_each(c.snapshot_json) j WHERE c.team_id=v.team_id AND c.kind IN ('list','search') AND c.expires_at>? AND json_extract(j.value,'$[0]')=v.session_id AND json_extract(j.value,'$[1]')=v.revision)
|
|
78
75
|
AND NOT EXISTS (SELECT 1 FROM cursors c WHERE c.team_id=v.team_id AND c.kind='history' AND c.expires_at>? AND json_extract(c.snapshot_json,'$.session_id')=v.session_id AND json_extract(c.snapshot_json,'$.revision')=v.revision)
|
|
79
76
|
LIMIT ${VERSION_PRUNE_BATCH}`,[now,now]);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
for(const {team_id,session_id,revision} of stale)await api.store.transaction(['messages','session_artifacts','session_versions'].map(table=>({sql:`DELETE FROM ${table} WHERE team_id=? AND session_id=? AND revision=?`,params:[team_id,session_id,revision]})));
|
|
78
|
+
await api.store.transaction([
|
|
79
|
+
{sql:'DELETE FROM cursors WHERE expires_at<=?',params:[now]},
|
|
80
|
+
{sql:'DELETE FROM global_cursors WHERE expires_at<=?',params:[now]},
|
|
81
|
+
{sql:'DELETE FROM global_idempotency WHERE expires_at<=?',params:[now]},
|
|
82
|
+
{sql:'DELETE FROM journal WHERE created_at+86400000<=?',params:[now]},
|
|
83
|
+
]).catch(()=>{});
|
|
84
|
+
await api.store.transaction([
|
|
85
|
+
{sql:'INSERT OR IGNORE INTO capture_expiries(team_id,source_id,id,expired_at) SELECT team_id,source_id,id,? FROM captures WHERE state=\'staging\' AND created_at+86400000<=?',params:[now,now]},
|
|
86
|
+
{sql:'DELETE FROM captures WHERE state=\'staging\' AND created_at+86400000<=?',params:[now]},
|
|
87
|
+
{sql:'INSERT OR IGNORE INTO capture_expiries(team_id,source_id,id,expired_at) SELECT team_id,source_id,id,? FROM uploads WHERE committed=0 AND created_at+86400000<=?',params:[now,now]},
|
|
88
|
+
{sql:'DELETE FROM uploads WHERE committed=0 AND created_at+86400000<=?',params:[now]},
|
|
89
|
+
]).catch(()=>{});
|
|
90
|
+
const result={pruned_versions:stale.length,pruned_parts:parts.length,backlog:stale.length>=VERSION_PRUNE_BATCH||parts.length>=PART_PRUNE_BATCH};
|
|
91
|
+
if(result.pruned_versions||result.pruned_parts)console.log(`maintain: pruned ${result.pruned_parts} parts, ${result.pruned_versions} versions${result.backlog?' (backlog)':''}`);
|
|
92
|
+
return result;}
|
|
86
93
|
/** Session versions `maintain` removes per run: small enough for one Durable Object alarm; a backlog runs again in seconds. */
|
|
87
|
-
const VERSION_PRUNE_BATCH=
|
|
94
|
+
const VERSION_PRUNE_BATCH=20;
|
|
88
95
|
/** Committed capture parts `maintain` removes per run. */
|
|
89
|
-
const PART_PRUNE_BATCH=
|
|
96
|
+
const PART_PRUNE_BATCH=50;
|