SHARE
TWEET

Untitled

a guest Mar 24th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mongoose = require("mongoose");
  2. var SonicChannelIngest = require("sonic-channel").Ingest;
  3.  
  4. var MessageModel = mongoose.model("message", new mongoose.Schema({
  5.   session_id : String,
  6.   website_id : String,
  7.   type       : String,
  8.   content    : Object
  9. }));
  10.  
  11. var push_count = 0;
  12. var last_count = 0;
  13. var last_time_start = 0;
  14.  
  15. var worst_time = -1;
  16. var best_time = -1;
  17.  
  18. var push_next = function(sonicChannelIngest, cursor) {
  19.   cursor.next()
  20.     .then((message) => {
  21.       // Done?
  22.       if (message === null) {
  23.         console.info("All done. Bye.");
  24.  
  25.         process.exit(0);
  26.  
  27.         return;
  28.       }
  29.  
  30.       var content = (message.content || "").trim();
  31.  
  32.       if (content) {
  33.         var commited = sonicChannelIngest.push(
  34.           "messages", message.website_id, message.session_id, content,
  35.  
  36.           (error) => {
  37.             if (error) {
  38.               console.error("Ingest error", error);
  39.             }
  40.  
  41.             if (last_count++ === 1000) {
  42.               last_count = 1;
  43.  
  44.               var date_now = Date.now();
  45.  
  46.               var time_taken = ((date_now - last_time_start) / 1000);
  47.  
  48.               if (worst_time === -1 || time_taken > worst_time) {
  49.                 worst_time = time_taken;
  50.               }
  51.               if (best_time === -1 || time_taken < best_time) {
  52.                 best_time = time_taken;
  53.               }
  54.  
  55.               console.debug(
  56.                 "Step: cursor #" + push_count + "; " +
  57.                   "took " + time_taken + " sec (worst " + worst_time + " sec;" +
  58.                   " best " + best_time + " sec)"
  59.               );
  60.  
  61.               last_time_start = date_now;
  62.             }
  63.  
  64.             push_count++;
  65.  
  66.             push_next(sonicChannelIngest, cursor);
  67.           }
  68.         );
  69.  
  70.         if (commited !== true) {
  71.           console.warn("Not commited yet: #" + last_count);
  72.         }
  73.       } else {
  74.         push_next(sonicChannelIngest, cursor);
  75.       }
  76.     })
  77.     .catch((error) => {
  78.       console.error("Cursor error", error);
  79.  
  80.       process.exit(1);
  81.     });
  82. };
  83.  
  84. mongoose.connect("mongodb://localhost/messages", {
  85.   useNewUrlParser : true
  86. })
  87.   .then(() => {
  88.     console.info("Mongoose connected.");
  89.  
  90.     var sonicChannelIngest = new SonicChannelIngest({
  91.       host : "::1",
  92.       port : 1491,
  93.       auth : "SecretPassword"
  94.     }).connect({
  95.       connected : function() {
  96.         console.info("Sonic Channel succeeded to connect to host.");
  97.  
  98.         var cursor = MessageModel.find(
  99.           {
  100.             type : "text"
  101.           },
  102.  
  103.           {
  104.             website_id : true,
  105.             session_id : true,
  106.             content    : true
  107.           }
  108.         )
  109.           .cursor();
  110.  
  111.         last_time_start = Date.now();
  112.  
  113.         push_next(sonicChannelIngest, cursor);
  114.       },
  115.  
  116.       disconnected : function() {
  117.         console.error("Sonic Channel is now disconnected.");
  118.  
  119.         process.exit(1);
  120.       },
  121.  
  122.       timeout : function() {
  123.         console.error("Sonic Channel connection timed out.");
  124.       },
  125.  
  126.       retrying : function() {
  127.         console.error("Trying to reconnect to Sonic Channel...");
  128.       },
  129.  
  130.       error : function(error) {
  131.         console.error("Sonic Channel failed to connect to host.", error);
  132.       }
  133.     });
  134.   })
  135.   .catch((error) => {
  136.     console.error("Mongoose connect failed.", error);
  137.   });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top