|
@@ -23,39 +23,52 @@ def get_queue(hashkey, num_queues):
|
|
|
|
|
|
|
|
|
|
def lambda_handler(event, context):
|
|
def lambda_handler(event, context):
|
|
- # Get required environment variables
|
|
|
|
- # SOURCE_SQS_ARN = os.environ["SOURCE_SQS_ARN"]
|
|
|
|
- SQS_PREFIX = os.environ["SQS_PREFIX"]
|
|
|
|
- NUM_QUEUES = int(os.environ["NUM_QUEUES"])
|
|
|
|
- HASH_JSONPATH = os.environ["HASH_JSONPATH"]
|
|
|
|
-
|
|
|
|
- client = boto3.client("sqs")
|
|
|
|
- urls = []
|
|
|
|
- for i in range(NUM_QUEUES):
|
|
|
|
- urls.append(client.get_queue_url(QueueName=f"{SQS_PREFIX}{i}")["QueueUrl"])
|
|
|
|
-
|
|
|
|
- try:
|
|
|
|
- jsonpath_expression = parse(HASH_JSONPATH)
|
|
|
|
- except Exception as e:
|
|
|
|
- raise "Could not parse jsonpath: '{HASH_JSONPATH}'. Error was: {str(e)}'"
|
|
|
|
-
|
|
|
|
- n = 0
|
|
|
|
- for record in event["Records"]:
|
|
|
|
- n += 1
|
|
|
|
- # Place the record onto the destination queue
|
|
|
|
- print(f"DEBUG: Got record: {json.dumps(record, default=str)}")
|
|
|
|
- hashkey = jsonpath_expression.find(record)
|
|
|
|
- if len(hashkey) > 0:
|
|
|
|
- queue = get_queue(hashkey=hashkey, num_queues=NUM_QUEUES)
|
|
|
|
- print(f"DEBUG: Queue is {queue}")
|
|
|
|
- else:
|
|
|
|
- print(
|
|
|
|
- f"WARNING: Using random queue for record: {json.dumps(record, default=str)}"
|
|
|
|
|
|
+ if event: # Checking for an event is a best practice, but no idea why
|
|
|
|
+ # Get required environment variables
|
|
|
|
+ # SOURCE_SQS_ARN = os.environ["SOURCE_SQS_ARN"]
|
|
|
|
+ SOURCE_SQS_URL = os.environ["SOURCE_SQS_URL"]
|
|
|
|
+ SQS_PREFIX = os.environ["SQS_PREFIX"]
|
|
|
|
+ NUM_QUEUES = int(os.environ["NUM_QUEUES"])
|
|
|
|
+ HASH_JSONPATH = os.environ["HASH_JSONPATH"]
|
|
|
|
+
|
|
|
|
+ client = boto3.client("sqs")
|
|
|
|
+ urls = []
|
|
|
|
+ for i in range(NUM_QUEUES):
|
|
|
|
+ urls.append(client.get_queue_url(QueueName=f"{SQS_PREFIX}{i}")["QueueUrl"])
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ jsonpath_expression = parse(HASH_JSONPATH)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ raise "Could not parse jsonpath: '{HASH_JSONPATH}'. Error was: {str(e)}'"
|
|
|
|
+
|
|
|
|
+ n = 0
|
|
|
|
+ for record in event["Records"]:
|
|
|
|
+ try:
|
|
|
|
+ n += 1
|
|
|
|
+ # Place the record onto the destination queue
|
|
|
|
+ print(f"DEBUG: Got record: {json.dumps(record, default=str)}")
|
|
|
|
+ hashkey = jsonpath_expression.find(record)
|
|
|
|
+ if len(hashkey) > 0:
|
|
|
|
+ queue = get_queue(hashkey=hashkey, num_queues=NUM_QUEUES)
|
|
|
|
+ print(f"DEBUG: Queue is {queue}")
|
|
|
|
+ else:
|
|
|
|
+ print(
|
|
|
|
+ f"WARNING: Using random queue for record: {json.dumps(record, default=str)}"
|
|
|
|
+ )
|
|
|
|
+ queue = random.randrange(NUM_QUEUES)
|
|
|
|
+ print(f"DEBUG: Random Queue is {queue}")
|
|
|
|
+ message = client.send_message(
|
|
|
|
+ QueueUrl=urls[queue], MessageBody=json.dumps(record, default=str)
|
|
|
|
+ )
|
|
|
|
+ print(f"DEBUG: Message submitted: {json.dumps(message, default=str)}")
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"ERROR: Could not process message. Error was: {str(e)}")
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ # Delete the message so it is not reprocessed.
|
|
|
|
+ # Google "partial batch response" for a potentially better way to do this.
|
|
|
|
+ response = client.delete_message(
|
|
|
|
+ QueueUrl=SOURCE_SQS_URL, ReceiptHandle=record["receiptHandle"]
|
|
)
|
|
)
|
|
- queue = random.randrange(NUM_QUEUES)
|
|
|
|
- print(f"DEBUG: Random Queue is {queue}")
|
|
|
|
- message = client.send_message(
|
|
|
|
- QueueUrl=urls[queue], MessageBody=json.dumps(record, default=str)
|
|
|
|
- )
|
|
|
|
- print(f"DEBUG: Message submitted: {json.dumps(message, default=str)}")
|
|
|
|
- return {"statusCode": 200, "body": f"Successfully fair queued {n} records"}
|
|
|
|
|
|
+ print(f"DEBUG: Message deleted: {json.dumps(response, default=str)}")
|
|
|
|
+ return {"statusCode": 200, "body": f"Successfully fair queued {n} records"}
|