def handler()

in sqs-lambda-eb-cdk-python/lambda/submit_job.py [0:0]


def handler(event, context):
    print("Lambda function invoked")
    print(json.dumps(event))

    qUrl = os.environ['QUEUE_URL']

    #change this value according to the limits of your downstream service
    max_jobs_to_submit = 5

    total_jobs_scheduled = 0
    i = 0
    hit_limit = False
    total_messages = 0

    while(i < max_jobs_to_submit):
        messages = getMessagesFromQueue(qUrl)
        if(messages):

            total_messages = len(messages)
            print("Total messages: {}".format(total_messages))

            for message in messages:
                receipt_handle = message['ReceiptHandle']

                try:
                    if(hit_limit):
                        changeVisibility(qUrl, receipt_handle)
                    else:
                        #SUBMIT JOB TO THE DOWNSTREAM SERVICE
                        print("Submitting job to downstream service...")

                        ### CATCH ERROR FROM DOWNSTREAM SERVICE ###
                        #Implement getting the error response from the service the an error cannot be thrown

                        # Delete received message from queue
                        deleteMessagesFromQueue(qUrl, receipt_handle)
                        total_jobs_scheduled += 1
                        i += 1
                except Exception as e:
                    print("Error while starting job: {}".format(e))
                    changeVisibility(qUrl, receipt_handle)

                    ### CATCH ERROR FROM DOWNSTREAM SERVICE ###
                    #ERROR FROM DOWNSTREAM SERVICE (if the code used to submit jobs throws an error)
                    #IF ERROR, THE DOWNSTREAM SERVICE CANNOT ACCEPT ANY JOBS ANYMORE -> STOP SUBMITTING AND STOP THE WHILE LOOP
                    if(e.__class__.__name__ == 'LimitExceededException'
                        or e.__class__.__name__ == "ProvisionedThroughputExceededException"):
                        hit_limit = True
                        i = max_jobs_to_submit

        else:
            i = max_jobs_to_submit


    output = "Started {} jobs.".format(total_jobs_scheduled)
    if(hit_limit):
        output += " Hit limit."

    if hit_limit or (total_jobs_scheduled > 0) :
        print(output)

    return {
        'statusCode': 200,
        'body': output
    }