Skip to content

Commit

Permalink
Merge pull request tuna#35 from tuna/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bigeagle authored Jun 15, 2016
2 parents 8cb4647 + 03fdaee commit 24bdfe5
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 5 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
*.swp
*~
/*.cov
node_modules
/build
2 changes: 2 additions & 0 deletions .testandcover.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function die() {

export GOPATH=`pwd`:$GOPATH

make

# Initialize profile.cov
echo "mode: count" > profile.cov

Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go:

before_install:
- sudo apt-get install cgroup-bin
- go get github.com/smartystreets/goconvey
- go get golang.org/x/tools/cmd/cover
- go get -v github.com/mattn/goveralls

Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LDFLAGS="-X main.buildstamp=`date -u '+%s'` -X main.githash=`git rev-parse HEAD`"

all: get tunasync tunasynctl

get:
go get ./cmd/tunasync
go get ./cmd/tunasynctl

build:
mkdir -p build

tunasync: build
go build -o build/tunasync -ldflags ${LDFLAGS} github.com/tuna/tunasync/cmd/tunasync

tunasynctl: build
go build -o build/tunasynctl -ldflags ${LDFLAGS} github.com/tuna/tunasync/cmd/tunasynctl
29 changes: 29 additions & 0 deletions cmd/tunasync/tunasync.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"

Expand All @@ -16,6 +18,11 @@ import (
"github.com/tuna/tunasync/worker"
)

var (
buildstamp = ""
githash = "No githash provided"
)

var logger = logging.MustGetLogger("tunasync")

func startManager(c *cli.Context) {
Expand Down Expand Up @@ -99,6 +106,28 @@ func startWorker(c *cli.Context) {
}

func main() {

cli.VersionPrinter = func(c *cli.Context) {
var builddate string
if buildstamp == "" {
builddate = "No build date provided"
} else {
ts, err := strconv.Atoi(buildstamp)
if err != nil {
builddate = "No build date provided"
} else {
t := time.Unix(int64(ts), 0)
builddate = t.String()
}
}
fmt.Printf(
"Version: %s\n"+
"Git Hash: %s\n"+
"Build Date: %s\n",
c.App.Version, githash, builddate,
)
}

app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = "0.1"
Expand Down
67 changes: 67 additions & 0 deletions cmd/tunasynctl/tunasynctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
Expand All @@ -15,6 +17,11 @@ import (
tunasync "github.com/tuna/tunasync/internal"
)

var (
buildstamp = ""
githash = "No githash provided"
)

const (
listJobsPath = "/jobs"
listWorkersPath = "/workers"
Expand Down Expand Up @@ -225,7 +232,61 @@ 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() {
cli.VersionPrinter = func(c *cli.Context) {
var builddate string
if buildstamp == "" {
builddate = "No build date provided"
} else {
ts, err := strconv.Atoi(buildstamp)
if err != nil {
builddate = "No build date provided"
} else {
t := time.Unix(int64(ts), 0)
builddate = t.String()
}
}
fmt.Printf(
"Version: %s\n"+
"Git Hash: %s\n"+
"Build Date: %s\n",
c.App.Version, githash, builddate,
)
}

app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = "0.1"
Expand Down Expand Up @@ -304,6 +365,12 @@ func main() {
Flags: append(commonFlags, cmdFlags...),
Action: initializeWrapper(cmdJob(tunasync.CmdRestart)),
},
{
Name: "reload",
Usage: "Tell worker to reload configurations",
Flags: append(commonFlags, cmdFlags...),
Action: initializeWrapper(cmdWorker(tunasync.CmdReload)),
},
{
Name: "ping",
Flags: append(commonFlags, cmdFlags...),
Expand Down
3 changes: 3 additions & 0 deletions internal/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
CmdDisable // disable the job (stops goroutine)
CmdRestart // restart syncing
CmdPing // ensure the goroutine is alive
CmdReload // reload mirror config
)

func (c CmdVerb) String() string {
Expand All @@ -49,6 +50,8 @@ func (c CmdVerb) String() string {
return "restart"
case CmdPing:
return "ping"
case CmdReload:
return "reload"
}
return "unknown"
}
Expand Down
20 changes: 19 additions & 1 deletion worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package worker
import (
"fmt"
"net/http"
"os"
"sync"
"syscall"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -185,16 +187,32 @@ func (w *Worker) makeHTTPServer() {
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]
if !ok {
c.JSON(http.StatusNotFound, gin.H{"msg": fmt.Sprintf("Mirror ``%s'' not found", cmd.MirrorID)})
return
}

logger.Noticef("Received command: %v", cmd)
// No matter what command, the existing job
// schedule should be flushed
w.schedule.Remove(job.Name())

// if job disabled, start them first
switch cmd.Cmd {
case CmdStart, CmdRestart:
Expand Down

0 comments on commit 24bdfe5

Please sign in to comment.