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_r250556929
 
 

 ##########
 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)
 
 Review comment:
   ```suggestion
           fl.seek(0, os.SEEK_END)
   ```

----------------------------------------------------------------
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