39 lines
553 B
Go
39 lines
553 B
Go
|
package jobs
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"lmika.dev/lmika/hugo-crm/models"
|
||
|
"log"
|
||
|
)
|
||
|
|
||
|
type Service struct {
|
||
|
queue chan models.Job
|
||
|
}
|
||
|
|
||
|
func New() *Service {
|
||
|
return &Service{
|
||
|
queue: make(chan models.Job, 50),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (j *Service) Start() {
|
||
|
go func() {
|
||
|
for j := range j.queue {
|
||
|
if err := j.Do(context.Background()); err != nil {
|
||
|
log.Printf("job failed %v", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
func (j *Service) Stop() {
|
||
|
q := j.queue
|
||
|
j.queue = nil
|
||
|
close(q)
|
||
|
}
|
||
|
|
||
|
func (j *Service) Queue(ctx context.Context, job models.Job) error {
|
||
|
j.queue <- job
|
||
|
return nil
|
||
|
}
|