11var AWS = require ( "aws-sdk" ) ;
22const util = require ( "util" ) ;
33var cwl = new AWS . CloudWatchLogs ( { apiVersion : '2014-03-28' } ) ;
4+ var maxRetryCounter = 3 ;
45
56async function createSubscriptionFilter ( lambdaLogGroupName , destinationArn , roleArn ) {
67 if ( destinationArn . startsWith ( "arn:aws:lambda" ) ) {
@@ -53,7 +54,7 @@ function filterLogGroups(event, logGroupRegex) {
5354 return false ;
5455}
5556
56- async function subscribeExistingLogGroups ( logGroups , counter ) {
57+ async function subscribeExistingLogGroups ( logGroups , retryCounter ) {
5758 var logGroupRegex = new RegExp ( process . env . LOG_GROUP_PATTERN , "i" ) ;
5859 var destinationArn = process . env . DESTINATION_ARN ;
5960 var roleArn = process . env . ROLE_ARN ;
@@ -73,9 +74,9 @@ async function subscribeExistingLogGroups(logGroups, counter) {
7374 }
7475 } , Promise . resolve ( ) ) ;
7576
76- if ( counter < 4 && failedLogGroupNames . length > 0 ) {
77- console . log ( "Retrying Subscription for Failed Log Groups due to throttling with counter number as " + counter ) ;
78- await subscribeExistingLogGroups ( failedLogGroupNames , counter + 1 ) ;
77+ if ( retryCounter <= maxRetryCounter && failedLogGroupNames . length > 0 ) {
78+ console . log ( "Retrying Subscription for Failed Log Groups due to throttling with counter number as " + retryCounter ) ;
79+ await subscribeExistingLogGroups ( failedLogGroupNames , retryCounter + 1 ) ;
7980 }
8081}
8182
@@ -100,7 +101,7 @@ function processExistingLogGroups(token, context, errorHandler) {
100101 } ) ;
101102 } ) ;
102103 var cb = async function ( data ) {
103- await subscribeExistingLogGroups ( data . logGroups , 0 ) ;
104+ await subscribeExistingLogGroups ( data . logGroups , 1 ) ;
104105 if ( data . nextToken ) { // if next set of log groups exists, invoke next instance of lambda
105106 console . log ( "Log Groups remaining...Calling the lambda again with token " + data . nextToken ) ;
106107 invoke_lambda ( context , data . nextToken , errorHandler ) ;
0 commit comments