Code Monkey home page Code Monkey logo

go-jobs's Introduction

Bounded job execution with in memory queue

Run jobs with controlled concurrency.

Installation

To install, use go get

go get github.com/IQ-tech/go-jobs

Usage

Creating a new dispatcher

Creating a dispatcher that will run 2 concurrent jobs and queue 5 jobs.

dispatcher := jobs.NewDispatcher(2, 5)

Running jobs

Run enqueues a job to that will be picked up by one of the workers.
Run will block if the queue is full.

dispatcher := jobs.NewDispatcher(2, 1)

// Does not block because a worker picks the job from
// the queue immediately after we add the job to it.
//
// We have 2 workers and 1 is busy now.
dispatcher.Run(func() {
  time.Sleep(10 * time.Second)
})


// Does not block because a worker picks the job from
// the queue immediately after we add the job to it.
//
// We have 2 workers and both are busy now.
dispatcher.Run(func() {
  time.Sleep(10 * time.Second)
})

// Does not block because even though both workers are busy,
// we have a queue that can have at most one job in it.
dispatcher.Run(func() {
  time.Sleep(10 * time.Second)
})

// Blocks because both workers are busy and the queue is full.
dispatcher.Run(func() {
  time.Sleep(10 * time.Second)
})

Testing

Asynchronicity makes testing more annoying, because of that we have the Sync
method to make testing easier.

Calling Sync will change the dispatcher to sync mode(default is async)
which makes the dispatcher execute jobs synchronously instead of enqueuing them.

package package_test

func Test_Sync(t*testing.T){
  t.Parallel()
	// We dont need workers or a queue if we are running in sync mode
	dispatcher := NewDispatcher(0, 0)

	dispatcher.Sync()

	ran := false
	dispatcher.Run(func() {
		ran = true
	})

  assert.True(t, ran)
}

go-jobs's People

Contributors

poorlydefinedbehaviour avatar

Watchers

Airton Vancin Junior avatar James Cloos avatar Raphael Fabeni avatar Lucas Rodrigues avatar Tim Fontes avatar

go-jobs's Issues

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.