2018-02-08 19:33:52 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/crypto"
|
2018-02-08 19:33:52 +01:00
|
|
|
"github.com/lbryio/reflector.go/cluster"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
Use: "cluster [start|join]",
|
2018-06-07 05:48:07 +02:00
|
|
|
Short: "Start(join) to or Start a new cluster",
|
2018-05-29 23:19:40 +02:00
|
|
|
ValidArgs: []string{"start", "join"},
|
2018-02-08 19:33:52 +01:00
|
|
|
Args: argFuncs(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
|
|
|
Run: clusterCmd,
|
|
|
|
}
|
2018-05-30 03:38:55 +02:00
|
|
|
rootCmd.AddCommand(cmd)
|
2018-02-08 19:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func clusterCmd(cmd *cobra.Command, args []string) {
|
2018-05-29 23:19:40 +02:00
|
|
|
port := 17946
|
|
|
|
var c *cluster.Cluster
|
|
|
|
if args[0] == "start" {
|
|
|
|
c = cluster.New(port, "")
|
2018-02-08 19:33:52 +01:00
|
|
|
} else {
|
2018-05-29 23:19:40 +02:00
|
|
|
c = cluster.New(port+1+int(crypto.RandInt64(1000)), "127.0.0.1:"+strconv.Itoa(port))
|
2018-02-08 19:33:52 +01:00
|
|
|
}
|
2018-05-29 23:19:40 +02:00
|
|
|
|
|
|
|
err := c.Connect()
|
2018-02-08 19:33:52 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-interruptChan
|
2018-05-29 23:19:40 +02:00
|
|
|
c.Shutdown()
|
2018-02-08 19:33:52 +01:00
|
|
|
}
|