nginx-proxy-manager-zh/backend/internal/jobqueue/main.go

48 lines
877 B
Go
Raw Normal View History

2022-07-14 18:52:38 -04:00
package jobqueue
import (
"context"
2023-02-24 02:19:07 -05:00
"github.com/rotisserie/eris"
2022-07-14 18:52:38 -04:00
)
var (
ctx context.Context
cancel context.CancelFunc
worker *Worker
)
2022-07-15 00:26:12 -04:00
// Start will intantiate the queue and start doing work
2022-07-14 18:52:38 -04:00
func Start() {
ctx, cancel = context.WithCancel(context.Background())
q := &Queue{
jobs: make(chan Job),
ctx: ctx,
cancel: cancel,
}
// Defines a queue worker, which will execute our queue.
worker = newWorker(q)
// Execute jobs in queue.
go worker.doWork()
}
2022-07-15 00:26:12 -04:00
// Shutdown will gracefully stop the queue
func Shutdown() error {
if cancel == nil {
2023-02-24 02:19:07 -05:00
return eris.New("Unable to shutdown, jobqueue has not been started")
2022-07-15 00:26:12 -04:00
}
cancel()
return nil
}
2022-07-14 18:52:38 -04:00
// AddJob adds a job to the queue for processing
func AddJob(j Job) error {
if worker == nil {
2023-02-24 02:19:07 -05:00
return eris.New("Unable to add job, jobqueue has not been started")
2022-07-14 18:52:38 -04:00
}
worker.Queue.AddJob(j)
return nil
}