Last active
March 3, 2025 19:51
-
-
Save ghale/6805747979016647253e7503a1aa3477 to your computer and use it in GitHub Desktop.
Attempt to reproduce worker api logging problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath("commons-io:commons-io:2.5") | |
classpath("commons-codec:commons-codec:1.9") | |
} | |
} | |
tasks.register('md5', CreateMD5) { | |
destinationDirectory = project.layout.buildDirectory.dir('md5') | |
source(project.layout.projectDirectory.file('src')) | |
logging.captureStandardOutput(LogLevel.INFO) | |
doFirst { | |
println "should not see this!!!" | |
} | |
} | |
import org.gradle.api.Action; | |
import org.gradle.api.file.RegularFile; | |
import org.gradle.api.provider.Provider; | |
import org.gradle.api.tasks.*; | |
import org.gradle.workers.*; | |
import org.gradle.api.file.DirectoryProperty; | |
import javax.inject.Inject; | |
import java.io.File; | |
abstract public class CreateMD5 extends SourceTask { | |
@OutputDirectory | |
abstract public DirectoryProperty getDestinationDirectory(); | |
@Inject | |
abstract public WorkerExecutor getWorkerExecutor(); | |
@TaskAction | |
public void createHashes() { | |
WorkQueue workQueue = getWorkerExecutor().processIsolation(); | |
for (File sourceFile : getSource().getFiles()) { | |
Provider<RegularFile> md5File = getDestinationDirectory().file(sourceFile.getName() + ".md5"); | |
workQueue.submit(GenerateMD5.class, parameters -> { | |
parameters.getSourceFile().set(sourceFile); | |
parameters.getMD5File().set(md5File); | |
}); | |
} | |
} | |
} | |
import org.apache.commons.codec.digest.DigestUtils; | |
import org.apache.commons.io.FileUtils; | |
import org.gradle.workers.WorkAction; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
public abstract class GenerateMD5 implements WorkAction<MD5WorkParameters> { | |
@Override | |
public void execute() { | |
try { | |
File sourceFile = getParameters().getSourceFile().getAsFile().get(); | |
File md5File = getParameters().getMD5File().getAsFile().get(); | |
InputStream stream = new FileInputStream(sourceFile); | |
System.out.println("Generating MD5 for " + sourceFile.getName() + "..."); | |
// Artificially make this task slower. | |
Thread.sleep(3000); | |
FileUtils.writeStringToFile(md5File, DigestUtils.md5Hex(stream), (String) null); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
import org.gradle.api.file.RegularFileProperty; | |
import org.gradle.workers.WorkParameters; | |
public interface MD5WorkParameters extends WorkParameters { | |
RegularFileProperty getSourceFile(); | |
RegularFileProperty getMD5File(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment