feat: add start and trigger savepoint routes

This commit is contained in:
2025-01-17 20:27:32 +03:30
parent c977c8a15d
commit 5066dc650f
4 changed files with 155 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
package controller
import (
"archive/zip"
"bufio"
"context"
"errors"
@@ -12,7 +13,6 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/danielgtaylor/huma/v2"
@@ -62,6 +62,18 @@ func StopJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
}
func StartJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := managed_job.GetManager()
job := mgr.GetJob(k8sTypes.UID(req.JobUId))
err := job.Run(false)
if err != nil {
return nil, err
}
return &StopJobResp{Body: StopJobRespBody{
Success: true,
}}, nil
}
func ResumeJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := managed_job.GetManager()
job := mgr.GetJob(k8sTypes.UID(req.JobUId))
err := job.Run(true)
@@ -91,6 +103,30 @@ func PauseJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
}}, nil
}
type JobTriggerSavepointReq struct {
JobUId string `path:"uid"`
}
type JobTriggerSavepointRespBody struct {
Success bool `json:"success"`
}
type JobTriggerSavepointResp struct {
Body JobTriggerSavepointRespBody
}
func TriggerSavepoint(ctx context.Context, req *JobTriggerSavepointReq) (*JobTriggerSavepointResp, error) {
mgr := managed_job.GetManager()
job := mgr.GetJob(k8sTypes.UID(req.JobUId))
err := job.TriggerSavepoint()
if err != nil {
return nil, err
}
return &JobTriggerSavepointResp{Body: JobTriggerSavepointRespBody{
Success: true,
}}, nil
}
func DownloadSavepoint(ctx context.Context, req *types.SavepointDownloadReq) (*huma.StreamResponse, error) {
folderPath := req.SavepointPath // Change this to your folder path
@@ -102,15 +138,66 @@ func DownloadSavepoint(ctx context.Context, req *types.SavepointDownloadReq) (*h
zap.String("folderPath", folderPath),
)
// Run the zip command
cmd := exec.Command("zip", "-r", zipFilePath, folderPath)
// Capture any output or errors
output, err := cmd.CombinedOutput()
// Create the zip file
zipFile, err := os.Create(zipFilePath)
if err != nil {
return nil, fmt.Errorf("failed to run zip command: %v\nOutput: %s", err, string(output))
fmt.Println("Error creating zip file:", err)
return nil, nil
}
pkg.Logger.Debug("[controller] [savepoint]", zap.Any("zip command output", string(output)))
defer zipFile.Close()
// Create a new zip writer
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Walk through the source directory and add files to the zip
err = filepath.Walk(folderPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Create a new file header
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// Set the header name to the relative path
header.Name, err = filepath.Rel(folderPath, filePath)
if err != nil {
return err
}
// If it's a directory, add a trailing slash
if info.IsDir() {
header.Name += "/"
} else {
// Set the compression method
header.Method = zip.Deflate
}
// Create a new writer for the file
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
// If it's a directory, we're done
if info.IsDir() {
return nil
}
// Open the file to be zipped
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// Copy the file content to the zip writer
_, err = io.Copy(writer, file)
return err
})
// Open the zip file for reading
zipFileReader, err := os.Open(zipFilePath)