|  |  |  | @ -1,9 +1,22 @@ | 
		
	
		
			
				|  |  |  |  | package alfred.web.core | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import alfred.web.core.build.BuildId | 
		
	
		
			
				|  |  |  |  | import alfred.web.core.build.LogFile | 
		
	
		
			
				|  |  |  |  | import org.slf4j.Logger | 
		
	
		
			
				|  |  |  |  | import org.slf4j.LoggerFactory | 
		
	
		
			
				|  |  |  |  | import org.springframework.beans.factory.annotation.Value | 
		
	
		
			
				|  |  |  |  | import org.springframework.scheduling.annotation.Scheduled | 
		
	
		
			
				|  |  |  |  | import org.springframework.stereotype.Service | 
		
	
		
			
				|  |  |  |  | import java.nio.file.Files | 
		
	
		
			
				|  |  |  |  | import java.nio.file.Files.createFile | 
		
	
		
			
				|  |  |  |  | import java.nio.file.Paths | 
		
	
		
			
				|  |  |  |  | import java.time.Instant | 
		
	
		
			
				|  |  |  |  | import java.time.LocalDateTime | 
		
	
		
			
				|  |  |  |  | import java.time.format.DateTimeFormatter | 
		
	
		
			
				|  |  |  |  | import java.time.temporal.ChronoUnit | 
		
	
		
			
				|  |  |  |  | import java.util.concurrent.TimeUnit | 
		
	
		
			
				|  |  |  |  | import kotlin.io.path.deleteIfExists | 
		
	
		
			
				|  |  |  |  | import kotlin.io.path.getLastModifiedTime | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | @Service | 
		
	
		
			
				|  |  |  |  | class AlfredHome( | 
		
	
	
		
			
				
					|  |  |  | @ -13,10 +26,33 @@ class AlfredHome( | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     val homePath = Paths.get(home) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     fun logsDir() = | 
		
	
		
			
				|  |  |  |  |         homePath.resolve("logs") | 
		
	
		
			
				|  |  |  |  |     val logsDir = homePath.resolve("logs") | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     fun buildConfig(buildId: BuildId) = | 
		
	
		
			
				|  |  |  |  |         homePath.resolve("builds/${buildId}.properties") | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     fun newLogFile(build: BuildId): LogFile { | 
		
	
		
			
				|  |  |  |  |         val nowStr = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) | 
		
	
		
			
				|  |  |  |  |         val fileName = "$build.$nowStr.log" | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         logsDir.toFile().mkdir() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         val logFilePath = logsDir.resolve(fileName) | 
		
	
		
			
				|  |  |  |  |         return LogFile(createFile(logFilePath).toFile()) | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     @Scheduled(fixedRate = 1, initialDelay = 1, timeUnit = TimeUnit.HOURS) | 
		
	
		
			
				|  |  |  |  |     fun cleanup() { | 
		
	
		
			
				|  |  |  |  |         val maxAge = Instant.now().minus(7L, ChronoUnit.DAYS) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         Files.list(logsDir) | 
		
	
		
			
				|  |  |  |  |             .filter { it.getLastModifiedTime().toInstant().isBefore(maxAge) } | 
		
	
		
			
				|  |  |  |  |             .forEach { | 
		
	
		
			
				|  |  |  |  |                 logger.info("log file $it gets old, deleting") | 
		
	
		
			
				|  |  |  |  |                 it.deleteIfExists() | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     val logger: Logger = LoggerFactory.getLogger(this::class.java) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |