Sunday 16 June 2019

Why is my function called twice?

Hi!My first interaction with nodejs is adding a function that sends a notification with a small payload to Slack when called. I have finally accomplished that! There is a small issue however is when I added my function to a script that handles datastreams, on the exit function dataStream.on('end'...) my function notification('Success!', message, destination)gets called twice. Either the exit function gets ran twice or only my function.​Any idea why this happens?const _ = require('highland') const async = require('async') const { BigQuery } = require('@google-cloud/bigquery') const CIO = require('customerio-node') // init BigQuery const bigQuery = new BigQuery({ projectId: process.env.PROJECT_ID }) let queue = [] function queueProcessor() { async.eachLimit( queue.splice(0, 15), // take 15 elements (since most will have at least 2 calls 5, // 5 at a time processRow, // function that will execute the work, passed (row, callback) err => { if (err) { logger.error(err) } } ) } // Reset interval let queueThrottleProcessor = setInterval(() => { queueProcessor() // Process queue }, 1000) let dataStream = _( bigQuery .dataset(process.env.BIGQUERY_DATASET) .table(process.env.BIGQUERY_TABLE) .createReadStream() ) dataStream .ratelimit(15, 1000) .on('error', err => { logger.error(err) }) .on('data', row => { queue.push(row) }) .on('end', function() { // All rows have been retrieved. var message = `${numSynced} synced, total calls: ${totalCalls}` logger.info(message) notification('Success!', message, destination) clearInterval(queueThrottleProcessor) // Give 15 seconds for all the current processing to go through setTimeout(() => { // Done process.exit(0) }, 15000) })

Submitted June 17, 2019 at 07:13AM by UL_Paper

No comments:

Post a Comment