Package queue provides a task queue implementation.
go get -u -v github.com/gopi-frame/queue
import "github.com/gopi-frame/queue"
package main
import (
"github.com/gopi-frame/queue"
"sync"
_ "github.com/gopi-frame/queue/driver/memory"
"time"
//_ "github.com/gopi-frame/queue/driver/database"
//_ "github.com/gopi-frame/queue/driver/redis"
)
type CustomJob struct {
queue.Job `json:"-"`
}
func (c *CustomJob) Handle() error {
// do something
return nil
}
func (c *CustomJob) Failed(err error) {
// handle failed job
}
func main() {
driver, err := queue.Open("memory", map[string]any{
"name": "test",
})
if err != nil {
panic(err)
}
q := queue.NewQueue(driver, queue.WorkerNum(3))
go q.Run()
for i := 0; i < 1000; i++ {
q.Enqueue(new(CustomJob))
}
for {
if q.Empty() {
q.Stop()
break
}
time.Sleep(time.Second)
}
}
Just implement the queue.Driver interface
and register it with queue.Register
.
Then you can use your driver in the following way:
driver, err := queue.Open("custom", map[string]any{
// options
})