def lambda_handler()

in source/sample-inlet-strategies/max_size_inlet.py [0:0]


def lambda_handler(event, context):
    """
    This function is responsible for processing an SNS message to update serving counter
    """

    print(event)
    msg = json.loads(event["Records"][0]["Sns"]["Message"])
    result = json.dumps({"message": "Nothing to process."})

    if msg:
        exited = None
        increment_by = 0
        parsed = urlparse(CORE_API_ENDPOINT)
        # create an authentication signer for AWS
        auth = BotoAWSRequestsAuth(aws_host=parsed.netloc,
                                    aws_region=CORE_API_REGION,
                                    aws_service='execute-api')
    
        if "exited" in msg:
            exited = int(msg["exited"])

        # Update tokens' status
        if "completed" in msg:
            status = 1
            update_tokens(status, msg["completed"], auth)
                
        if "abandoned" in msg: 
            status = -1
            update_tokens(status, msg["abandoned"], auth)
        
        # Call num_active_tokens and subtract result from max allowed users
        payload = {"event_id": EVENT_ID}
        response = requests.get(active_tokens_api, params=payload, auth=auth)
        active_tokens = response.json()["active_tokens"]
        capacity = MAX_SIZE - int(active_tokens)

        # Always use the value provided with "exited" for increment_serving_counter when provided 
        if exited is not None and exited < capacity:
            increment_by = exited
        # otherwise the sum of items in "completed" and "abandoned" lists
        elif num_updated_tokens < capacity: 
            increment_by = num_updated_tokens
        # but if capacity is less than exited value and num_updated_tokens, 
        # then use that value to increment serving counter
        else: 
            increment_by = capacity
        body = {
                "event_id": EVENT_ID,
                "increment_by": increment_by
        }
        print(f"exited: {exited}, num_updated_tokens: {num_updated_tokens}, capacity: {capacity}, increment_by: {increment_by}")
        # only increment counter if exited information was present or tokens were actually updated
        if exited or num_updated_tokens > 0:
            response = requests.post(increment_by_api, json=body, auth=auth)
            result = response.json()
    print(result)    
    return result