Skip to content

Commit

Permalink
Update progress checker to work on file output size
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Cameron <[email protected]>
  • Loading branch information
JasonLovesDoggo committed Nov 4, 2024
1 parent e8b0535 commit fd530cc
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions handlers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -124,22 +123,22 @@ func (h *MySQLHandler) Backup(ctx context.Context) (string, error) {
// Create and configure mysqldump command
cmd := h.createMySQLDumpCommand(ctx, filename)

// Set up pipes for output handling
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", &ErrMySQLBackup{Op: "create stdout pipe", Err: err}
}
cmd.Stderr = os.Stderr

// Start the backup process
if err := cmd.Start(); err != nil {
return "", &ErrMySQLBackup{Op: "start mysqldump", Err: err}
}

// Write output to both file and progress bar
multiWriter := io.MultiWriter(file, bar)
if _, err := io.Copy(multiWriter, stdout); err != nil {
return "", &ErrMySQLBackup{Op: "copy backup data", Err: err}
// Update progress bar based on size of the output file
for checkRunning(cmd) {
fileInfo, err := os.Stat(filename)
if err != nil {
return "", err
}
err = bar.Set64(fileInfo.Size())
if err != nil {
return "", err
}
time.Sleep(time.Second)
}

// Wait for the process to complete
Expand All @@ -154,3 +153,11 @@ func (h *MySQLHandler) Backup(ctx context.Context) (string, error) {
func (h *MySQLHandler) Name() string {
return "MySQL Backup"
}

func checkRunning(cmd *exec.Cmd) bool {
if cmd == nil || cmd.ProcessState != nil && cmd.ProcessState.Exited() || cmd.Process == nil {
return false
}

return true
}

0 comments on commit fd530cc

Please sign in to comment.