-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteSnapshotsAndRecords.js
More file actions
105 lines (97 loc) · 3.13 KB
/
deleteSnapshotsAndRecords.js
File metadata and controls
105 lines (97 loc) · 3.13 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
/*
Download up to 1000 snapshots and delete each (and associated records) by id
*/
const fs = require('fs');
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
let anything = process.argv[2];
let type = process.argv[3] || 'MARC_BIB';
try {
if (!anything) {
throw new Error ('Usage: node deleteSnapshotsAndRecords <tenant OR jobExecutionId> [ <recordType> ]');
}
const wait = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
(async () => {
try {
let single;
let ttl = 0;
let skipped = 0;
let config = await getAuthToken(superagent);
if (anything.match(/........-....-....-....-............/)) {
single = anything;
} else if (config.tenant !== anything) {
throw new Error(`Tenant "${anything}" does not match the current tenant "${config.tenant}!`);
}
const endpoint = 'source-storage/snapshots';
const getUrl = config.okapi + '/' + endpoint + '?limit=1000';
let refData = {};
if (single) {
refData = { snapshots: [{ jobExecutionId: single }], totalRecords: 1 };
} else {
try {
console.log(`GET ${getUrl}`);
const res = await superagent
.get(getUrl)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
.set('accept', 'application/json');
refData = res.body;
} catch (e) {
console.log(e);
}
}
console.log(`Deleting ${refData.totalRecords} snapshots...`);
for (let x = 0; x < refData.snapshots.length; x++) {
let id = refData.snapshots[x].jobExecutionId;
let delFlag = false;
try {
let turl = `${config.okapi}/source-storage/records?recordType=${type}&snapshotId=${id}&limit=0`;
console.log(`GET ${turl}`);
let res = await superagent
.get(turl)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
if (res && res.body) {
if (res.body.totalRecords > 0) {
delFlag = true;
}
}
} catch (e) {}
if (single) delFlag = true;
if (delFlag) {
console.log(`Deleting ${id}`);
try {
await superagent
.delete(`${config.okapi}/source-storage/snapshots/${id}`)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
.set('accept', 'text/plain');
ttl++;
} catch (e) {
let msg = (e.response) ? e.response.text : e;
console.error(msg);
}
} else {
console.log(`INFO no ${type} records found with snapshotId ${id}-- skipping`);
skipped++;
}
await wait(config.delay);
}
console.log('Done!');
console.log('Snapshots deleted:', ttl);
console.log('Skipped:', skipped);
} catch (e) {
console.error(e.message);
}
})();
} catch (e) {
console.log(`${e}`);
}