workqueue

package module
v0.0.0-...-a499989 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 4 Imported by: 0

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

func New(numWorkers int) Job

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!")
}
Output:

func (*Job) AddQueue

func (obj *Job) AddQueue(p any, f func(any))

AddQueue will add to the work to be done

func (*Job) RunAsynchronously

func (obj *Job) RunAsynchronously()

RunAsynchronously will execute all jobs with all available workers without waiting

func (*Job) RunAsynchronouslyWithChannel

func (obj *Job) RunAsynchronouslyWithChannel() chan string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL