private Optional < Long > getMaybeTaskLogReadOffset ( final String slaveHostname , final String fullPath , final Long logLength , Optional < SingularityTask > task ) { long offset = 0 ; long maxOffset = smtpConfiguration . get ( ) . getMaxTaskLogSearchOffset ( ) ; Optional < Pattern > maybePattern = getLogErrorRegex ( task ) ; Pattern pattern ; if ( maybePattern . isPresent ( ) ) { pattern = maybePattern . get ( ) ; } else { LOG . trace ( "Could not get regex pattern. Reading from bottom of file instead." ) ; return getMaybeTaskLogEndOfFileOffset ( slaveHostname , fullPath , logLength ) ; } long length = logLength + pattern . toString ( ) . length ( ) ; // Get extra so that we can be sure to find the error Optional < MesosFileChunkObject > logChunkObject ; Optional < MesosFileChunkObject > previous = Optional . absent ( ) ; while ( offset <= maxOffset ) { try { logChunkObject = sandboxManager . read ( slaveHostname , fullPath , Optional . of ( offset ) , Optional . of ( length ) ) ; } catch ( RuntimeException e ) { LOG . error ( "Sandboxmanager failed to read {} on slave {}" , fullPath , slaveHostname , e ) ; return Optional . absent ( ) ; } if ( logChunkObject . isPresent ( ) ) { if ( logChunkObject . get ( ) . getData ( ) . equals ( "" ) ) { // Passed end of file if ( previous . isPresent ( ) ) { // If there was any log, get the bottom bytes of it long end = previous . get ( ) . getOffset ( ) + previous . get ( ) . getData ( ) . length ( ) ; return Optional . of ( end - logLength ) ; } return Optional . absent ( ) ; } Matcher matcher = pattern . matcher ( logChunkObject . get ( ) . getData ( ) ) ; if ( matcher . find ( ) ) { return Optional . of ( offset + matcher . start ( ) ) ; } else { offset += logLength ; } } else { // Couldn't read anything LOG . error ( "Failed to read log file {}" , fullPath ) ; return Optional . absent ( ) ; } previous = logChunkObject ; } LOG . trace ( "Searched through the first {} bytes of file {} and didn't find an error. Tailing bottom of file instead" , maxOffset , fullPath ) ; return getMaybeTaskLogEndOfFileOffset ( slaveHostname , fullPath , logLength ) ; }
Searches through the file looking for first error