Documentation
¶
Overview ¶
workqueue package allows a simple interface to append a FIFO queue where you are in control of how many gophers are crunching on your data and how it should be started/ended with either waiting `RunSynchronously()` or running immediately `RunASynchronously()`
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
func New ¶
New returns a Job in which you can append functions to have N numWorkers to process
Example ¶
package main
import (
"log"
"math/rand"
"time"
"github.com/DanielRenne/GoCore/core/utils"
workqueue "github.com/DanielRenne/GoCore/core/workQueue"
)
func main() {
type data struct {
sleep int
strParameterExample string
}
min := 0
max := 5
rand.Seed(time.Now().UnixNano())
job := workqueue.New(10)
for i := 0; i < 50; i++ {
randomInt := rand.Intn(max-min+1) + min
job.AddQueue(data{
sleep: randomInt,
strParameterExample: utils.RandStringRunes(6),
}, func(parameter any) {
value, ok := parameter.(data)
if !ok {
log.Println("Could not cast")
return
}
log.Println("Worker starting work....")
time.Sleep(time.Duration(value.sleep) * time.Second)
log.Println(value.strParameterExample, "Slept ", value.sleep)
})
}
job.RunSynchronously()
log.Println("Job Done!")
}
func (*Job) RunAsynchronously ¶
func (obj *Job) RunAsynchronously()
RunAsynchronously will execute all jobs with all available workers without waiting
func (*Job) RunAsynchronouslyWithChannel ¶
RunAsynchronouslyWithChannel will execute all jobs with all available workers without waiting and return you a channel that will be signaled when the jobs are completed
func (*Job) RunSynchronously ¶
func (obj *Job) RunSynchronously()
RunSynchronously will complete your job