From 00c032866160594f7e98e4d06a87223f8565b929 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 3 Oct 2022 12:00:26 -0400 Subject: [PATCH] looper skeleton (#40) --- cmd/zrokloop/loop.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/cmd/zrokloop/loop.go b/cmd/zrokloop/loop.go index edaa6538..8fd1fde7 100644 --- a/cmd/zrokloop/loop.go +++ b/cmd/zrokloop/loop.go @@ -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) }