Skip to content

Commit

Permalink
Merge pull request #835 from jglick/TailLog
Browse files Browse the repository at this point in the history
`TailLog` to support reopened files
  • Loading branch information
jglick authored Sep 19, 2024
2 parents 6ff4dcb + c604889 commit ff90091
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/main/java/org/jvnet/hudson/test/TailLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
import hudson.model.Job;
import hudson.model.Run;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.input.Tailer;
Expand Down Expand Up @@ -96,7 +101,45 @@ public TailLog withColor(PrefixedOutputStream.AnsiColor color) {
* @param job a {@link Job#getFullName}
*/
public TailLog(File buildDirectory, String job, int number) {
tailer = Tailer.create(new File(buildDirectory, "log"), new TailerListenerAdapter() {
var log = buildDirectory.toPath().resolve("log");
tailer = Tailer.builder().setDelayDuration(Duration.ofMillis(50)).setTailable(new Tailer.Tailable() {
// like TailablePath
@Override public long size() throws IOException {
return Files.size(log);
}
@Override public FileTime lastModifiedFileTime() throws IOException {
return Files.getLastModifiedTime(log);
}
@Override public boolean isNewer(FileTime fileTime) throws IOException {
return Files.getLastModifiedTime(log).compareTo(fileTime) > 0;
}
@Override public Tailer.RandomAccessResourceBridge getRandomAccess(String mode) throws FileNotFoundException {
if (!Files.isRegularFile(log)) {
throw new FileNotFoundException(log.toString());
}
return new Tailer.RandomAccessResourceBridge() {
long ptr;
@Override public long getPointer() throws IOException {
return ptr;
}
@Override public void seek(long pos) throws IOException {
ptr = pos;
}
@Override public int read(byte[] b) throws IOException {
// Unlike RandomAccessFileBridge, not sensitive to file handle:
try (var is = Files.newInputStream(log)) {
is.skipNBytes(ptr);
int r = is.read(b);
if (r > 0) {
ptr += r;
}
return r;
}
}
@Override public void close() throws IOException {}
};
}
}).setTailerListener(new TailerListenerAdapter() {
PrintStream ps;
@Override
public void handle(String line) {
Expand All @@ -114,7 +157,7 @@ public void handle(String line) {
finished.release();
}
}
}, 50);
}).get();
}

public void waitForCompletion() throws InterruptedException {
Expand All @@ -123,7 +166,7 @@ public void waitForCompletion() throws InterruptedException {

@Override
public void close() {
tailer.stop();
tailer.close();
}

}
68 changes: 68 additions & 0 deletions src/test/java/org/jvnet/hudson/test/TailLogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* The MIT License
*
* Copyright 2024 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jvnet.hudson.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import org.apache.commons.io.FileUtils;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public final class TailLogTest {

@Rule public final TemporaryFolder tmp = new TemporaryFolder();

@Test public void recreatedLog() throws Exception {
var dir = tmp.getRoot();
try (var tail = new TailLog(dir, "prj", 123).withColor(PrefixedOutputStream.Color.MAGENTA)) {
Thread.sleep(1000);
var log = new File(dir, "log");
try (var os = new FileOutputStream(log); var pw = new PrintWriter(os)) {
for (int i = 0; i < 10; i++) {
pw.println(i);
pw.flush();
Thread.sleep(500);
}
}
var log2 = new File(dir, "log.tmp");
FileUtils.copyFile(log, log2);
FileUtils.delete(log);
assertTrue(log2.renameTo(log));
try (var os = new FileOutputStream(log, true); var pw = new PrintWriter(os)) {
for (int i = 10; i < 20; i++) {
pw.println(i);
pw.flush();
Thread.sleep(500);
}
pw.println("Finished: WHATEVER");
}
tail.waitForCompletion();
}
}

}

0 comments on commit ff90091

Please sign in to comment.