vendredi 30 avril 2021

Re-Running the function in case "if" condition fails or else continue

I am trying to get the sum of all the messages available in my multiple SQS queues using boto3. It is executing fine but sometimes only a few of the queues are returning data instead of all of them and the program is returning empty sets. How do I solve this problem using the "IF" condition so that the function re-runs if one of the sets is empty? PS: I am a newbie to programmming import boto3 import time from datetime import datetime, timedelta

sqs = boto3.client('sqs')
cloudwatch = boto3.client('cloudwatch')
asg = boto3.client('autoscaling')

response = sqs.list_queues(QueueNamePrefix ='Queue')
queuelist = response["QueueUrls"]

soa = 0.0
noi = 0.0
ABPI = 100

def SumOfAverages(queuelist, soa, response, cloudwatch):
    for eachqueue in queuelist:
        step0 = eachqueue.split('/')
        step1 = step0[4]
        response1 = cloudwatch.get_metric_statistics(
        Namespace='AWS/SQS',
        MetricName='ApproximateNumberOfMessagesVisible',
        Dimensions=[
            {
                'Name': 'QueueName',
                'Value': step1
            },
        ],
        StartTime=datetime.utcnow() - timedelta(minutes=1),
        EndTime=datetime.utcnow(),
        Period=60,
        Statistics=[
            'Average',
        ],
        
        Unit='Count'
        )
        datapoints = response1['Datapoints']
        print(datapoints)
        #*Need If Condition*
        for values in datapoints:
            averages = values['Average']
            soa += averages
    return(soa)

result = SumOfAverages(queuelist, soa, response, cloudwatch)
print(result)

Aucun commentaire:

Enregistrer un commentaire