zrok/cmd/zrokloop/loop.go

58 lines
953 B
Go
Raw Normal View History

2022-09-30 21:23:53 +02:00
package main
2022-10-03 18:00:26 +02:00
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
2022-09-30 21:23:53 +02:00
func init() {
rootCmd.AddCommand(newRun().cmd)
}
type run struct {
2022-10-03 18:00:26 +02:00
cmd *cobra.Command
loopers int
2022-09-30 21:23:53 +02:00
}
func newRun() *run {
cmd := &cobra.Command{
Use: "run",
Short: "Start a loop agent",
Args: cobra.ExactArgs(0),
}
r := &run{cmd: cmd}
cmd.Run = r.run
2022-10-03 18:00:26 +02:00
cmd.Flags().IntVarP(&r.loopers, "loopers", "l", 1, "Number of current loopers to start")
2022-09-30 21:23:53 +02:00
return r
}
func (r *run) run(_ *cobra.Command, _ []string) {
2022-10-03 18:00:26 +02:00
var loopers []*looper
for i := 0; i < r.loopers; i++ {
l := newLooper(i)
loopers = append(loopers, l)
go l.run()
}
for _, l := range loopers {
<-l.done
}
}
type looper struct {
id int
done chan struct{}
}
func newLooper(id int) *looper {
return &looper{
id: id,
done: make(chan struct{}),
}
}
func (l *looper) run() {
logrus.Infof("starting #%d", l.id)
defer close(l.done)
defer logrus.Infof("stopping #%d", l.id)
2022-09-30 21:23:53 +02:00
}