| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package worker
- import (
- "context"
- "log"
- "github.com/google/uuid"
- )
- type Job interface {
- ID() uuid.UUID
- Run() error
- }
- type Worker struct {
- WorkerPool chan chan Job
- JobChannel chan Job
- ctx context.Context
- }
- func NewWorker(ctx context.Context, workerPool chan chan Job) Worker {
- return Worker{
- WorkerPool: workerPool,
- JobChannel: make(chan Job),
- ctx: ctx,
- }
- }
- func (w Worker) Start() {
- go func() {
- for {
- w.WorkerPool <- w.JobChannel
- select {
- case job := <-w.JobChannel:
- if err := job.Run(); err != nil {
- log.Default().Printf("error running job %s: %s", job.ID(), err.Error())
- }
- case <-w.ctx.Done():
- return
- }
- }
- }()
- }
|