mirror of
https://github.com/tuna/tunasync.git
synced 2025-06-15 05:52:43 +00:00
commit
0db8fc6614
@ -2,18 +2,20 @@ package worker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/codeskyblue/go-sh"
|
"github.com/codeskyblue/go-sh"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cgSubsystem string = "cpu"
|
var cgSubsystem = "cpu"
|
||||||
|
|
||||||
type cgroupHook struct {
|
type cgroupHook struct {
|
||||||
emptyHook
|
emptyHook
|
||||||
@ -82,23 +84,43 @@ func (c *cgroupHook) killAll() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
name := c.provider.Name()
|
name := c.provider.Name()
|
||||||
taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks"))
|
|
||||||
if err != nil {
|
readTaskList := func() ([]int, error) {
|
||||||
return err
|
taskList := []int{}
|
||||||
|
taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks"))
|
||||||
|
if err != nil {
|
||||||
|
return taskList, err
|
||||||
|
}
|
||||||
|
defer taskFile.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(taskFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
pid, err := strconv.Atoi(scanner.Text())
|
||||||
|
if err != nil {
|
||||||
|
return taskList, err
|
||||||
|
}
|
||||||
|
taskList = append(taskList, pid)
|
||||||
|
}
|
||||||
|
return taskList, nil
|
||||||
}
|
}
|
||||||
defer taskFile.Close()
|
|
||||||
taskList := []int{}
|
for i := 0; i < 4; i++ {
|
||||||
scanner := bufio.NewScanner(taskFile)
|
if i == 3 {
|
||||||
for scanner.Scan() {
|
return errors.New("Unable to kill all child tasks")
|
||||||
pid, err := strconv.Atoi(scanner.Text())
|
}
|
||||||
|
taskList, err := readTaskList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
taskList = append(taskList, pid)
|
if len(taskList) == 0 {
|
||||||
}
|
return nil
|
||||||
for _, pid := range taskList {
|
}
|
||||||
logger.Debugf("Killing process: %d", pid)
|
for _, pid := range taskList {
|
||||||
unix.Kill(pid, syscall.SIGKILL)
|
logger.Debugf("Killing process: %d", pid)
|
||||||
|
unix.Kill(pid, syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
// sleep 10ms for the first round, and 1.01s, 2.01s, 3.01s for the rest
|
||||||
|
time.Sleep(time.Duration(i)*time.Second + 10*time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -79,7 +79,7 @@ func (l *logLimiter) preExec() error {
|
|||||||
|
|
||||||
logLink := filepath.Join(logDir, "latest")
|
logLink := filepath.Join(logDir, "latest")
|
||||||
|
|
||||||
if _, err = os.Stat(logLink); err == nil {
|
if _, err = os.Lstat(logLink); err == nil {
|
||||||
os.Remove(logLink)
|
os.Remove(logLink)
|
||||||
}
|
}
|
||||||
os.Symlink(logFileName, logLink)
|
os.Symlink(logFileName, logLink)
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -17,12 +18,14 @@ import (
|
|||||||
var errProcessNotStarted = errors.New("Process Not Started")
|
var errProcessNotStarted = errors.New("Process Not Started")
|
||||||
|
|
||||||
type cmdJob struct {
|
type cmdJob struct {
|
||||||
|
sync.Mutex
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
workingDir string
|
workingDir string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
finished chan empty
|
finished chan empty
|
||||||
provider mirrorProvider
|
provider mirrorProvider
|
||||||
|
retErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCmdJob(provider mirrorProvider, cmdAndArgs []string, workingDir string, env map[string]string) *cmdJob {
|
func newCmdJob(provider mirrorProvider, cmdAndArgs []string, workingDir string, env map[string]string) *cmdJob {
|
||||||
@ -69,9 +72,18 @@ func (c *cmdJob) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmdJob) Wait() error {
|
func (c *cmdJob) Wait() error {
|
||||||
err := c.cmd.Wait()
|
c.Lock()
|
||||||
close(c.finished)
|
defer c.Unlock()
|
||||||
return err
|
|
||||||
|
select {
|
||||||
|
case <-c.finished:
|
||||||
|
return c.retErr
|
||||||
|
default:
|
||||||
|
err := c.cmd.Wait()
|
||||||
|
c.retErr = err
|
||||||
|
close(c.finished)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmdJob) SetLogFile(logFile *os.File) {
|
func (c *cmdJob) SetLogFile(logFile *os.File) {
|
||||||
|
@ -108,6 +108,7 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *twoStageRsyncProvider) Run() error {
|
func (p *twoStageRsyncProvider) Run() error {
|
||||||
|
defer p.Wait()
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
if p.username != "" {
|
if p.username != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user