Coder Social home page Coder Social logo

workerful's Introduction

Workerful

GitHub tag Build Status codecov Go Report Card GoDoc MIT license sprbox

Workerful is a minimal yet powerful worker-pool implementation with some helper funcs for a simple usage.

Install

$ go get github.com/oblq/workerful

Quickstart

It is possible to pass the config as a yaml file path, the workerful.Config struct or none of the above (the default values will be loaded).
The config struct takes two parameters:

Parameter Description Default
queue_size Determine the size of the queue. Use 0 to have an unbuffered jobQueue channel. 0 (unbuffered)
workers The number of parallel jobs to be executed. Using 0 will load the default value. runtime.NumCPU()

Get a new instance (with unbuffered jobQueue):

wp := workerful.New("", &workerful.Config{QueueSize: 0, Workers: 0})

Push a job

You can push a simple func() error or a more complex custom Job (see workerful.Job interface).

Push a simple func:

wp.PushFunc(func() error { 
    println("func executed...")
    return nil
})

// Non blocking.
wp.PushFuncAsync(func() error { 
    println("func executed...")
    return nil
})

Push a custom job if you need to pass parameters or for more complicated stuff:

// CustomJob implement the workerful.Job interface (F())
type CustomJob struct {
    WG  *sync.WaitGroup
    ID  int
}

func (cj CustomJob) F() error {
    time.Sleep(time.Second)
    println("job", cj.ID, "executed...")
    cj.WG.Done()
    return nil
}
    
wp := workerful.New("", nil)

waitGroup := sync.WaitGroup{}
waitGroup.Add(2)

wp.PushJob(CustomJob{&waitGroup, 1})
    
// Non blocking.
wp.PushJobAsync(CustomJob{&waitGroup, 2})

waitGroup.Wait()

println("finished")
wp.Stop()

Get status

doneJobs, failedJobs, inQueueJobs := wp.Status()

Graceful shutdown

wp := workerful.New("./workerful.yml", nil)
wp.Stop()

Author

License

Workerful is available under the MIT license. See the LICENSE file for more information.

workerful's People

Contributors

oblq avatar

Stargazers

thorny avatar Thomas Jost avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.