import {MongoClient} from "mongodb";


import {ThreadPool} from "./ThreadPool.js";


// const uri = "mongodb://lcc-spider:JIUsfhiad&^@54.183.159.9:27017/?authSource=admin&directConnection=true";
//
//
// const client = new MongoClient(uri);
// const database = client.db("env");
// const movies = database.collection("env_shape");
// // Query for a movie that has the title 'The Room'
// const query = {
// 'env.funcs': {$ne: null},
// };
//
// // Execute query
// const movie = await movies.aggregate([
// {$match: query},
// {$sample: {size: 1}}
// ])
// let res = await movie.toArray()
// console.log(res)
const pool = new ThreadPool(5);

let data = { name: '你好' }

// 异步任务
pool.submitAsync(end => {
      setTimeout(() => {
            console.log("任务一开始" + new Date().getTime());
            let num = 0;
            while (true) {
                  num++;
                  if (num > 100000000) {
                        console.log("任务一结束" + new Date().getTime());
                        break;
                  }
            }
            end();
      }, 1000);
});

//异步任务+入参
pool.submitAsync((end, data1) => {
      setTimeout(() => {
            let num = 0;
            console.log("任务二开始" + new Date().getTime() + ",data=" + JSON.stringify(data1));
            while (true) {
                  num++;
                  if (num > 100000000) {
                        console.log("任务二结束" + new Date().getTime());
                        break;
                  }
            }
            end();
      }, 1000);
}, data);

//同步任务
pool.submit(data1 => {

      console.log("任务一开始" + new Date().getTime() + ",data" + JSON.stringify(data1));
      let num = 0;
      while (true) {
            num++;
            if (num > 100000000) {
                  console.log("任务一结束" + new Date().getTime());
                  break;
            }
      }

}, data);

//同步任务+入参
pool.submit(data1 => {
      console.log("任务二开始" + new Date().getTime() + ",data" + JSON.stringify(data1));
      let num = 0;
      while (true) {
            num++;
            if (num > 100000000) {
                  console.log("任务二结束" + new Date().getTime());
                  break;
            }
      }
}, data);
