@hvedinich/db 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +30 -8
- package/src/migration/index.js +55 -56
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
const migration = require('./migration');
|
|
3
3
|
|
|
4
|
-
const init = config => {
|
|
4
|
+
const init = ({ config, setStatus }) => {
|
|
5
5
|
const db = mongoose.connection;
|
|
6
|
-
|
|
6
|
+
let retry = 0;
|
|
7
|
+
const maxCount = 10;
|
|
8
|
+
|
|
7
9
|
const state = { connected: false };
|
|
8
10
|
const opt = {
|
|
9
11
|
useNewUrlParser: true,
|
|
@@ -13,29 +15,49 @@ const init = config => {
|
|
|
13
15
|
useUnifiedTopology: true,
|
|
14
16
|
useFindAndModify: false,
|
|
15
17
|
};
|
|
16
|
-
|
|
18
|
+
|
|
19
|
+
mongoose.connect(config.mongo.url, opt).catch(err => {
|
|
20
|
+
if (retry >= maxCount) {
|
|
21
|
+
setStatus(false);
|
|
22
|
+
}
|
|
23
|
+
console.log(`consction error: ${err}`);
|
|
24
|
+
});
|
|
25
|
+
|
|
17
26
|
db.on('disconnected', async () => {
|
|
27
|
+
console.log(`Can not connect to mongo retry: ${retry}`);
|
|
18
28
|
state.connected = false;
|
|
19
|
-
if (retry <
|
|
29
|
+
if (retry < maxCount) {
|
|
30
|
+
retry = retry + 1;
|
|
20
31
|
await new Promise(res => setTimeout(res, 1000));
|
|
21
|
-
|
|
32
|
+
try {
|
|
33
|
+
await mongoose.connect(config.mongo.url, opt);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if (retry >= maxCount) {
|
|
36
|
+
setStatus(false);
|
|
37
|
+
}
|
|
38
|
+
console.log(`retry error: ${err?.message || JSON.stringify(err)}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (retry >= maxCount) {
|
|
42
|
+
setStatus(false);
|
|
22
43
|
}
|
|
23
|
-
console.log('Can not connect to mongo');
|
|
24
44
|
});
|
|
25
45
|
|
|
26
46
|
db.on('error', () => {
|
|
27
47
|
console.log('connection error:');
|
|
28
48
|
mongoose.disconnect();
|
|
29
49
|
});
|
|
30
|
-
db.
|
|
50
|
+
db.on('open', () => {
|
|
31
51
|
console.log(`Mongo conected to ${config.mongo.url}`);
|
|
32
52
|
state.connected = true;
|
|
53
|
+
setStatus(true);
|
|
54
|
+
retry = 0;
|
|
33
55
|
});
|
|
34
56
|
|
|
35
57
|
const createModel = ({ name, schema }) => mongoose.model(name, schema);
|
|
36
58
|
|
|
37
59
|
const runMigrations = (migrations, log = console) =>
|
|
38
|
-
migration.run(createModel, config.serviceName, migrations, log, state);
|
|
60
|
+
migration.run(createModel, config.serviceName, migrations, log, state, setStatus);
|
|
39
61
|
|
|
40
62
|
return {
|
|
41
63
|
createModel,
|
package/src/migration/index.js
CHANGED
|
@@ -1,71 +1,70 @@
|
|
|
1
1
|
/* eslint-disable no-await-in-loop */
|
|
2
2
|
const schema = require('./schema');
|
|
3
3
|
|
|
4
|
+
const run = async (createModel, serviceName, migrations = [], log, state, setStatus) => {
|
|
5
|
+
log.debug('Migration: waiting for connection');
|
|
6
|
+
let count = 0;
|
|
7
|
+
const maxCount = 60;
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (!currentMigration.inProgress) {
|
|
13
|
-
const updated = await model.findOneAndUpdate(
|
|
14
|
-
{ serviceName, inProgress: false },
|
|
15
|
-
{ $set: { inProgress: true } },
|
|
16
|
-
{ new: true },
|
|
17
|
-
);
|
|
18
|
-
if (updated) {
|
|
19
|
-
waiting = false;
|
|
20
|
-
result = currentMigration;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
await new Promise(res => setTimeout(res, 1000));
|
|
24
|
-
} else {
|
|
25
|
-
waiting = false;
|
|
26
|
-
}
|
|
9
|
+
while (!state.connected) {
|
|
10
|
+
count += 1;
|
|
11
|
+
if (count >= maxCount) setStatus(false);
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line no-await-in-loop
|
|
14
|
+
await new Promise(res => setTimeout(res, 1000));
|
|
15
|
+
console.log(`while (!state.connected)${count}`);
|
|
27
16
|
}
|
|
28
|
-
return result;
|
|
29
|
-
};
|
|
30
17
|
|
|
31
|
-
const run = async (createModel, serviceName, migrations = [], log, state) => {
|
|
32
|
-
log.debug('Migration: waiting for connection');
|
|
33
|
-
await new Promise(async (done) => {
|
|
34
|
-
while (!state.connected) {
|
|
35
|
-
// eslint-disable-next-line no-await-in-loop
|
|
36
|
-
await new Promise(res => setTimeout(res, 100));
|
|
37
|
-
}
|
|
38
|
-
done();
|
|
39
|
-
});
|
|
40
18
|
log.debug('Migration: Start');
|
|
41
19
|
const model = createModel({ name: 'Migration', schema });
|
|
42
|
-
try {
|
|
43
|
-
const currentMigration = await getOrWait(model, serviceName, log);
|
|
44
20
|
|
|
45
|
-
|
|
46
|
-
|
|
21
|
+
const migrationList = await model.find({}).lean();
|
|
22
|
+
if (migrationList.length > 1) {
|
|
23
|
+
throw new Error('The migration fails due to an incorrect number of migration objects.');
|
|
24
|
+
}
|
|
25
|
+
const currentMigration =
|
|
26
|
+
migrationList[0] || (await model.create({ serviceName, startTime: Date.now() + 60000 * 10 }));
|
|
27
|
+
const isPrevMigrationStuck = currentMigration.inProgress && (currentMigration.startTime || 0) < Date.now();
|
|
28
|
+
|
|
29
|
+
if (isPrevMigrationStuck) {
|
|
30
|
+
log.error(`Previous migration stuck`);
|
|
31
|
+
}
|
|
47
32
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
33
|
+
if (!currentMigration.inProgress || isPrevMigrationStuck) {
|
|
34
|
+
try {
|
|
35
|
+
log.debug(`Migration: last version ${currentMigration.version}`);
|
|
36
|
+
await model.updateOne({ serviceName }, { $set: { inProgress: true } }).lean();
|
|
51
37
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
38
|
+
const migrationToDo = migrations.filter(e => e.version > currentMigration.version);
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < migrationToDo.length; i += 1) {
|
|
41
|
+
log.debug(`Migration: update from ${currentMigration.version} to ${migrationToDo[i].version}`);
|
|
42
|
+
await migrationToDo[i].run();
|
|
43
|
+
|
|
44
|
+
await model
|
|
45
|
+
.updateOne(
|
|
46
|
+
{ serviceName },
|
|
47
|
+
{
|
|
48
|
+
$set: { version: migrationToDo[i].version },
|
|
49
|
+
$push: { history: { version: migrationToDo[i].version } },
|
|
50
|
+
},
|
|
51
|
+
{ upsert: true },
|
|
52
|
+
)
|
|
53
|
+
.lean();
|
|
54
|
+
}
|
|
55
|
+
log.debug('Migration: migrations are done');
|
|
56
|
+
} finally {
|
|
57
|
+
await model
|
|
58
|
+
.updateOne(
|
|
59
|
+
{ serviceName },
|
|
60
|
+
{
|
|
61
|
+
$set: { inProgress: false },
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
.lean();
|
|
60
65
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
await model.updateOne(
|
|
64
|
-
{ serviceName, inProgress: true },
|
|
65
|
-
{
|
|
66
|
-
$set: { inProgress: false },
|
|
67
|
-
},
|
|
68
|
-
).lean();
|
|
66
|
+
} else if (!isPrevMigrationStuck) {
|
|
67
|
+
log.debug('Migration is being performed by another instance');
|
|
69
68
|
}
|
|
70
69
|
};
|
|
71
70
|
|