fix schedulers not running, shuffle things around, again!

master
Josha von Gizycki 3 weeks ago
parent e7327b8c18
commit 19c6f25928

@ -11,7 +11,7 @@
<groupId>alfred</groupId> <groupId>alfred</groupId>
<artifactId>web</artifactId> <artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.2-SNAPSHOT</version>
<name>web</name> <name>web</name>
<description>Alfred CI</description> <description>Alfred CI</description>

@ -2,8 +2,10 @@ package alfred.web
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
@SpringBootApplication @SpringBootApplication
@EnableScheduling
open class WebApplication open class WebApplication
fun main(args: Array<String>) { fun main(args: Array<String>) {

@ -1,9 +1,22 @@
package alfred.web.core package alfred.web.core
import alfred.web.core.build.BuildId 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.beans.factory.annotation.Value
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.nio.file.Files
import java.nio.file.Files.createFile
import java.nio.file.Paths 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 @Service
class AlfredHome( class AlfredHome(
@ -13,10 +26,33 @@ class AlfredHome(
val homePath = Paths.get(home) val homePath = Paths.get(home)
fun logsDir() = val logsDir = homePath.resolve("logs")
homePath.resolve("logs")
fun buildConfig(buildId: BuildId) = fun buildConfig(buildId: BuildId) =
homePath.resolve("builds/${buildId}.properties") 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)
} }

@ -1,9 +1,12 @@
package alfred.web.core package alfred.web.core
import alfred.web.core.build.BuildId import alfred.web.core.build.BuildId
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Instant import java.time.Instant
import java.util.concurrent.TimeUnit
@Service @Service
class Handles { class Handles {
@ -25,11 +28,18 @@ class Handles {
) )
} }
@Scheduled(fixedDelay = 60_000) @Scheduled(fixedRate = 1, initialDelay = 1, timeUnit = TimeUnit.MINUTES)
fun cleanup() { fun cleanup() {
handles.removeIf { !it.handle.isAlive } handles
.filter { !it.handle.isAlive }
.forEach {
logger.info("handle for command '${it.command}' is not alive and too old, removing")
}
handles
.removeIf { !it.handle.isAlive }
} }
val logger: Logger = LoggerFactory.getLogger(this::class.java)
} }
data class Handle( data class Handle(

@ -7,9 +7,6 @@ import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.ResponseStatus
import java.nio.file.Files.createFile
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.* import java.util.*
@Service @Service
@ -41,17 +38,6 @@ class Builds(
return props.toBuildConfig(env) return props.toBuildConfig(env)
} }
fun createLogFile(build: BuildId): LogFile {
val nowStr = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)
val fileName = "$build.$nowStr.log"
val logsDir = alfredHome.logsDir()
logsDir.toFile().mkdir()
val logFilePath = logsDir.resolve(fileName)
return LogFile(createFile(logFilePath).toFile())
}
} }
private fun Properties.toBuildConfig(env: Map<String, String>) = private fun Properties.toBuildConfig(env: Map<String, String>) =

@ -11,7 +11,7 @@ import kotlin.text.lines
import kotlin.text.trim import kotlin.text.trim
class LogFile( class LogFile(
val backingFile: File val backingFile: File,
) { ) {
val logger: Logger = LoggerFactory.getLogger(this::class.java) val logger: Logger = LoggerFactory.getLogger(this::class.java)

@ -1,5 +1,6 @@
package alfred.web.core.runner package alfred.web.core.runner
import alfred.web.core.AlfredHome
import alfred.web.core.build.BuildContext import alfred.web.core.build.BuildContext
import alfred.web.core.build.BuildId import alfred.web.core.build.BuildId
import alfred.web.core.build.Builds import alfred.web.core.build.Builds
@ -23,7 +24,8 @@ class GitRunner(
val workspaces: Workspaces, val workspaces: Workspaces,
val eventPublisher: ApplicationEventPublisher, val eventPublisher: ApplicationEventPublisher,
val git: Git, val git: Git,
val script: Script val script: Script,
val alfredHome: AlfredHome
) { ) {
val scriptsDir = ".alfred" val scriptsDir = ".alfred"
@ -32,7 +34,7 @@ class GitRunner(
fun run(buildId: BuildId, rev: String): ProcessInfo { fun run(buildId: BuildId, rev: String): ProcessInfo {
val config = builds.buildConfig(buildId) val config = builds.buildConfig(buildId)
val logFile = builds.createLogFile(buildId) val logFile = alfredHome.newLogFile(buildId)
logger.info("preparing process for build $buildId") logger.info("preparing process for build $buildId")
logger.info("log file: ${logFile.backingFile.absolutePath}") logger.info("log file: ${logFile.backingFile.absolutePath}")

@ -1,5 +1,6 @@
package alfred.web.core.runner package alfred.web.core.runner
import alfred.web.core.AlfredHome
import alfred.web.core.build.* import alfred.web.core.build.*
import alfred.web.core.event.BuildFinished import alfred.web.core.event.BuildFinished
import alfred.web.core.process.Script import alfred.web.core.process.Script
@ -14,12 +15,13 @@ class ScriptRunner(
val builds: Builds, val builds: Builds,
val workspaces: Workspaces, val workspaces: Workspaces,
val eventPublisher: ApplicationEventPublisher, val eventPublisher: ApplicationEventPublisher,
val script: Script val script: Script,
val alfredHome: AlfredHome
) { ) {
fun run(buildId: BuildId, rev: String?): ProcessInfo { fun run(buildId: BuildId, rev: String?): ProcessInfo {
val config = builds.buildConfig(buildId) val config = builds.buildConfig(buildId)
val logFile = builds.createLogFile(buildId) val logFile = alfredHome.newLogFile(buildId)
logger.info("preparing process for build $buildId") logger.info("preparing process for build $buildId")
logger.info("log file: ${logFile.backingFile.absolutePath}") logger.info("log file: ${logFile.backingFile.absolutePath}")

@ -14,3 +14,5 @@ spring.mail.password=${ALFRED_MAIL_PASSWORD}
spring.mail.properties.mail.smtp.auth=${ALFRED_MAIL_SMTP_AUTH : true} spring.mail.properties.mail.smtp.auth=${ALFRED_MAIL_SMTP_AUTH : true}
spring.mail.properties.mail.smtp.starttls.enable=${ALFRED_MAIL_STARTTLS : true} spring.mail.properties.mail.smtp.starttls.enable=${ALFRED_MAIL_STARTTLS : true}
ALFRED_MAIL_FROM=alfred@your.service ALFRED_MAIL_FROM=alfred@your.service
management.endpoints.web.exposure.include=scheduledtasks,health

@ -17,7 +17,6 @@ import io.mockk.verify
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths
class GitRunnerTest { class GitRunnerTest {
@ -36,12 +35,10 @@ class GitRunnerTest {
val workspacesSpy = spyk(Workspaces()) val workspacesSpy = spyk(Workspaces())
val home = mockk<AlfredHome>() val home = AlfredHome(homeDir.toString())
every { home.buildConfig(any()) } returns homeDir.resolve("simple-git.properties") homeDir.resolve("builds").toFile().mkdir()
every { home.logsDir() } returns homeDir.resolve("logs")
every { home.homePath } returns homeDir
homeDir homeDir
.resolve("simple-git.properties") .resolve("builds/simple-git.properties")
.toFile() .toFile()
.writeText(buildProperties()) .writeText(buildProperties())
@ -51,7 +48,8 @@ class GitRunnerTest {
workspaces = workspacesSpy, workspaces = workspacesSpy,
eventPublisher = eventPublisher, eventPublisher = eventPublisher,
git = git, git = git,
script = script script = script,
alfredHome = home
) )
// When // When

@ -17,12 +17,10 @@ class ScriptRunnerTest {
@Test @Test
fun runs(@TempDir homeDir: Path) { fun runs(@TempDir homeDir: Path) {
// Given // Given
val home = mockk<AlfredHome>() val home = AlfredHome(homeDir.toString())
every { home.buildConfig(any()) } returns homeDir.resolve("simple-script.properties") homeDir.resolve("builds").toFile().mkdir()
every { home.logsDir() } returns homeDir.resolve("logs")
every { home.homePath } returns homeDir
homeDir homeDir
.resolve("simple-script.properties") .resolve("builds/simple-script.properties")
.toFile() .toFile()
.writeText(buildProperties()) .writeText(buildProperties())
@ -39,7 +37,8 @@ class ScriptRunnerTest {
builds = Builds(home), builds = Builds(home),
workspaces = workspaces, workspaces = workspaces,
eventPublisher = eventPublisher, eventPublisher = eventPublisher,
script = script script = script,
alfredHome = home
) )
// When // When

Loading…
Cancel
Save