@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hvedinich/db",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "db",
5
5
  "main": "index.js",
6
6
  "scripts": {
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
- const retry = 0;
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
- mongoose.connect(config.mongo.url, opt);
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 < 10) {
29
+ if (retry < maxCount) {
30
+ retry = retry + 1;
20
31
  await new Promise(res => setTimeout(res, 1000));
21
- await mongoose.connect(config.mongo.url, opt);
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.once('open', () => {
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,
@@ -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
- const getOrWait = async (model, serviceName, log) => {
6
- let waiting = true;
7
- let result = { version: 0 };
8
- while (waiting) {
9
- log.debug('getOrWait: waiting for db');
10
- const currentMigration = await model.findOne({ serviceName }).lean().exec();
11
- if (currentMigration) {
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
- log.debug(`Migration: last version ${currentMigration.version}`);
46
- const migrationToDo = migrations.filter(e => e.version > currentMigration.version);
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
- for (let i = 0; i < migrationToDo.length; i += 1) {
49
- log.debug(`Migration: update from ${currentMigration.version} to ${migrationToDo[i].version}`);
50
- await migrationToDo[i].run();
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
- await model.updateOne(
53
- { serviceName },
54
- {
55
- $set: { version: migrationToDo[i].version },
56
- $push: { history: { version: migrationToDo[i].version } },
57
- },
58
- { upsert: true },
59
- ).lean();
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
- log.debug('Migration: migrations are done');
62
- } finally {
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