forked from oknosoft/windowbuilder-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfl_compact.js
More file actions
88 lines (74 loc) · 1.83 KB
/
Copy pathfl_compact.js
File metadata and controls
88 lines (74 loc) · 1.83 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
/**
* Обслуживание баз flowcon
*
* @module fl_compact
*
* Created by Evgeniy Malyarov on 04.07.2018.
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* DBPWD admin
* DBUSER admin
* COUCHPATH http://cou221:5984/wb_
*/
'use strict';
require('http').globalAgent.maxSockets = 35;
const debug = require('debug')('wb:reindex');
const PouchDB = require('./pouchdb').plugin({exec});
const fs = require('fs');
debug('required');
// инициализируем параметры сеанса и метаданные
const {DBUSER, DBPWD, COUCHPATH} = process.env;
const prefix = 'fl_';
let index = 1;
// получаем массив всех баз
new PouchDB(`${COUCHPATH}/_all_dbs`, {
auth: {
username: DBUSER,
password: DBPWD
},
skip_setup: true
}).info()
.then(next);
// перебирает базы в асинхронном цикле
function next(dbs) {
index++;
let name = dbs[index];
if(name && name.startsWith(prefix)) {
return reindex(name)
.then(() => next(dbs));
}
else if(name) {
return next(dbs);
}
}
function exec(method, path, form) {
return new Promise((resolve, reject) => {
this._ajax({
url: `${this.name}/${path}`,
auth: {
username: DBUSER,
password: DBPWD
},
method,
form,
}, (err, obj, resp) => {
debug(`${this.name.replace(/^(.*\/)/, '')}/${path}: ${JSON.stringify(err || obj)}`);
setTimeout(resolve, 2000);
});
});
}
function reindex(name) {
const db = new PouchDB(`${COUCHPATH}/${name}`, {
auth: {
username: DBUSER,
password: DBPWD
},
skip_setup: true
});
return db.exec('PUT', '_revs_limit', '3')
.then(() => db.exec('POST', '_compact', ''))
.then(() => db.exec('POST', '_view_cleanup', ''))
.then(() => db.close());
}