async def main(mytimer: func.TimerRequest):
    """
        Ensure flushing messages happens during these times:
        1. When script has reached 90% of execution time
        2. When files accumulated has crossed MAX_ACCUMULATED_FILE_SIZE
        3. When there are no more messages in SQS
        4. Check if user chose a specific log type or wants all logs types to be processed, in this case, lesser storage

    Args:
        mytimer (func.TimerRequest): timer set in function app
    """

    script_start_time = int(time.time())

    async with _create_sqs_client() as client:
        mainQueueHelper = AzureStorageQueueHelper(
            connectionString=AZURE_STORAGE_CONNECTION_STRING,
            queueName=AZURE_STORAGE_PRIMARY_QUEUE,
        )
        backlogQueueHelper = AzureStorageQueueHelper(
            connectionString=AZURE_STORAGE_CONNECTION_STRING,
            queueName=AZURE_STORAGE_BACKLOG_QUEUE,
        )
        files_processed = 0
        accumulated_file_size = (
            0  # logic is to accumulate file sizes upto MAX_ACCUMULATED_FILE_SIZE
        )
        file_arr = []

        while True:
            try:
                # This should return MaxNumberOfMessages message from SQS only
                response = await client.receive_message(
                    QueueUrl=SQS_QUEUE_URL,
                    MaxNumberOfMessages=10,
                    WaitTimeSeconds=2,
                    VisibilityTimeout=VISIBILITY_TIMEOUT,
                )

                if "Messages" in response:  # this is an array
                    for msg in response["Messages"]:
                        body_obj = json.loads(msg["Body"])
                        file_path, file_size, bucket_name = process_sqs_body(body_obj)

                        if (
                            file_path is None
                        ):  # case when sqs message doesnt have any records in it
                            return

                        # decide whether audit or network traffic or both should be consumed or not
                        if skip_processing_file(file_path):
                            logging.warn(
                                "[AWSQueue] Skipping file since logs to be consumed is {}, but file is {}".format(
                                    LOGS_TO_CONSUME, file_path
                                )
                            )
                            await delete_file_from_sqs(client, msg, body_obj)
                            continue

                        # decide which network traffic file paths to consume
                        if skip_processing_network_traffic_file(
                            file_path, NETWORK_TRAFFIC_TO_CONSUME
                        ):
                            logging.warn(
                                "[AWSQueue] Skipping network traffic file since logs to be consumed is {}, but file is {}".format(
                                    NETWORK_TRAFFIC_TO_CONSUME, file_path
                                )
                            )
                            await delete_file_from_sqs(client, msg, body_obj)
                            continue

                        files_processed += 1

                        accumulated_file_size += file_size

                        file_arr.append(
                            {
                                "link": urllib.parse.unquote(file_path),
                                "file_size": file_size,
                                "bucket_name": bucket_name,
                                "sqs_message_id": msg["MessageId"],
                            }
                        )

                        await delete_file_from_sqs(client, msg, body_obj)

                        # ensure to return if files processed are more than the limit
                        if files_processed >= SQS_FILES_READ_LIMIT:
                            logging.warn(
                                "[AWSQueue] Have processed {} files and hence exiting".format(
                                    files_processed
                                )
                            )
                            await enqueue_message_azure(
                                mainQueueHelper, backlogQueueHelper, file_arr
                            )
                            file_stats = {
                                "Trigger": "Timer",
                                "Type": "FileStats",
                                "file_count": len(file_arr),
                                "azure_queue_size": get_string_size(file_arr),
                                "aggregated_file_size": accumulated_file_size,
                            }
                            logging.info(json.dumps(file_stats))
                            return

                        if check_if_script_runs_too_long(0.90, script_start_time):
                            logging.warn(
                                "[AWSQueue]SQS Queue manager has run close to 90 percentage of max time. Flushing files to queue before termination"
                            )
                            await enqueue_message_azure(
                                mainQueueHelper, backlogQueueHelper, file_arr
                            )
                            file_stats = {
                                "Trigger": "Timer",
                                "Type": "FileStats",
                                "file_count": len(file_arr),
                                "azure_queue_size": get_string_size(file_arr),
                                "aggregated_file_size": accumulated_file_size,
                            }
                            logging.info(json.dumps(file_stats))
                            return

                        # decide whether file size accumulated so far has reached limit or not
                        # if not, wait, else if nothing else left, just add it to queue and terminate function

                        if accumulated_file_size >= MAX_ACCUMULATED_FILE_SIZE:
                            await enqueue_message_azure(
                                mainQueueHelper, backlogQueueHelper, file_arr
                            )
                            logging.info(
                                "[AWSQueue] Crossed the max file size limit, enqueing messages in azure queue"
                            )
                            file_stats = {
                                "Trigger": "Timer",
                                "Type": "FileStats",
                                "file_count": len(file_arr),
                                "azure_queue_size": get_string_size(file_arr),
                                "aggregated_file_size": accumulated_file_size,
                            }
                            logging.info(json.dumps(file_stats))
                            accumulated_file_size = 0
                            file_arr.clear()

                else:
                    logging.info(
                        "[AWSQueue] There are no messages in SQS, attempting to enqueue files seen so far"
                    )
                    await enqueue_message_azure(
                        mainQueueHelper, backlogQueueHelper, file_arr
                    )
                    file_stats = {
                        "Trigger": "Timer",
                        "Type": "FileStats",
                        "file_count": len(file_arr),
                        "azure_queue_size": get_string_size(file_arr),
                        "aggregated_file_size": accumulated_file_size,
                    }
                    logging.info(json.dumps(file_stats))
                    return

            except Exception as e:
                logging.warning(traceback.format_exc())
                return

        Ensure flushing messages happens during these times:
        1. When script has reached 90% of execution time
        2. When files accumulated has crossed MAX_ACCUMULATED_FILE_SIZE
        3. When there are no more messages in SQS
        4. Check if user chose a specific log type or wants all logs types to be processed, in this case, lesser storage

    Args:
        mytimer (func.TimerRequest): timer set in function app
    
Solutions/IllumioSaaS/Data Connectors/TimedSQSFunctionApp/aws_queue.py