-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcompactPlayerDb.js
86 lines (79 loc) · 2.34 KB
/
compactPlayerDb.js
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
const { wrappedRun } = require("./entryPoint");
const axios = require("axios").default;
const { CouchStorage } = require("./couchStorage");
const { COUCHDB_USER, COUCHDB_PASSWORD, COUCHDB_PROTO } = require("./env");
const COUCHDB_SERVER = "127.0.0.1:5989";
async function withRetry(func, num = 5, retryInterval = 5000) {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
return await func();
} catch (e) {
console.log(e);
if (num <= 0 || e.status === 403) {
throw e;
}
console.log(`Retrying (${num})`);
await new Promise((r) => setTimeout(r, Math.random() * retryInterval));
}
num--;
}
}
const URL_BASE = `${COUCHDB_PROTO}://${COUCHDB_USER}:${COUCHDB_PASSWORD}@${COUCHDB_SERVER}`;
Promise.allSettled =
Promise.allSettled ||
((promises) =>
Promise.all(
promises.map((p) =>
p
.then((v) => ({
status: "fulfilled",
value: v,
}))
.catch((e) => ({
status: "rejected",
reason: e,
}))
)
));
async function main() {
const dbStorage = new CouchStorage({
uri: `${URL_BASE}/_dbs`,
skipSetup: true,
});
const dbs = await dbStorage._db.allDocs({ startkey: "" });
for (const row of dbs.rows) {
if (!/^p\d+_/.test(row.id)) {
continue;
}
console.log(row.id);
const s = new CouchStorage({
uri: `${URL_BASE}/${row.id}`,
skipSetup: true,
});
await withRetry(() => axios.put(`${URL_BASE}/${row.id}/_revs_limit`, "1"));
for (const result of await Promise.allSettled(
[withRetry(() => s._db.compact({ interval: 50 }))].concat(
["basic", "extended"].map(async (x) => {
await withRetry(() => axios.post(`${URL_BASE}/${row.id}/_compact/${x}`, {}));
await new Promise((res) => setTimeout(res, 50));
while (
(await withRetry(() => axios.get(`${URL_BASE}/${row.id}/_design/${x}/_info`))).data.view_index
.compact_running !== false
) {
await new Promise((res) => setTimeout(res, 50));
}
})
)
)) {
if (result.status !== "fulfilled") {
throw result.reason || result;
}
}
s._db.close().catch(() => {});
}
}
if (require.main === module) {
wrappedRun(main);
}
// vim: sw=2:ts=2:expandtab:fdm=syntax