Advertisement
vitareinforce

worker tarik dinamis

Apr 5th, 2023 (edited)
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios')
  2. const convert = require('xml2js').parseString
  3. const fs = require('fs')
  4. const moment = require('moment')
  5. const dotenv = require('dotenv')
  6. dotenv.config()
  7.  
  8. const header = {
  9.     'User-Agent': 'Chrome',
  10.     'Content-Type': 'text/xml;charset=UTF-8',
  11. }
  12.  
  13. const request_mesin = `<?xml version="1.0" encoding="utf-8"?>
  14. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  15.     <soap:Body>
  16.         <GetAttLog>
  17.             <ArgComKey xsi:type="xsd:integer">0</ArgComKey>
  18.             <Arg><PIN xsi:type="xsd:integer">All</PIN></Arg>
  19.         </GetAttLog>
  20.     </soap:Body>
  21. </soap:Envelope>`
  22.  
  23. let timeout = 0
  24. let ip_mesin = process.env.IP_MESIN3
  25. let newData = []
  26.  
  27. async function main(index) {
  28.  
  29.     try {
  30.         let execution_time = 0
  31.         let start_execution_time = moment()
  32.         axios({ method: 'POST',
  33.             url: 'http://'+ ip_mesin +'/iWsService',
  34.             headers: header, data: request_mesin, keepAlive: true, maxBodyLength: Infinity, maxContentLength: Infinity })
  35.         .then(function (res) {
  36.             execution_time = moment().diff(start_execution_time, 'milliseconds')
  37.             timeout = timeout + execution_time
  38.             console.log('waktu timeout = ' + timeout)
  39.            
  40.             console.log('tarik dari ' + ip_mesin)
  41.             // if(res.status = 200) {
  42.                 console.log("ambil data dari " + res.config ? res.config.url : null)
  43.                 if(res.data && res.data.length > 0) {
  44.                     convert(res.data.toString(), function (err, msg) {
  45.                         let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  46.                         console.log("Jumlah data yang ditarik " + arrayResponse.length)
  47.                         for(let i = 0; i < arrayResponse.length; i++) {
  48.  
  49.                             if(i >= index) {
  50.                                 // start get execution time
  51.                                 let start_execution_time = moment()
  52.                                 arrayResponse[i].IpAddress = ip_mesin
  53.                                 arrayResponse[i].Type = 'rutin'
  54.                                 newData.push(arrayResponse[i])
  55.                                 execution_time = moment().diff(start_execution_time, 'milliseconds')
  56.                                
  57.                                 if(execution_time <= 2) {
  58.                                     if(res.status = 200) {
  59.                                         timeout = timeout + execution_time
  60.                                         // console.log('di bawah 2 detik')
  61.                                         // console.log('waktu timeout = ' + timeout)
  62.                                         // next loop
  63.                                     } else {
  64.                                         timeout = timeout + execution_time
  65.                                         // console.log('di atas 2 detik')
  66.                                         // console.log('waktu timeout = ' + timeout)
  67.                                         setTimeout(() => {
  68.                                             console.log('Restarting on index ' + i)
  69.                                             main(i)
  70.                                         }, timeout)
  71.                                     }
  72.                                 } else {
  73.                                     execution_time = moment().diff(start_execution_time, 'miliseconds')
  74.                                     timeout = timeout + execution_time
  75.                                     // console.log('waktu timeout = ' + timeout)
  76.                                     setTimeout(() => {
  77.                                         console.log('restarting on index ' + i)
  78.                                         main(i)
  79.                                     }, timeout)
  80.                                 }
  81.  
  82.                                 if(i == arrayResponse.length - 1) {
  83.                                     if(arrayResponse.length == newData.length) {
  84.                                         fs.writeFile(ip_mesin + '.txt', JSON.stringify(newData), function (err) {
  85.                                             if (err) {
  86.                                                 return console.log(err);
  87.                                             }
  88.  
  89.                                             fs.closeSync(fs.openSync(ip_mesin + '.txt', 'r'))
  90.                                            
  91.                                             console.log('saved raw')
  92.                                             console.log("Jumlah data yang ditarik " + arrayResponse.length)
  93.                                             console.log('jumlah data yang disimpan = ' + newData.length)
  94.                                             console.log('waktu timeout = ' + timeout)
  95.                                             newData = []
  96.                                             timeout = 0
  97.  
  98.                                             setTimeout(() => {
  99.                                                 console.log('restarting after save')
  100.                                                 main(0)
  101.                                             }, (parseInt(timeout) + parseInt(process.env.TIMEOUT_MESIN)))
  102.                                         })
  103.                                     } else {
  104.                                         newData = []
  105.                                         timeout = 0
  106.  
  107.                                         setTimeout(() => {
  108.                                             console.log('restarting because not match')
  109.                                             main(0)
  110.                                         }, timeout)
  111.                                     }
  112.                                 }
  113.                             }
  114.                         }
  115.                     })
  116.                 // }
  117.             }
  118.         }).catch(function (error) {
  119.             console.log(error)
  120.             console.log('waiting')
  121.             newData = []
  122.             timeout = 0
  123.             setTimeout(() => {
  124.                 console.log('restarting on error')
  125.                 main(0)
  126.             }, timeout)
  127.         })
  128.     } catch(err) {
  129.         console.log(err)
  130.     }
  131. }
  132.  
  133. main(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement