Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
wow, global is gone
Browse files Browse the repository at this point in the history
  • Loading branch information
simon300000 committed Nov 10, 2019
1 parent aead83e commit 85830fb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
27 changes: 0 additions & 27 deletions packages/superspider-ana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ const bodyParser = require('koa-bodyparser')

const core = require('./module/core')

const MongoClient = require('mongodb').MongoClient

process.on('uncaughtException', (err) => {
console.log('ERR unc expt')
console.log(err)
})

global.amdb = false
global.utrdb = false

// schedule.scheduleJob('*/5 * * * *', adb)
;(async () => {
// Koa Init
Expand All @@ -45,27 +40,5 @@ global.utrdb = false

app.listen(2163)

// DB Init

const client = new MongoClient(
'mongodb://admin:' +
process.env.MONGODB_PASS +
'@' +
process.env.MONGODB_IP +
':27017/admin?authMechanism=DEFAULT',
{ useNewUrlParser: true }
)

try {
await client.connect()

global.amdb = client.db('amdb').collection('maindb')
global.utrdb = client.db('amdb').collection('utrdb')
} catch (err) {
console.log('ERR when connect to AMDB')
console.log(err)
process.exit(1)
}

schedule.scheduleJob('* * * * *', core)
})()
5 changes: 3 additions & 2 deletions packages/superspider-ana/module/core.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const rp = require('request-promise-native')
const fs = require('fs')

const { amdbP } = require('../utils/db')

const log = process.env.NODE_ENV == 'development' ? console.log : () => {}

module.exports = async function() {
if (!global.amdb) return
const amdb = global.amdb
const amdb = await amdbP

const lim = await fs.promises.readFile('/scdb/trlimit')

Expand Down
8 changes: 4 additions & 4 deletions packages/superspider-ana/module/untr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const rp = require('request-promise-native')

const conv = require('../utils/nameConv')

const { amdbP, utrdbP } = require('../utils/db')

const log = process.env.NODE_ENV == 'development' ? console.log : () => {}

module.exports = async function() {
if (!global.amdb) return
const amdb = global.amdb
if (!global.utrdb) return
const utrdb = global.utrdb
const amdb = await amdbP
const utrdb = await utrdbP

const cursor = amdb.find({})
while (await cursor.hasNext()) {
Expand Down
19 changes: 19 additions & 0 deletions packages/superspider-ana/utils/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const MongoClient = require('mongodb').MongoClient
// DB Init

const client = new MongoClient(
`mongodb://admin:${process.env.MONGODB_PASS}@${process.env.MONGODB_IP}:27017/admin?authMechanism=DEFAULT`,
{ useNewUrlParser: true }
)

const connect = client.connect().catch((err) => {
console.log('ERR when connect to AMDB')
console.log(err)
process.exit(1)
})

const amdbP = connect.then(() => client.db('amdb').collection('maindb'))

const utrdbP = connect.then(() => client.db('amdb').collection('utrdb'))

module.exports = { amdbP, utrdbP }

0 comments on commit 85830fb

Please sign in to comment.