|
| 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 | +SZ_CONFIG = json.loads(os.environ['SENZING_ENGINE_CONFIGURATION_JSON']) |
| 21 | + |
| 22 | +# How long to wait before attempting next Senzing op. |
| 23 | +WAIT_SECONDS = int(os.environ.get('WAIT_SECONDS', 10)) |
| 24 | + |
| 25 | +#------------------------------------------------------------------------------- |
| 26 | + |
| 27 | +def go(): |
| 28 | + '''Starts the Redoer process; runs indefinitely.''' |
| 29 | + |
| 30 | + sz_eng = None |
| 31 | + try: |
| 32 | + sz_factory = sz_core.SzAbstractFactoryCore("ERS", SZ_CONFIG) |
| 33 | + |
| 34 | + # Init senzing engine object. |
| 35 | + # Senzing engine object cannot be passed around between functions, |
| 36 | + # else it will be eagerly cleaned up / destroyed and no longer usable. |
| 37 | + sz_eng = sz_factory.create_engine() |
| 38 | + log.info(SZ_TAG + 'Senzing engine object instantiated.') |
| 39 | + except sz.SzError as sz_err: |
| 40 | + log.error(SZ_TAG + str(sz_err)) |
| 41 | + sys.exit(1) |
| 42 | + except Exception as e: |
| 43 | + log.error(str(e)) |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + log.info('Starting primary loop.') |
| 47 | + |
| 48 | + # Approach: |
| 49 | + # - We don't try to both 'get' and 'process' in a single loop; instead we |
| 50 | + # 'get', then 'continue' to the next loop; the have_rcd flag is used to |
| 51 | + # facilitate this. |
| 52 | + # - Each Senzing call (3 distinct calls) is couched in its own try-except block for |
| 53 | + # robustness. |
| 54 | + tally = None |
| 55 | + have_rcd = 0 |
| 56 | + rcd = None |
| 57 | + while 1: |
| 58 | + try: |
| 59 | + |
| 60 | + if have_rcd: |
| 61 | + try: |
| 62 | + sz_eng.process_redo_record(rcd) |
| 63 | + rcd = None # <-- TODO this op might not be necessary |
| 64 | + have_rcd = 0 |
| 65 | + log.debug(SZ_TAG + 'Successfully redid one record.') |
| 66 | + continue |
| 67 | + except sz.SzRetryableError as sz_ret_err: |
| 68 | + # We'll try to process this record again. |
| 69 | + log.error(SZ_TAG + str(sz_ret_err)) |
| 70 | + time.sleep(WAIT_SECONDS) |
| 71 | + continue |
| 72 | + except sz.SzError as sz_err: |
| 73 | + log.error(SZ_TAG + str(sz_err)) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + else: |
| 77 | + try: |
| 78 | + tally = sz_eng.count_redo_records() |
| 79 | + log.debug(SZ_TAG + 'Current redo count: ' + str(tally)) |
| 80 | + except sz.SzRetryableError as sz_ret_err: |
| 81 | + log.error(SZ_TAG + str(sz_ret_err)) |
| 82 | + time.sleep(WAIT_SECONDS) |
| 83 | + continue |
| 84 | + except sz.SzError as sz_err: |
| 85 | + log.error(SZ_TAG + str(sz_err)) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + if tally: |
| 89 | + |
| 90 | + try: |
| 91 | + rcd = sz_eng.get_redo_record() |
| 92 | + if rcd: |
| 93 | + have_rcd = 1 |
| 94 | + # At this point, rcd var holds a record, and have_rcd flag |
| 95 | + # raised. Will process in the next loop. |
| 96 | + log.debug(SZ_TAG + 'Retrieved 1 record via get_redo_record()') |
| 97 | + else: |
| 98 | + log.debug(SZ_TAG + 'Redo count was greater than 0, but got ' |
| 99 | + + 'nothing from get_redo_record') |
| 100 | + except sz.SzRetryableError as sz_ret_err: |
| 101 | + log.error(SZ_TAG + str(sz_ret_err)) |
| 102 | + except sz.SzError as sz_err: |
| 103 | + log.error(SZ_TAG + str(sz_err)) |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + else: |
| 107 | + log.debug('No redo records. Will wait ' + str(WAIT_SECONDS) + ' seconds.') |
| 108 | + time.sleep(WAIT_SECONDS) |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + log.error(str(e)) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | +#------------------------------------------------------------------------------- |
| 115 | + |
| 116 | +def main(): |
| 117 | + log.info('====================') |
| 118 | + log.info(' ------->') |
| 119 | + log.info(' REDOER ') |
| 120 | + log.info(' STARTED') |
| 121 | + log.info(' ------->') |
| 122 | + log.info('====================') |
| 123 | + go() |
| 124 | + |
| 125 | +if __name__ == '__main__': main() |
0 commit comments