gworker

package module
v0.0.0-...-15f48c3 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2018 License: MIT Imports: 3 Imported by: 0

README

gworker

gworker is simple dispatch worker management library written in golang.

Requirement

Go (>= 1.8)

Installation

go get github.com/hlts2/gworker

Example


func main() {

    // Generates 3 workers
    d := gworker.NewDispatcher(3)

    // Register finish monitoring of all jobs.
    // When all jobs are completed, a notification is sent to `<-d.Finish()`.
    d.StartJobObserver()

    for i := 0; i < 100; i++ {

        // Add adds job
        d.Add(func() error {
            time.Sleep(time.Second * 1)
            return nil
        })
    }

    // Start starts workers.
    d.Start()

    for i := 0; i < 100; i++ {
        d.Add(func() error {
            time.Sleep(time.Second * 2)
            return errors.New("errors.New")
        })
    }

    // Scale up the number of worker.
    d.UpScale(20)

FINISH_ALL_JOB:
    for {
        select {
        case err := <-d.JobError():  // get job error
            fmt.Println(err)
        case _ = <-d.Finish():       // When all the jobs are finished, notification comes
            break FINISH_ALL_JOB
        }
    }

    // Stop stops workers.
    // In the case of this example, 23 workers are stopped.
    d.Stop()
}

Future

  • Auto scaling of worker

Automatically scaled up when there are many jobs and fewer workers, while scaled down when there are fewer jobs and more workers.

Author

hlts2

LICENSE

gworker released under MIT license, refer LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher managements worker

func NewDispatcher

func NewDispatcher(workerCount int) *Dispatcher

NewDispatcher returns Dispatcher object

func (*Dispatcher) Add

func (d *Dispatcher) Add(job func() error)

Add adds job

func (*Dispatcher) DownScale

func (d *Dispatcher) DownScale(workerCount int) *Dispatcher

DownScale scales down the numer of worker

func (*Dispatcher) Finish

func (d *Dispatcher) Finish() <-chan struct{}

Finish notifies when all jobs are finished

func (*Dispatcher) GetWorkerCount

func (d *Dispatcher) GetWorkerCount() int

GetWorkerCount returns the number of workers

func (*Dispatcher) JobError

func (d *Dispatcher) JobError() <-chan error

JobError returns error of job

func (*Dispatcher) Start

func (d *Dispatcher) Start() *Dispatcher

Start starts workers

func (*Dispatcher) StartJobObserver

func (d *Dispatcher) StartJobObserver()

StartJobObserver monitors jobs. Then, when all the jobs are completed, the notification is transmitted

func (*Dispatcher) Stop

func (d *Dispatcher) Stop() *Dispatcher

Stop stops workers.

func (*Dispatcher) UpScale

func (d *Dispatcher) UpScale(workerCount int) *Dispatcher

UpScale scales up the numer of worker

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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