2020-10-03 09:57:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
type worker struct {
|
|
|
|
name string
|
|
|
|
fn func(context.Context)
|
2020-10-21 09:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWorkerFromFunc(fn func(ctx context.Context)) worker {
|
2022-11-24 11:36:49 +00:00
|
|
|
return worker{
|
2020-10-21 09:26:16 +00:00
|
|
|
fn: fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:59:14 +00:00
|
|
|
func startWorkers(ctx context.Context, c *cfg) {
|
2020-10-03 09:57:02 +00:00
|
|
|
for _, wrk := range c.workers {
|
2023-03-23 14:59:14 +00:00
|
|
|
startWorker(ctx, c, wrk)
|
2022-11-24 11:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:59:14 +00:00
|
|
|
func startWorker(ctx context.Context, c *cfg, wrk worker) {
|
2022-11-24 11:36:49 +00:00
|
|
|
c.wg.Add(1)
|
2020-10-03 09:57:02 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
go func(w worker) {
|
2023-03-23 14:59:14 +00:00
|
|
|
w.fn(ctx)
|
2022-11-24 11:36:49 +00:00
|
|
|
c.wg.Done()
|
|
|
|
}(wrk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWorker(c *cfg, name string) *worker {
|
|
|
|
for _, wrk := range c.workers {
|
|
|
|
if wrk.name == name {
|
|
|
|
return &wrk
|
|
|
|
}
|
2020-10-03 09:57:02 +00:00
|
|
|
}
|
2022-11-24 11:36:49 +00:00
|
|
|
return nil
|
2020-10-03 09:57:02 +00:00
|
|
|
}
|