|
| 1 | +import json |
| 2 | +import os |
| 3 | +import time |
| 4 | +import sys |
| 5 | +import boto3 |
| 6 | +import senzing as sz |
| 7 | + |
| 8 | +from loglib import * |
| 9 | +log = retrieve_logger() |
| 10 | + |
| 11 | +try: |
| 12 | + log.info('Importing senzing_core library . . .') |
| 13 | + import senzing_core as sz_core |
| 14 | + log.info('Imported senzing_core successfully.') |
| 15 | +except Exception as e: |
| 16 | + log.error('Importing senzing_core library failed.') |
| 17 | + log.error(e) |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | +# TODO add DLQ logic (see DLG_TAG logging) |
| 21 | + |
| 22 | +Q_URL = os.environ['Q_URL'] |
| 23 | +SZ_CONFIG = json.loads(os.environ['SENZING_ENGINE_CONFIGURATION_JSON']) |
| 24 | + |
| 25 | +POLL_SECONDS = 20 # 20 seconds is SQS max |
| 26 | +HIDE_MESSAGE_SECONDS = 600 # SQS visibility timeout |
| 27 | + |
| 28 | +#------------------------------------------------------------------------------- |
| 29 | + |
| 30 | +def _make_boto_session(fpath=None): |
| 31 | + ''' |
| 32 | + If `AWS_PROFILE` environment variable is set, then `Session()` can be |
| 33 | + called with no arguments. |
| 34 | +
|
| 35 | + fpath is path to json file with keys: |
| 36 | + - aws_access_key_id |
| 37 | + - aws_secret_access_key |
| 38 | + - aws_session_token |
| 39 | + - region |
| 40 | + (Same keys should typically be present in .aws/credentials file |
| 41 | + if using profile.)''' |
| 42 | + if fpath: |
| 43 | + return boto3.Session(**json.load(open(fpath))) |
| 44 | + else: |
| 45 | + return boto3.Session() |
| 46 | + |
| 47 | +def _make_sqs_client(boto_session): |
| 48 | + return boto_session.client('sqs') |
| 49 | + |
| 50 | +def init(): |
| 51 | + '''Returns sqs client object''' |
| 52 | + try: |
| 53 | + sess = _make_boto_session() |
| 54 | + sqs = sess.client('sqs') |
| 55 | + log.info(AWS_TAG + 'SQS client object instantiated.') |
| 56 | + return sqs |
| 57 | + except Exception as e: |
| 58 | + log.error(AWS_TAG + str(e)) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | +def get_msgs(sqs, q_url): |
| 62 | + '''Generator function; returns a single SQS msg at a time. |
| 63 | + Pertinent keys in an SQS message include: |
| 64 | + - MessageId |
| 65 | + - ReceiptHandle -- you'll need this to delete the msg later |
| 66 | + - Body -- here, should be the JSONL record as a string |
| 67 | + ''' |
| 68 | + while 1: |
| 69 | + print('waiting for msg') |
| 70 | + try: |
| 71 | + log.info(AWS_TAG + 'Polling SQS for the next message') |
| 72 | + resp = sqs.receive_message(QueueUrl=q_url, MaxNumberOfMessages=1, |
| 73 | + WaitTimeSeconds=POLL_SECONDS) |
| 74 | + if 'Messages' in resp and len(resp['Messages']) == 1: |
| 75 | + yield resp['Messages'][0] |
| 76 | + except Exception as e: |
| 77 | + log.error(AWS_TAG + str(e)) |
| 78 | + |
| 79 | +def del_msg(sqs, q_url, receipt_handle): |
| 80 | + try: |
| 81 | + return sqs.delete_message(QueueUrl=q_url, ReceiptHandle=receipt_handle) |
| 82 | + except Exception as e: |
| 83 | + log.error(AWS_TAG + DLQ_TAG + 'SQS delete failure for ReceiptHandle: ' + |
| 84 | + ReceiptHandle + ' Additional info: ' + str(e)) |
| 85 | + |
| 86 | +#------------------------------------------------------------------------------- |
| 87 | + |
| 88 | +def go(): |
| 89 | + '''Starts the Consumer process; runs indefinitely.''' |
| 90 | + |
| 91 | + # SQS client |
| 92 | + sqs = init() |
| 93 | + |
| 94 | + # Spin up msgs generator |
| 95 | + log.info('Spinning up messages generator') |
| 96 | + msgs = get_msgs(sqs, Q_URL) |
| 97 | + |
| 98 | + # Senzing init tasks. |
| 99 | + sz_eng = None |
| 100 | + try: |
| 101 | + sz_factory = sz_core.SzAbstractFactoryCore("ERS", SZ_CONFIG) |
| 102 | + |
| 103 | + # Init senzing engine object. |
| 104 | + # Senzing engine object cannot be passed around between functions, |
| 105 | + # else it will be eagerly cleaned up / destroyed and no longer usable. |
| 106 | + sz_eng = sz_factory.create_engine() |
| 107 | + log.info(SZ_TAG + 'Senzing engine object instantiated.') |
| 108 | + except sz.SzError as sz_err: |
| 109 | + log.error(SZ_TAG + str(sz_err)) |
| 110 | + sys.exit(1) |
| 111 | + except Exception as e: |
| 112 | + log.error(str(e)) |
| 113 | + sys.exit(1) |
| 114 | + |
| 115 | + while 1: |
| 116 | + try: |
| 117 | + # Get next message. |
| 118 | + msg = next(msgs) |
| 119 | + receipt_handle, body = msg['ReceiptHandle'], msg['Body'] |
| 120 | + log.info('SQS message retrieved, having ReceiptHandle: ' |
| 121 | + + receipt_handle) |
| 122 | + rcd = json.loads(body) |
| 123 | + |
| 124 | + # Process and send to Senzing. |
| 125 | + resp = sz_eng.add_record(rcd['DATA_SOURCE'], rcd['RECORD_ID'], body, |
| 126 | + sz.SzEngineFlags.SZ_WITH_INFO) |
| 127 | + log.info(SZ_TAG + 'Successful add_record having ReceiptHandle: ' |
| 128 | + + receipt_handle) |
| 129 | + |
| 130 | + # Delete msg from queue. |
| 131 | + del_msg(sqs, Q_URL, receipt_handle) |
| 132 | + except sz.SzError as sz_err: |
| 133 | + log.error(SZ_TAG + DLQ_TAG + str(sz_err)) |
| 134 | + except Exception as e: |
| 135 | + log.error(str(e)) |
| 136 | + sys.exit(1) |
| 137 | + |
| 138 | +#------------------------------------------------------------------------------- |
| 139 | + |
| 140 | +def main(): |
| 141 | + log.info('====================') |
| 142 | + log.info(' CONSUMER') |
| 143 | + log.info(' STARTED') |
| 144 | + log.info('====================') |
| 145 | + go() |
| 146 | + |
| 147 | +if __name__ == '__main__': main() |
0 commit comments