aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/worker.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-02-26 19:54:48 +0100
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-06-18 16:30:36 +0200
commitc8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch)
treefaee639a4c55c5dc3bfc59a5400026822c40221d /pkg/worker/worker.go
downloadlens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz
lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2
lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip
feat: Inicial commit
It contains rough template for the server and runners. It contains rough template for the server and runners.
Diffstat (limited to 'pkg/worker/worker.go')
-rw-r--r--pkg/worker/worker.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go
new file mode 100644
index 0000000..c52f0be
--- /dev/null
+++ b/pkg/worker/worker.go
@@ -0,0 +1,54 @@
+package worker
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "sync"
+)
+
+type (
+ // Worker should watch for context
+ Worker interface {
+ Start(context.Context) error
+ }
+
+ Work struct {
+ Name string
+ Worker Worker
+ }
+
+ WorkerPool struct {
+ workers []*Work
+ wg sync.WaitGroup
+ }
+)
+
+func NewWorkerPool() *WorkerPool {
+ return &WorkerPool{}
+}
+
+func (self *WorkerPool) AddWorker(name string, worker Worker) {
+ self.workers = append(self.workers, &Work{
+ Name: name,
+ Worker: worker,
+ })
+}
+
+func (self *WorkerPool) Start(ctx context.Context) {
+ for _, w := range self.workers {
+ self.wg.Add(1)
+ go func(w *Work) {
+ defer self.wg.Done()
+ if err := w.Worker.Start(ctx); err != nil && !errors.Is(err, context.Canceled) {
+ fmt.Println("Error ", w.Name, err.Error())
+ } else {
+ fmt.Println(w.Name, "done")
+ }
+ }(w)
+ }
+}
+
+func (self *WorkerPool) Wait() {
+ self.wg.Wait()
+}