镜像自地址
https://github.com/tuna/tunasync.git
已同步 2025-12-07 06:56:47 +00:00
feat(tunasync): add tunasynctl reload subcommand
这个提交包含在:
@@ -225,6 +225,39 @@ func cmdJob(cmd tunasync.CmdVerb) cli.ActionFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cmdWorker(cmd tunasync.CmdVerb) cli.ActionFunc {
|
||||||
|
return func(c *cli.Context) error {
|
||||||
|
cmd := tunasync.ClientCmd{
|
||||||
|
Cmd: cmd,
|
||||||
|
WorkerID: c.String("worker"),
|
||||||
|
}
|
||||||
|
resp, err := tunasync.PostJSON(baseURL+cmdPath, cmd, client)
|
||||||
|
if err != nil {
|
||||||
|
return cli.NewExitError(
|
||||||
|
fmt.Sprintf("Failed to correctly send command: %s",
|
||||||
|
err.Error()),
|
||||||
|
1)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return cli.NewExitError(
|
||||||
|
fmt.Sprintf("Failed to parse response: %s", err.Error()),
|
||||||
|
1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cli.NewExitError(fmt.Sprintf("Failed to correctly send"+
|
||||||
|
" command: HTTP status code is not 200: %s", body),
|
||||||
|
1)
|
||||||
|
}
|
||||||
|
logger.Info("Succesfully send command")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.EnableBashCompletion = true
|
app.EnableBashCompletion = true
|
||||||
@@ -304,6 +337,12 @@ func main() {
|
|||||||
Flags: append(commonFlags, cmdFlags...),
|
Flags: append(commonFlags, cmdFlags...),
|
||||||
Action: initializeWrapper(cmdJob(tunasync.CmdRestart)),
|
Action: initializeWrapper(cmdJob(tunasync.CmdRestart)),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "reload",
|
||||||
|
Usage: "Tell worker to reload configurations",
|
||||||
|
Flags: append(commonFlags, cmdFlags...),
|
||||||
|
Action: initializeWrapper(cmdWorker(tunasync.CmdReload)),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "ping",
|
Name: "ping",
|
||||||
Flags: append(commonFlags, cmdFlags...),
|
Flags: append(commonFlags, cmdFlags...),
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ const (
|
|||||||
CmdDisable // disable the job (stops goroutine)
|
CmdDisable // disable the job (stops goroutine)
|
||||||
CmdRestart // restart syncing
|
CmdRestart // restart syncing
|
||||||
CmdPing // ensure the goroutine is alive
|
CmdPing // ensure the goroutine is alive
|
||||||
|
CmdReload // reload mirror config
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c CmdVerb) String() string {
|
func (c CmdVerb) String() string {
|
||||||
@@ -49,6 +50,8 @@ func (c CmdVerb) String() string {
|
|||||||
return "restart"
|
return "restart"
|
||||||
case CmdPing:
|
case CmdPing:
|
||||||
return "ping"
|
return "ping"
|
||||||
|
case CmdReload:
|
||||||
|
return "reload"
|
||||||
}
|
}
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package worker
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -185,16 +187,32 @@ func (w *Worker) makeHTTPServer() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Noticef("Received command: %v", cmd)
|
||||||
|
|
||||||
|
if cmd.MirrorID == "" {
|
||||||
|
// worker-level commands
|
||||||
|
switch cmd.Cmd {
|
||||||
|
case CmdReload:
|
||||||
|
// send myself a SIGHUP
|
||||||
|
pid := os.Getpid()
|
||||||
|
syscall.Kill(pid, syscall.SIGHUP)
|
||||||
|
default:
|
||||||
|
c.JSON(http.StatusNotAcceptable, gin.H{"msg": "Invalid Command"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// job level comands
|
||||||
job, ok := w.jobs[cmd.MirrorID]
|
job, ok := w.jobs[cmd.MirrorID]
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"msg": fmt.Sprintf("Mirror ``%s'' not found", cmd.MirrorID)})
|
c.JSON(http.StatusNotFound, gin.H{"msg": fmt.Sprintf("Mirror ``%s'' not found", cmd.MirrorID)})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Noticef("Received command: %v", cmd)
|
|
||||||
// No matter what command, the existing job
|
// No matter what command, the existing job
|
||||||
// schedule should be flushed
|
// schedule should be flushed
|
||||||
w.schedule.Remove(job.Name())
|
w.schedule.Remove(job.Name())
|
||||||
|
|
||||||
// if job disabled, start them first
|
// if job disabled, start them first
|
||||||
switch cmd.Cmd {
|
switch cmd.Cmd {
|
||||||
case CmdStart, CmdRestart:
|
case CmdStart, CmdRestart:
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户