December 14, 2018 · JS Web

Simple Concurrency Control - ES6

let pool = new PromisePool(function () {
  try {
    let obj1 = json.urlset.url[currentLoop++]
    // console.log(util.inspect(
    //   {
    //     url: obj1.loc[0],
    //     name: obj1['image:image'][0]['image:title'][0],
    //     createdAt: moment(obj1['lastmod'][0], 'YYYY-M-DD').utcOffset('+0800').format('YYYY-MM-DD HH:mm'),
    //     others: obj1
    //   }, false, null, true))
    let nItem = {
      url: obj1.loc[0],
      name: null,
      others: obj1
    }
    try {
      nItem.name = obj1['image:image'][0]['image:title'][0]
    } catch (e) {
    }

    // process.exit()
    return entity.count({where: {url: nItem.url}}).then(count => {
      if (!count) {
        entity.create(nItem)
        console.log(moment().format('YYYY/M/D-H:mm:ss') + '+' + nItem.url)
      }
    })
  } catch (e) {
    console.log('on error' + e.message + e.stack)
    if (!--maxErrPerProcess) process.exit()
  }
}, 3)
const startPool = async () => {
  await pool.start()
}
startPool()