-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0f85a1
commit 1223120
Showing
9 changed files
with
44 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
port=50606 | ||
silent=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,28 @@ | ||
import cluster from "cluster"; | ||
import locks from "mongo-locks"; | ||
import mongoose from "mongoose"; | ||
import { LockManager } from "mongo-locks"; | ||
import { migrate } from "../models/migrations"; | ||
import env from "./env"; | ||
import { MongoClient } from "mongodb"; | ||
|
||
mongoose.Promise = global.Promise; // native promises | ||
const client = new MongoClient(env.database.bgs.url, { directConnection: true, ignoreUndefined: true }); | ||
|
||
let dbInit = false; | ||
const db = client.db(env.database.bgs.name); | ||
|
||
export default async function initDb(url = env.database.bgs.url, runMigrations = true) { | ||
if (dbInit) { | ||
console.log("DB already initialized"); | ||
return; | ||
} | ||
|
||
dbInit = true; | ||
|
||
const connect = () => | ||
mongoose | ||
.connect(url, { dbName: env.database.bgs.name, directConnection: true }) | ||
.then(() => console.log("successfully connected to database")); | ||
await connect(); | ||
const locks = new LockManager(db.collection("mongo-locks")); | ||
|
||
locks.init(mongoose.connection); | ||
await db | ||
.listCollections() | ||
.toArray() | ||
.then(() => { | ||
console.log("Connected to database"); | ||
}); | ||
|
||
if (cluster.isMaster && runMigrations) { | ||
let free = () => {}; | ||
try { | ||
free = await locks.lock("migration"); | ||
await migrate(); | ||
} catch (err) { | ||
console.error(err); | ||
} finally { | ||
free(); | ||
} | ||
} | ||
export const collections = {}; | ||
|
||
mongoose.connection.on("error", (err) => { | ||
console.error("db error", err); | ||
}); | ||
if (!env.isTest && cluster.isMaster) { | ||
await using lock = await locks.lock("db"); | ||
|
||
if (!env.script) { | ||
mongoose.connection.on("disconnected", () => { | ||
console.log("attempt to reconnect to database"); | ||
setTimeout(() => connect().catch(console.error), 5000); | ||
}); | ||
if (lock) { | ||
await migrate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters