ashb commented on a change in pull request #3992: [AIRFLOW-620] Feature to tail 
custom number of logs instead of rendering whole log
URL: https://github.com/apache/airflow/pull/3992#discussion_r250557787
 
 

 ##########
 File path: airflow/utils/helpers.py
 ##########
 @@ -317,3 +317,45 @@ def render_log_filename(ti, try_number, 
filename_template):
                                     task_id=ti.task_id,
                                     
execution_date=ti.execution_date.isoformat(),
                                     try_number=try_number)
+
+
+def tail_file(filepath, lines):
+    """
+    Tail last n lines from give file.
+    :param filepath: Path to the file we have to tail
+    :param lines: Number of lines to tail
+    :return last n lines in the file
+    """
+    def get_byte_range_file(fl, num_bytes):
+        # Get current position of file pointer
+        current_position = fl.tell()
+        if current_position < num_bytes:
+            num_bytes = current_position
+        if current_position == 0:
+            return None
+        fl.seek(-num_bytes, 1)
+        data = str(fl.read(num_bytes), 'utf-8', 'ignore')
+        fl.seek(-num_bytes, 1)
+        return data
+
+    LINE_SIZE = 200  # Assuming each line will have 200 bytes
+    lines_to_fetch = lines
+    data = ""
+
+    with open(filepath, 'rb') as fl:  # Seek to a position from cur pointer is 
not supported in read text mode
+        fl.seek(0, 2)
+        while True:
+            num_bytes = lines_to_fetch * LINE_SIZE  # Calculate number of 
bytes to fetch based on lines
+            tail_chunk = get_byte_range_file(fl, num_bytes)
+            if tail_chunk is not None:
+                data = tail_chunk + data
+            else:
+                print('Reached starting of the file.Exiting')
 
 Review comment:
   This print will show up.. somewhere unhelpful (like in the stdout of the 
webserver?) so should be removed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to