looper skeleton (#40)

This commit is contained in:
Michael Quigley 2022-10-03 12:00:26 -04:00
parent 37c1de1103
commit 00c0328661
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -1,13 +1,17 @@
package main
import "github.com/spf13/cobra"
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(newRun().cmd)
}
type run struct {
cmd *cobra.Command
cmd *cobra.Command
loopers int
}
func newRun() *run {
@ -18,8 +22,36 @@ func newRun() *run {
}
r := &run{cmd: cmd}
cmd.Run = r.run
cmd.Flags().IntVarP(&r.loopers, "loopers", "l", 1, "Number of current loopers to start")
return r
}
func (r *run) run(_ *cobra.Command, _ []string) {
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)
}