CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/run/maintenance-blobs.js
Views: 687
1
#!/usr/bin/env node
2
/*
3
Delete expired rows in the database.
4
*/
5
6
const postgres = require("@cocalc/database");
7
8
const db = postgres.db({ ensure_exists: false });
9
10
let {
11
WAIT_BETWEEN_RUNS_S,
12
MAX_BLOBS_PER_RUN,
13
BLOBS_AT_ONCE,
14
COCALC_BLOB_STORE,
15
WAIT_BETWEEN_UPLOADS_S,
16
CUTOFF,
17
} = process.env;
18
19
if (WAIT_BETWEEN_RUNS_S == null) {
20
WAIT_BETWEEN_RUNS_S = "120";
21
}
22
if (MAX_BLOBS_PER_RUN == null) {
23
MAX_BLOBS_PER_RUN = "2000";
24
}
25
if (BLOBS_AT_ONCE == null) {
26
BLOBS_AT_ONCE = "10";
27
}
28
if (COCALC_BLOB_STORE == null) {
29
COCALC_BLOB_STORE = "/blobs";
30
}
31
if (WAIT_BETWEEN_UPLOADS_S == null) {
32
WAIT_BETWEEN_UPLOADS_S = "0";
33
}
34
if (CUTOFF == null) {
35
CUTOFF = "2 months";
36
}
37
38
function move_blobs_to_gcloud(cb) {
39
console.log(
40
`move_blobs_to_gcloud: copying up to ${MAX_BLOBS_PER_RUN} non-expiring blobs to bucket ${COCALC_BLOB_STORE} and deleting them from the database`
41
);
42
db.copy_all_blobs_to_gcloud({
43
bucket: COCALC_BLOB_STORE,
44
limit: parseInt(MAX_BLOBS_PER_RUN),
45
map_limit: parseInt(BLOBS_AT_ONCE),
46
repeat_until_done_s: 0,
47
remove: true,
48
throttle: parseFloat(WAIT_BETWEEN_UPLOADS_S),
49
cutoff: CUTOFF,
50
cb,
51
});
52
}
53
54
function go() {
55
console.log("go");
56
move_blobs_to_gcloud(function (err) {
57
if (err) {
58
throw Error(`error in move_blobs_to_gcloud -- ${err}`);
59
}
60
console.log(
61
`now waiting ${WAIT_BETWEEN_RUNS_S} seconds before doing another move_blobs_to_gcloud...`
62
);
63
setTimeout(go, parseFloat(WAIT_BETWEEN_RUNS_S) * 1000);
64
});
65
}
66
67
go();
68
69