Skip to content

Latest commit

History

48 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

core-queue

A simple queue implementation for package Core.

BuildGo ReferencecodecovGo Report CardSourcegraph

Queues in go is not as prominent as in some other languages, since go excels at handling concurrency. However, the deferrableDecorator queue can still offer some benefit missing from the native mechanism, say go channels. The queued job won't be lost even if the system shutdown. In other words, it means jobs can be retried until success. Plus, it is also possible to queue the execution of a particular job until a lengthy period of time. Useful when you need to implement "send email after 30 days" type of Job handler.

Example

package main
import (
"context""fmt"
queue "github.com/DoNewsCode/core-queue""time"
)
typeExampleJobstringfunc (eExampleJob) Type() string {
return"example"
}
func (eExampleJob) Data() interface{} {
returne
}
typeExampleListenerstruct {
chchanstruct{}
}
func (e*ExampleListener) Listen() queue.Job {
returnExampleJob("")
}
func (e*ExampleListener) Process(ctx context.Context, job queue.Job) error {
fmt.Println(job.Data())
e.ch<-struct{}{}
returnnil
}
funcmain() {
queueDispatcher:=queue.NewQueue(queue.NewInProcessDriver())
ctx, cancel:=context.WithCancel(context.Background())
defercancel()
varch=make(chanstruct{})
goqueueDispatcher.Consume(ctx)
queueDispatcher.Subscribe(&ExampleListener{ch: ch})
queueDispatcher.Dispatch(ctx, queue.Adjust(ExampleJob("foo"), queue.Defer(time.Second)))
queueDispatcher.Dispatch(ctx, queue.Adjust(ExampleJob("bar"), queue.Defer(time.Hour)))
<-ch
}

GoDoc

https://pkg.go.dev/github.com/DoNewsCode/core-queue

About

A simple queue implementation for core.

Resources

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages