https://github.com/mvdkleijn/go-simplequeue
Simple locking queue system with workers
https://github.com/mvdkleijn/go-simplequeue
go golang job job-queue library locking queue simple worker
Last synced: 8 months ago
JSON representation
Simple locking queue system with workers
- Host: GitHub
- URL: https://github.com/mvdkleijn/go-simplequeue
- Owner: mvdkleijn
- License: mpl-2.0
- Created: 2021-08-20T14:39:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-02-19T15:51:50.000Z (almost 2 years ago)
- Last Synced: 2025-03-31T14:57:18.301Z (9 months ago)
- Topics: go, golang, job, job-queue, library, locking, queue, simple, worker
- Language: Go
- Homepage:
- Size: 49.8 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# go-simplequeue

[](https://app.codacy.com/gh/mvdkleijn/go-simplequeue)
[](https://goreportcard.com/report/github.com/mvdkleijn/go-simplequeue) [](https://liberapay.com/mvdkleijn/) [](https://ko-fi.com/O4O7H6C73)
Simple locking queue system with workers.
Also see:
## Support
- Go versions, see: https://endoflife.date/go
Source code and issues: https://github.com/mvdkleijn/go-simplequeue
Also see: https://vanderkleijn.net/posts/announcing-go-simplequeue/
## Usage / Example
```golang
// Define a job that conforms to the simplequeue.Job interface
type MyJob struct {
id int
}
func (mj *MyJob) ID() int64 {
return int64(mj.id)
}
func (mj *MyJob) Do() {
// Lets just pause the job for a little time
ms := time.Duration(rand.Intn(1000)+1) * time.Millisecond
time.Sleep(ms)
fmt.Printf("Job %d executing\n", mj.ID())
}
// Create some jobs for our test
func createJobs(number int) []*MyJob {
jobs := make([]*MyJob, 0)
for i := 1; i <= number; i++ {
jobs = append(jobs, &MyJob{id: i})
}
return jobs
}
// Run our program
func main() {
ctx := context.Background()
// How much we want of each
numWorkers := 15
numJobs := 200
// Create some jobs with a helper function
jobs := createJobs(numJobs)
// Create a queue
q := sq.CreateQueue(ctx)
// Initialize the workers
workers := sq.InitializeWorkers(ctx, numWorkers)
fmt.Printf("Number of workers in pool: %d\n", len(workers))
fmt.Printf("Number of jobs for queue: %d\n", len(jobs))
// Push the jobs onto the Queue
for _, job := range jobs {
q.Push(job)
}
// Process the queue with some workers
q.Process(ctx, workers)
// Show some stats afterwards
var totalJobsHandled int64 = 0
for _, w := range workers {
totalJobsHandled += w.Handled()
fmt.Printf("Worker %d processed a total of %d jobs\n", w.ID(), w.Handled())
}
fmt.Printf("Total jobs handled: %d\n", totalJobsHandled)
fmt.Printf("Total workers: %d\n", len(workers))
}
```
# Licensing
Go-simplequeue is made available under the [MPL-2.0](https://choosealicense.com/licenses/mpl-2.0/)
license. The full details are available from the [LICENSE](/LICENSE) file.