diff --git a/migrations/000015_alter_tasks_table_log_offset.down.sql b/migrations/000015_alter_tasks_table_log_offset.down.sql new file mode 100644 index 0000000..bfbd448 --- /dev/null +++ b/migrations/000015_alter_tasks_table_log_offset.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE tasks +DROP IF EXISTS log_offset; diff --git a/migrations/000015_alter_tasks_table_log_offset.up.sql b/migrations/000015_alter_tasks_table_log_offset.up.sql new file mode 100644 index 0000000..7543ea9 --- /dev/null +++ b/migrations/000015_alter_tasks_table_log_offset.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE tasks +ADD log_offset integer NOT NULL DEFAULT COALESCE(CARDINALITY(log),0); diff --git a/server/worker.go b/server/worker.go index b501165..ed8322e 100644 --- a/server/worker.go +++ b/server/worker.go @@ -182,7 +182,7 @@ func updateWorkerTaskStatus(ctx context.Context) { if Workers[uuid].Connected { w := Workers[uuid] - rows, err := db.Query(ctx, "SELECT id, COALESCE(CARDINALITY(log),0) as log_offset FROM tasks WHERE worker_id = $1 AND (status = $2 OR status = $3 OR status = $4 OR status = $5)", uuid, constants.TASK_STATUS_UNKNOWN, constants.TASK_STATUS_ASSIGNED, constants.TASK_STATUS_RUNNING, constants.TASK_STATUS_WAITING) + rows, err := db.Query(ctx, "SELECT id, log_offset FROM tasks WHERE worker_id = $1 AND (status = $2 OR status = $3 OR status = $4 OR status = $5)", uuid, constants.TASK_STATUS_UNKNOWN, constants.TASK_STATUS_ASSIGNED, constants.TASK_STATUS_RUNNING, constants.TASK_STATUS_WAITING) if err != nil { slog.ErrorContext(ctx, "Error Getting Tasks for Worker", "err", err, "worker_id", uuid) return @@ -236,7 +236,7 @@ func updateWorkerTaskStatus(ctx context.Context) { } } - _, err = db.Exec(ctx, "UPDATE tasks SET status = $2, log = log || $3 WHERE id = $1", taskID, ts.Task.Status, ts.Task.Log) + _, err = db.Exec(ctx, "UPDATE tasks SET status = $2, log = log || $3, log_offset = log_offset + $4 WHERE id = $1", taskID, ts.Task.Status, ts.Task.Log, len(ts.Task.Log)) if err != nil { slog.ErrorContext(ctx, "Updating Task Status", "err", err) return