Skip to content

Commit a185dc3

Browse files
Merge pull request #8 from Traverse-Research/jasper-bekkers/atomic-job-claim
Jasper bekkers/atomic job claim
2 parents e3b1e68 + f57afa7 commit a185dc3

File tree

3 files changed

+410
-46
lines changed

3 files changed

+410
-46
lines changed

src/api.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::jobs::{Job, JobProgress, JobQueue, JobStatus, Quadrant, VideoQuadrantSelection, WebDavConfig};
1+
use crate::jobs::{Job, JobProgress, JobQueue, JobStatus, LogEntry, Quadrant, VideoQuadrantSelection, WebDavConfig};
22
use crate::webdav::WebDavClient;
33
use anyhow::Result;
44
use axum::{
@@ -90,6 +90,7 @@ pub async fn run_server(port: u16, data_dir: &str) -> Result<()> {
9090
.route("/api/jobs/pending", get(get_pending_job))
9191
.route("/api/jobs/claim", post(claim_job))
9292
.route("/api/jobs/{id}/progress", patch(update_job_progress))
93+
.route("/api/jobs/{id}/logs", post(append_job_logs))
9394
.route("/health", get(health_check))
9495
// Static files for worker provisioning
9596
.route("/assets/worker", get(serve_worker_binary))
@@ -456,6 +457,49 @@ async fn update_job_progress(
456457
}
457458
}
458459

460+
#[derive(Debug, Deserialize)]
461+
struct AppendLogsRequest {
462+
logs: Vec<LogEntryRequest>,
463+
}
464+
465+
#[derive(Debug, Deserialize)]
466+
struct LogEntryRequest {
467+
timestamp: String,
468+
level: String,
469+
message: String,
470+
}
471+
472+
/// Append log entries to a job
473+
async fn append_job_logs(
474+
State(state): State<AppState>,
475+
Path(id): Path<String>,
476+
Json(req): Json<AppendLogsRequest>,
477+
) -> Response {
478+
let logs: Vec<LogEntry> = req.logs.into_iter().map(|l| {
479+
LogEntry {
480+
timestamp: chrono::DateTime::parse_from_rfc3339(&l.timestamp)
481+
.map(|dt| dt.with_timezone(&chrono::Utc))
482+
.unwrap_or_else(|_| chrono::Utc::now()),
483+
level: l.level,
484+
message: l.message,
485+
}
486+
}).collect();
487+
488+
let queue = state.queue.lock().unwrap();
489+
match queue.append_job_logs(&id, logs) {
490+
Ok(_) => {
491+
StatusCode::OK.into_response()
492+
}
493+
Err(e) => {
494+
error!("Failed to append job logs: {}", e);
495+
AppError {
496+
status: StatusCode::NOT_FOUND,
497+
message: format!("Job not found: {}", e),
498+
}.into_response()
499+
}
500+
}
501+
}
502+
459503
async fn serve_worker_binary() -> Response {
460504
// Serve the Linux worker binary from ./assets/worker-linux
461505
let path = "./assets/worker-linux";

0 commit comments

Comments
 (0)