Skip to content

gopi-frame/queue

Repository files navigation

Overview

Go Reference Go codecov Go Report Card License: MIT

Package queue provides a task queue implementation.

Installation

go get -u -v github.com/gopi-frame/queue

Import

import "github.com/gopi-frame/queue"

Usage

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)
    }
}

Drivers

How to create custom driver

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
})

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages