def _read_event_file()

in azurelinuxagent/ga/collect_telemetry_events.py [0:0]


    def _read_event_file(event_file_path):
        """
        Read the event file and return the data.
        :param event_file_path: Full path of the event file.
        :return: Event data in list or string format.
        """
        # Retry for reading the event file in case file is modified while reading
        # We except FileNotFoundError and ValueError to handle the case where the file is deleted or modified while reading
        error_count = 0
        while True:
            try:
                # Read event file and decode it properly
                with open(event_file_path, "rb") as event_file_descriptor:
                    event_data = event_file_descriptor.read().decode("utf-8")

                # Parse the string and get the list of events
                return json.loads(event_data)
            except Exception as e:
                if is_file_not_found_error(e) or isinstance(e, ValueError):
                    error_count += 1
                    if error_count >= NUM_OF_EVENT_FILE_RETRIES:
                        raise
                else:
                    raise
            time.sleep(EVENT_FILE_RETRY_DELAY)