From 90a2140b8cbfce921edc0d7a94875e26abd41b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 21 Feb 2022 19:47:07 +0100 Subject: [PATCH] Manager: store task logs to disk --- .gitignore | 1 + internal/manager/api_impl/api_impl.go | 1 + internal/manager/api_impl/jobs.go | 18 +++++++++++++++--- internal/manager/persistence/jobs.go | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ff7e1e2c..dcf99d6e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /flamenco-worker.yaml /flamenco-worker-credentials.yaml node_modules/ +task-logs/ diff --git a/internal/manager/api_impl/api_impl.go b/internal/manager/api_impl/api_impl.go index 4db62bbe..a8969491 100644 --- a/internal/manager/api_impl/api_impl.go +++ b/internal/manager/api_impl/api_impl.go @@ -45,6 +45,7 @@ type Flamenco struct { type PersistenceService interface { StoreAuthoredJob(ctx context.Context, authoredJob job_compilers.AuthoredJob) error FetchJob(ctx context.Context, jobID string) (*persistence.Job, error) + // FetchTask fetches the given task and the accompanying job. FetchTask(ctx context.Context, taskID string) (*persistence.Task, error) SaveTask(ctx context.Context, task *persistence.Task) error diff --git a/internal/manager/api_impl/jobs.go b/internal/manager/api_impl/jobs.go index fa671b6c..7e2640c9 100644 --- a/internal/manager/api_impl/jobs.go +++ b/internal/manager/api_impl/jobs.go @@ -180,6 +180,10 @@ func (f *Flamenco) doTaskUpdate( dbTask *persistence.Task, update api.TaskUpdateJSONRequestBody, ) error { + if dbTask.Job == nil { + logger.Panic().Msg("dbTask.Job is nil, unable to continue") + } + if update.TaskStatus != nil { // TODO: check that this status transition is valid. // TODO: process this status transition. @@ -195,10 +199,18 @@ func (f *Flamenco) doTaskUpdate( dbTask.Activity = *update.Activity } + // Do the database persistence first, as that's more important than the logging. + dbErr := f.persist.SaveTask(ctx, dbTask) + if update.Log != nil { - // TODO: write log to disk. - logger.Warn().Msg("task logs are not yet handled") + // Errors writing the log to file should be logged in our own logging + // system, but shouldn't abort the render. As such, `err` is not returned to + // the caller. + err := f.logStorage.Write(logger, dbTask.Job.UUID, dbTask.UUID, *update.Log) + if err != nil { + logger.Error().Err(err).Msg("error writing task log") + } } - return f.persist.SaveTask(ctx, dbTask) + return dbErr } diff --git a/internal/manager/persistence/jobs.go b/internal/manager/persistence/jobs.go index 5bb5b2a3..989e40e9 100644 --- a/internal/manager/persistence/jobs.go +++ b/internal/manager/persistence/jobs.go @@ -205,7 +205,7 @@ func (db *DB) SaveJobStatus(ctx context.Context, j *Job) error { func (db *DB) FetchTask(ctx context.Context, taskUUID string) (*Task, error) { dbTask := Task{} - findResult := db.gormDB.First(&dbTask, "uuid = ?", taskUUID) + findResult := db.gormDB.Joins("Job").First(&dbTask, "tasks.uuid = ?", taskUUID) if findResult.Error != nil { return nil, findResult.Error }