最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我的节点脚本在完成功能后挂起

运维笔记admin16浏览0评论

我的节点脚本在完成功能后挂起

我的节点脚本在完成功能后挂起

我正在调用三个函数,这些函数完成后,我希望自己的脚本自行关闭,但只是挂起。

我已经尝试使函数基于异步/承诺,在每个'mongodb'类型的函数之后关闭数据库,并在函数内使用process.exit()作为最后调用的函数的回调。

连接到(本地-非Atlas)数据库:

MongoClient.connect(local, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, db) {
  if (err) {
    console.log(err)
  }
  else {
    console.log('Connected to MongoDB...')
    //Read in data from jsonfiles and store each file's contents into the database : This is where the functions are being called... within a successful connect to the MongoDB
    insertJSON(db, jsonfiles, 'requests', jsonfilesSource)
    insertJSON(db, issuedfiles, 'issuedLicenses', isssuedfilesSource)
    insertLicenses(db)
  }
  db.close()
})

功能1:

function insertJSON(db, dirBuf,collection, sourceFolder) {
  var database = db.db('license-server')
  var collection = database.collection(collection)
  fs.readdir(dirBuf, function(err, files) {
    if (err) {
      console.log(err.message)
    }
    else {
      files.forEach(function(filename) {
        var text = fs.readFileSync(sourceFolder + filename);
        var filecontents = JSON.parse(text)
        //collection.insertOne(filecontents)
        collection.findOne({"DisplayTitle" : filecontents.DisplayTitle, "NodeInformation" : filecontents.NodeInformation, "Date": filecontents.Date})
          .then(function(result) {
            if(result) {
              console.log(`An Item could already be in the database: A file is unique if its display title, nodeinformation, and date are different.
              the items display title is ${result.DisplayTitle}`)
              return
            }
            else {
              collection.insertOne(filecontents)
              console.log(`Added ${filecontents.DisplayTitle} to database`)
            }
          })
          .catch(function(error) {
            console.log(error)
          })
      })
    }
  })
}

功能2:

function insertLicenses(db) {
  // Set up GridFS to import .lic and .licx files into the database
  var database = db.db('license-server')
  var collection = database.collection('fs.files')
  var bucket = new mongodb.GridFSBucket(database);
  var dirBuf = Buffer.from('../license-server/private/licenses')
  fs.readdir(dirBuf, function(err, files) {
    if (err) {
      console.log(err.message)
    }
    else {
      files.forEach(function(filename) {
        collection.findOne({"filename": filename}).
        then(function(result) {
          if(result) {
            console.log(`The file ${filename} is already in the database`)
            return
          }
          else {
            fs.createReadStream('./private/licenses/' + filename).
            pipe(bucket.openUploadStream(filename)).
            on('error', function(error) {
              assert.ifError(error)
            }).
            on('finish', function() {
              console.log(`Uploaded ${filename}`)
            })
          }
        })
      })
    }
  })
// I tried calling db.close() here since this is the last function to be called. No luck.
}

我猜想这与mongodb函数有其自身的关闭方式有关,但我似乎无法找到我在以前尝试解决该问题时所寻找的东西。

预期结果应该是脚本关闭自身,实际结果是处理脚本。

回答如下:

所有这些数据库调用都是异步的-运行此代码的结果是立即调用db.close,然后在insertJSONinsertLicenses中进行工作。如果您要重写此代码以使用async/await(并且还需要更新其他功能),则db.close调用将关闭数据库,这将允许脚本退出:

  await insertJSON(db, jsonfiles, 'requests', jsonfilesSource)
  await insertJSON(db, issuedfiles, 'issuedLicenses', isssuedfilesSource)
  await insertLicenses(db)
  db.close()

https://developer.mozilla/en-US/docs/Learn/JavaScript/Asynchronous/Introducinghttps://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/async_function

发布评论

评论列表(0)

  1. 暂无评论