def greengrass_internal_handler()

in functions/SharedFile1Python3/SharedFile1Python3.py [0:0]


def greengrass_internal_handler():
    try:
        # Generate a random integer, endpoints included
        if random.randint(RAND_MIN, RAND_MAX) == RAND_TARGET:
            # 10% of the time we're going to write some random data in the shared output file
            shared_output_file = open(SHARED_OUTPUT_FILE, 'w')
            shared_output = f"Random output from other function [{random.uniform(RAND_MIN, RAND_MAX):.2f}]"
            logger.info(f"Writing random output string [{shared_output}] to shared output file [{SHARED_OUTPUT_FILE}]")
            shared_output_file.write(shared_output)
            shared_output_file.close()

        if not os.path.exists(SHARED_INPUT_FILE):
            # Try again in 1 second
            logger.info('No data waiting in shared file, sleeping...')
            Timer(5, greengrass_internal_handler).start()
            return

        with open(SHARED_INPUT_FILE) as input:
            shared_input = input.read()

        # Delete the file so we don't pick it up again
        os.remove(SHARED_INPUT_FILE)

        # Send the payload to the expected topic
        payload['message'] = f"Data found in shared input file [{SHARED_INPUT_FILE}], it was [{shared_input}]"
        client.publish(topic=OUTPUT_TOPIC, payload=json.dumps(payload))
    except:
        # An exception occurred, log it and then retry the function
        e = sys.exc_info()[0]
        logger.error(f"Exception: {e}")

    # Asynchronously schedule this function to be run again in 5 seconds
    Timer(5, greengrass_internal_handler).start()