184 lines
5.3 KiB
Go
184 lines
5.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
)
|
|
|
|
func runPostPublishCleanup(ctx context.Context, env *Env, manifestPath string, m *manifest.Manifest, executed []string) error {
|
|
if env == nil || env.Config == nil || env.Config.Pipeline == nil || m == nil {
|
|
return nil
|
|
}
|
|
|
|
spoolRequested := env.Config.Pipeline.Spool.DeleteAudioAfterPublish
|
|
workRequested := env.Config.Pipeline.Workspace.CleanupAfterPublish
|
|
if !spoolRequested && !workRequested {
|
|
return nil
|
|
}
|
|
|
|
sr := publishStageRecordForCleanup(m, executed)
|
|
if sr == nil {
|
|
return nil
|
|
}
|
|
if sr.Metadata == nil {
|
|
sr.Metadata = map[string]any{}
|
|
}
|
|
sr.Metadata["spool_cleanup_requested"] = spoolRequested
|
|
sr.Metadata["workdir_cleanup_requested"] = workRequested
|
|
|
|
eligible, reason := publishCleanupEligible(env.Config, sr)
|
|
if !eligible {
|
|
sr.Metadata["cleanup_skipped"] = true
|
|
sr.Metadata["cleanup_skipped_reason"] = reason
|
|
if err := env.ManifestStore.Save(ctx, manifestPath, m); err != nil {
|
|
return fmt.Errorf("save manifest cleanup skip metadata %q: %w", manifestPath, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
spoolDir := strings.TrimSpace(m.LocalSpoolDir)
|
|
if spoolDir == "" {
|
|
spoolDir = artifacts.SessionSpoolAudioDir(
|
|
env.Config.Pipeline.Spool.Root,
|
|
strings.TrimSpace(env.Config.Session.Campaign),
|
|
strings.TrimSpace(env.Config.Session.SessionID),
|
|
strings.TrimSpace(m.RunID),
|
|
)
|
|
}
|
|
workDir := strings.TrimSpace(m.LocalWorkDir)
|
|
if workDir == "" {
|
|
workDir = artifacts.SessionRunRootForCampaign(
|
|
env.Config.Pipeline.Workspace.Root,
|
|
strings.TrimSpace(env.Config.Session.Campaign),
|
|
strings.TrimSpace(env.Config.Session.SessionID),
|
|
strings.TrimSpace(m.RunID),
|
|
)
|
|
}
|
|
|
|
if spoolRequested {
|
|
if err := removeRunScopedDir(strings.TrimSpace(env.Config.Pipeline.Spool.Root), spoolDir, "pipeline.spool.delete_audio_after_publish"); err != nil {
|
|
sr.Metadata["cleanup_failed"] = true
|
|
sr.Metadata["cleanup_failed_policy"] = "pipeline.spool.delete_audio_after_publish"
|
|
sr.Metadata["cleanup_failed_path"] = spoolDir
|
|
_ = env.ManifestStore.Save(ctx, manifestPath, m)
|
|
return err
|
|
}
|
|
sr.Metadata["spool_cleanup_deleted"] = filepath.Clean(spoolDir)
|
|
}
|
|
|
|
if !workRequested {
|
|
sr.Metadata["cleanup_completed"] = true
|
|
sr.Metadata["cleanup_skipped"] = false
|
|
if err := env.ManifestStore.Save(ctx, manifestPath, m); err != nil {
|
|
return fmt.Errorf("save manifest cleanup metadata %q: %w", manifestPath, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if err := removeRunScopedDir(strings.TrimSpace(env.Config.Pipeline.Workspace.Root), workDir, "pipeline.workspace.cleanup_after_publish"); err != nil {
|
|
sr.Metadata["cleanup_failed"] = true
|
|
sr.Metadata["cleanup_failed_policy"] = "pipeline.workspace.cleanup_after_publish"
|
|
sr.Metadata["cleanup_failed_path"] = workDir
|
|
_ = env.ManifestStore.Save(ctx, manifestPath, m)
|
|
return err
|
|
}
|
|
|
|
sr.Metadata["workdir_cleanup_deleted"] = filepath.Clean(workDir)
|
|
sr.Metadata["cleanup_completed"] = true
|
|
sr.Metadata["cleanup_skipped"] = false
|
|
return nil
|
|
}
|
|
|
|
func publishStageRecordForCleanup(m *manifest.Manifest, executed []string) *manifest.StageRecord {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
publishRan := false
|
|
for _, name := range executed {
|
|
if name == "publish" {
|
|
publishRan = true
|
|
break
|
|
}
|
|
}
|
|
if !publishRan {
|
|
return nil
|
|
}
|
|
sr := m.Stages["publish"]
|
|
if sr == nil || sr.Status != manifest.StatusSucceeded {
|
|
return nil
|
|
}
|
|
return sr
|
|
}
|
|
|
|
func publishCleanupEligible(cfg *config.Config, sr *manifest.StageRecord) (bool, string) {
|
|
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Publish == nil {
|
|
return false, "publish configuration is missing"
|
|
}
|
|
enabled := true
|
|
if cfg.Pipeline.Publish.Enabled != nil {
|
|
enabled = *cfg.Pipeline.Publish.Enabled
|
|
}
|
|
if !enabled {
|
|
return false, "publish.enabled is false"
|
|
}
|
|
uploadRun := true
|
|
if cfg.Pipeline.Publish.UploadRun != nil {
|
|
uploadRun = *cfg.Pipeline.Publish.UploadRun
|
|
}
|
|
if !uploadRun {
|
|
return false, "publish.upload_run is false"
|
|
}
|
|
if sr == nil || sr.Metadata == nil {
|
|
return false, "publish metadata is missing"
|
|
}
|
|
if skipped, _ := sr.Metadata["skipped"].(bool); skipped {
|
|
return false, "publish stage was skipped"
|
|
}
|
|
if uploaded, _ := sr.Metadata["uploaded"].(bool); !uploaded {
|
|
return false, "publish did not upload run record"
|
|
}
|
|
if pointer, _ := sr.Metadata["current_pointer_written"].(bool); !pointer {
|
|
return false, "publish did not write current pointer"
|
|
}
|
|
if strings.TrimSpace(asString(sr.Metadata["current_run_id_key"])) == "" {
|
|
return false, "publish current run pointer key is missing"
|
|
}
|
|
return true, ""
|
|
}
|
|
|
|
type scopedDir struct {
|
|
RootAbs string
|
|
TargetAbs string
|
|
Exists bool
|
|
}
|
|
|
|
func removeRunScopedDir(root, target, policy string) error {
|
|
dir, err := validateScopedDir(root, target, policy)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !dir.Exists {
|
|
return nil
|
|
}
|
|
if err := fileops.RemoveAllUnderRoot(dir.RootAbs, dir.TargetAbs); err != nil {
|
|
return fmt.Errorf("cleanup policy %s: remove %q: %w", policy, dir.TargetAbs, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateScopedDir(root, target, policy string) (scopedDir, error) {
|
|
return validateScopedTarget(root, target, policy, true)
|
|
}
|
|
|
|
func asString(v any) string {
|
|
s, _ := v.(string)
|
|
return s
|
|
}
|