added params to cmd

This commit is contained in:
marvzhang
2021-07-10 21:47:30 +08:00
parent f44a29cff1
commit 5b2aff713e
11 changed files with 203 additions and 24 deletions

View File

@@ -9,8 +9,17 @@ type App interface {
Stop()
}
type MasterApp interface {
type NodeApp interface {
App
interfaces.WithConfigPath
SetGrpcAddress(address interfaces.Address)
}
type MasterApp interface {
NodeApp
SetRunOnMaster(ok bool)
}
type WorkerApp interface {
NodeApp
}

View File

@@ -10,6 +10,7 @@ import (
type Master struct {
// settings
runOnMaster bool
grpcAddress interfaces.Address
// dependencies
interfaces.WithConfigPath
@@ -22,6 +23,10 @@ type Master struct {
quit chan int
}
func (app *Master) SetGrpcAddress(address interfaces.Address) {
app.grpcAddress = address
}
func (app *Master) SetRunOnMaster(ok bool) {
app.runOnMaster = ok
}
@@ -56,9 +61,15 @@ func NewMaster(opts ...MasterOption) (app MasterApp) {
opt(m)
}
// service options
var svcOpts []service.Option
if m.grpcAddress != nil {
svcOpts = append(svcOpts, service.WithAddress(m.grpcAddress))
}
// dependency injection
c := dig.New()
if err := c.Provide(service.ProvideMasterService(m.GetConfigPath())); err != nil {
if err := c.Provide(service.ProvideMasterService(m.GetConfigPath(), svcOpts...)); err != nil {
panic(err)
}
if err := c.Invoke(func(masterSvc interfaces.NodeMasterService) {

View File

@@ -1,15 +1,37 @@
package apps
import "github.com/crawlab-team/crawlab-core/interfaces"
type MasterOption func(app MasterApp)
func WithConfigPath(path string) MasterOption {
func WithMasterConfigPath(path string) MasterOption {
return func(app MasterApp) {
app.SetConfigPath(path)
}
}
func WithMasterGrpcAddress(address interfaces.Address) MasterOption {
return func(app MasterApp) {
app.SetGrpcAddress(address)
}
}
func WithRunOnMaster(ok bool) MasterOption {
return func(app MasterApp) {
app.SetRunOnMaster(ok)
}
}
type WorkerOption func(app WorkerApp)
func WithWorkerConfigPath(path string) WorkerOption {
return func(app WorkerApp) {
app.SetConfigPath(path)
}
}
func WithWorkerGrpcAddress(address interfaces.Address) WorkerOption {
return func(app WorkerApp) {
app.SetGrpcAddress(address)
}
}

View File

@@ -1,16 +1,33 @@
package apps
import (
"github.com/crawlab-team/crawlab-core/config"
"github.com/crawlab-team/crawlab-core/interfaces"
"github.com/crawlab-team/crawlab-core/node/service"
"go.uber.org/dig"
)
type Worker struct {
handler *Handler
quit chan int
// settings
grpcAddress interfaces.Address
// dependencies
interfaces.WithConfigPath
workerSvc interfaces.NodeWorkerService
// internals
quit chan int
}
func (app *Worker) SetGrpcAddress(address interfaces.Address) {
app.grpcAddress = address
}
func (app *Worker) Init() {
initApp("handler", app.handler) // handler
}
func (app *Worker) Start() {
go app.handler.Start()
go app.workerSvc.Start()
}
func (app *Worker) Wait() {
@@ -18,14 +35,37 @@ func (app *Worker) Wait() {
}
func (app *Worker) Stop() {
app.handler.Stop()
app.quit <- 1
}
func NewWorker() *Worker {
return &Worker{
handler: NewHandler(),
quit: make(chan int, 1),
func NewWorker(opts ...WorkerOption) (app *Worker) {
// worker
w := &Worker{
WithConfigPath: config.NewConfigPathService(),
quit: make(chan int, 1),
}
// apply options
for _, opt := range opts {
opt(w)
}
// service options
var svcOpts []service.Option
if w.grpcAddress != nil {
svcOpts = append(svcOpts, service.WithAddress(w.grpcAddress))
}
// dependency injection
c := dig.New()
if err := c.Provide(service.ProvideWorkerService(w.GetConfigPath(), svcOpts...)); err != nil {
panic(err)
}
if err := c.Invoke(func(workerSvc interfaces.NodeWorkerService) {
w.workerSvc = workerSvc
}); err != nil {
panic(err)
}
return w
}

View File

@@ -2,11 +2,26 @@ package cmd
import (
"crawlab/apps"
"fmt"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
runOnMaster bool
masterConfigPath string
masterGrpcAddress string
)
func init() {
rootCmd.AddCommand(masterCmd)
masterCmd.PersistentFlags().StringVarP(&masterConfigPath, "config-path", "c", "", "Config path of master node")
_ = viper.BindPFlag("configPath", masterCmd.PersistentFlags().Lookup("configPath"))
masterCmd.PersistentFlags().StringVarP(&masterGrpcAddress, "grpc-address", "g", "", "gRPC address of master node")
_ = viper.BindPFlag("grpcAddress", masterCmd.PersistentFlags().Lookup("grpcAddress"))
}
var masterCmd = &cobra.Command{
@@ -16,9 +31,26 @@ var masterCmd = &cobra.Command{
Long: `Start a master instance of Crawlab
which runs api and assign tasks to worker nodes`,
Run: func(cmd *cobra.Command, args []string) {
master := apps.NewMaster(
apps.WithRunOnMaster(runOnMaster),
)
// options
var opts []apps.MasterOption
if masterConfigPath != "" {
opts = append(opts, apps.WithMasterConfigPath(masterConfigPath))
viper.Set("config.path", masterConfigPath)
}
opts = append(opts, apps.WithRunOnMaster(runOnMaster))
if masterGrpcAddress != "" {
address, err := entity.NewAddressFromString(masterGrpcAddress)
if err != nil {
fmt.Println(fmt.Sprintf("invalid grpc-address: %s", masterGrpcAddress))
}
opts = append(opts, apps.WithMasterGrpcAddress(address))
viper.Set("grpc.client.address", masterGrpcAddress)
}
// app
master := apps.NewMaster(opts...)
// start
apps.Start(master)
},
}

View File

@@ -10,8 +10,7 @@ import (
var (
// Used for flags.
cfgFile string
runOnMaster bool
cfgFile string
rootCmd = &cobra.Command{
Use: "crawlab",
@@ -30,9 +29,6 @@ func Execute() error {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().BoolVar(&runOnMaster, "runOnMaster", false, "Whether to run tasks on master node (default: false)")
_ = viper.BindPFlag("runOnMaster", rootCmd.PersistentFlags().Lookup("runOnMaster"))
}
func initConfig() {

View File

@@ -2,11 +2,25 @@ package cmd
import (
"crawlab/apps"
"fmt"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
workerConfigPath string
workerGrpcAddress string
)
func init() {
rootCmd.AddCommand(workerCmd)
workerCmd.PersistentFlags().StringVarP(&workerConfigPath, "config-path", "c", "", "Config path of worker node")
_ = viper.BindPFlag("configPath", workerCmd.PersistentFlags().Lookup("configPath"))
workerCmd.PersistentFlags().StringVarP(&workerGrpcAddress, "grpc-address", "g", "", "gRPC address of worker node")
_ = viper.BindPFlag("grpcAddress", workerCmd.PersistentFlags().Lookup("grpcAddress"))
}
var workerCmd = &cobra.Command{
@@ -17,7 +31,26 @@ var workerCmd = &cobra.Command{
serving in the worker node and executes tasks
assigned by the master node`,
Run: func(cmd *cobra.Command, args []string) {
worker := apps.NewWorker()
apps.Start(worker)
// options
var opts []apps.WorkerOption
if workerConfigPath != "" {
opts = append(opts, apps.WithWorkerConfigPath(workerConfigPath))
viper.Set("config.path", workerConfigPath)
}
if workerGrpcAddress != "" {
address, err := entity.NewAddressFromString(workerGrpcAddress)
if err != nil {
fmt.Println(fmt.Sprintf("invalid grpc-address: %s", workerGrpcAddress))
return
}
opts = append(opts, apps.WithWorkerGrpcAddress(address))
viper.Set("grpc.client.address", workerGrpcAddress)
}
// app
master := apps.NewWorker(opts...)
// start
apps.Start(master)
},
}

View File

@@ -61,4 +61,10 @@ notification:
senderIdentity: ''
smtp:
user: ''
password: ''
password: ''
config:
path: ''
grpc:
client:
address: localhost:9666
authKey: Crawlab2021!

View File

@@ -0,0 +1,10 @@
{
"key": "master",
"is_master": true,
"name": "master",
"ip": "",
"mac": "",
"hostname": "",
"description": "",
"auth_key": "Crawlab2021!"
}

View File

@@ -0,0 +1,10 @@
{
"key": "worker-invalid-auth-key",
"is_master": false,
"name": "worker",
"ip": "",
"mac": "",
"hostname": "",
"description": "",
"auth_key": "invalid-auth-key"
}

View File

@@ -0,0 +1,10 @@
{
"key": "worker",
"is_master": false,
"name": "worker",
"ip": "",
"mac": "",
"hostname": "",
"description": "",
"auth_key": "Crawlab2021!"
}