-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (141 loc) · 5.05 KB
/
Copy pathindex.js
File metadata and controls
166 lines (141 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// for database cleanup
/*
Copyright (C) 2025 Anish Sarkar
This file is part of Loopr.
Loopr is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Loopr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Loopr. If not, see <https://www.gnu.org/licenses/>.
*/
import { Client, Databases, Query } from 'node-appwrite';
export default async function ({ res }) {
const client = new Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT_ID)
.setKey(process.env.APPWRITE_API_KEY);
const databases = new Databases(client);
try {
const MAX_RETENTION_DAYS = parseInt(process.env.MAX_RETENTION_DAYS) || 15;
const DELETION_BATCH_SIZE = parseInt(process.env.DELETION_BATCH_SIZE) || 25;
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - MAX_RETENTION_DAYS);
const cutoffString = cutoffDate.toISOString();
// Clean up old ping results
const oldResultShards = await databases.listDocuments(
process.env.DATABASE_ID,
process.env.RESULTS_COLLECTION_ID,
[Query.lessThan('date', cutoffString.split('T')[0])],
parseInt(process.env.QUERY_LIMIT) || 500
);
// Delete old results in parallel
if (oldResultShards.documents.length > 0) {
for (let i = 0; i < oldResultShards.documents.length; i += DELETION_BATCH_SIZE) {
const batch = oldResultShards.documents.slice(i, i + DELETION_BATCH_SIZE);
try {
await Promise.all(
batch.map((doc) =>
databases.deleteDocument(
process.env.DATABASE_ID,
process.env.RESULTS_COLLECTION_ID,
doc.$id
)
)
);
// console.log(`Deleted batch of ${batch.length} old result documents`);
} catch (error) {
console.error(`Error deleting batch starting at ${i}:`, error);
// Continue with next batch instead of failing
}
}
}
// Clean up inactive worker nodes (last heartbeat > 1 day ago)
const dayAgo = new Date();
dayAgo.setDate(dayAgo.getDate() - 1);
const staleNodes = await databases.listDocuments(
process.env.DATABASE_ID,
process.env.WORKER_NODES_COLLECTION_ID,
[Query.lessThan('lastHeartbeat', dayAgo.toISOString())],
parseInt(process.env.QUERY_LIMIT) || 500
);
if (staleNodes.documents.length > 0) {
// Reassign URLs BEFORE marking nodes offline (more efficient)
await reassignUrls(databases, staleNodes.documents);
// Now mark nodes offline in a single batch
await Promise.all(
staleNodes.documents.map((node) =>
databases.updateDocument(
process.env.DATABASE_ID,
process.env.WORKER_NODES_COLLECTION_ID,
node.$id,
{ status: 'offline' }
)
)
);
}
return res.json({
success: true,
deletedResults: oldResultShards.documents.length,
offlineNodes: staleNodes.documents.length
});
} catch (error) {
console.error('Cleanup error:', error);
return res.json(
{
success: false,
error: error.message
},
500
);
}
}
// Helper function to reassign URLs from offline nodes to active ones
async function reassignUrls(databases, offlineNodes) {
if (offlineNodes.length === 0) return;
// Get active nodes
const activeNodes = await databases.listDocuments(
process.env.DATABASE_ID,
process.env.WORKER_NODES_COLLECTION_ID,
[Query.equal('status', 'online')],
parseInt(process.env.QUERY_LIMIT) || 500
);
if (activeNodes.documents.length === 0) return; // No active nodes to reassign to
const REASSIGNMENT_BATCH_SIZE = parseInt(process.env.REASSIGNMENT_BATCH_SIZE) || 25;
// For each offline node, reassign its URLs in batches
for (const offlineNode of offlineNodes) {
const orphanedUrls = await databases.listDocuments(
process.env.DATABASE_ID,
process.env.URLS_COLLECTION_ID,
[Query.equal('nodeId', offlineNode.$id)],
parseInt(process.env.ORPHANED_URL_QUERY_LIMIT) || 1000
);
if (orphanedUrls.documents.length === 0) continue;
// Process URL reassignments in batches
for (let i = 0; i < orphanedUrls.documents.length; i += REASSIGNMENT_BATCH_SIZE) {
const batch = orphanedUrls.documents.slice(i, i + REASSIGNMENT_BATCH_SIZE);
const updates = batch.map((url, index) => {
// Round-robin assignment
const targetNodeIndex = (i + index) % activeNodes.documents.length;
const targetNode = activeNodes.documents[targetNodeIndex];
return databases.updateDocument(
process.env.DATABASE_ID,
process.env.URLS_COLLECTION_ID,
url.$id,
{ nodeId: targetNode.$id }
);
});
try {
await Promise.all(updates);
// console.log(`Reassigned batch of ${batch.length} URLs from offline node`);
} catch (error) {
console.error(`Error reassigning batch:`, error);
// Continue with next batch
}
}
}
}