-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid Java Temp Files in task creation (#6178)
* Avoid Java Temp Files in task creation * [wip] temp file service * add clean up logic, refactor * pr feedback, replace slash in temp files
- Loading branch information
Showing
9 changed files
with
106 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package oxalis.files | ||
|
||
import java.nio.file.{Files, Path, Paths} | ||
|
||
import com.scalableminds.util.tools.Fox | ||
import com.typesafe.scalalogging.LazyLogging | ||
import javax.inject.Inject | ||
import net.liftweb.common.Box.tryo | ||
import org.apache.commons.io.FileUtils | ||
import oxalis.cleanup.CleanUpService | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
import scala.util.Random | ||
|
||
/** | ||
* Avoiding Java TemporaryFiles because of seeming openJDK regression, | ||
* see discussion at https://github.com/scalableminds/webknossos/issues/6173 | ||
*/ | ||
class TempFileService @Inject()(cleanUpService: CleanUpService)(implicit ec: ExecutionContext) extends LazyLogging { | ||
|
||
private val tmpDir: Path = Paths.get(System.getProperty("java.io.tmpdir")).resolve("webKnossosTempFiles") | ||
|
||
private val activeTempFiles = scala.collection.mutable.Set[(Path, Long)]() | ||
|
||
cleanUpService.register("Clean up expired temporary files", 1 hour)(cleanUpExpiredFiles()) | ||
|
||
private def ensureParent(): Path = | ||
Files.createDirectories(tmpDir) | ||
|
||
def create(prefix: String = "tmpFile", lifeTime: FiniteDuration = 2 hours): Path = { | ||
ensureParent() | ||
val path = tmpDir.resolve(f"$prefix-${Random.alphanumeric.take(15).mkString("")}") | ||
logger.info(f"Creating temp file at $path") | ||
Files.createFile(path) | ||
activeTempFiles.add((path, System.currentTimeMillis() + lifeTime.toMillis)) | ||
path | ||
} | ||
|
||
def cleanUpExpiredFiles(): Fox[Unit] = { | ||
val now = System.currentTimeMillis() | ||
activeTempFiles.foreach { | ||
case (path, expiryTime) => | ||
if (expiryTime < now) { | ||
tryo(Files.delete(path)) | ||
activeTempFiles.remove((path, expiryTime)) | ||
} | ||
} | ||
Fox.successful(()) | ||
} | ||
|
||
def cleanUpAll(): Unit = | ||
FileUtils.deleteDirectory(tmpDir.toFile) | ||
|
||
} |
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