Skip to content

Multiple Job Queue provides an API to process jobs in an asynchronous way using one ore more queues.

License

Notifications You must be signed in to change notification settings

jorbriib/multiple-job-queue

Repository files navigation

Go Report Card Build Status

Multiple Job Queue provides an API to process jobs in asynchronous way. Also, it allows you to defer the processing of a time-consuming task and creates a number of workers to parallelize the execution of each job.

Installation

The routing package is just a common Go module. You can install it as any other Go module.

go get github.com/jorbriib/multiple-job-queue

To get more information just review the official Go blog regarding this topic.

Usage

This is just a quick introduction, view the GoDoc for details.

Basic usage example in a web server:

package main

import (
    mjq "github.com/jorbriib/multiple-job-queue"
    "fmt"
    "log"
    "net/http"
    "time"
)
type TestJob struct {
}

func (tj *TestJob) Handle() {
    time.Sleep(2 * time.Second)
    fmt.Println("Job handled")
}

func handler(w http.ResponseWriter, r *http.Request) {
    dispatcher := mjq.GetDispatcher()
    testJob1 := &TestJob{}
    // Dispatch dispatches testJob1 to high queue
    _ = dispatcher.Dispatch(testJob1, "high")
    w.WriteHeader(200)
}

func main() {
    // InitializeQueues creates a default queue with 1 worker
    _ = mjq.InitializeQueues(1,
            mjq.AddQueue("high", 4), // AddQueue creates a high queue with 4 workers
            mjq.AddQueue("medium", 2),
    	    mjq.AddQueue("low", 1))

    http.HandleFunc("/job", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Basic usage example in a console command:

package main
    
import (
    mjq "github.com/jorbriib/multiple-job-queue"
    "math/rand"
)
    
func main() {
    q := mjq.InitializeQueues(0,
      		mjq.AddQueue("high", 5),
      		mjq.AddQueue("medium", 2),
      		mjq.AddQueue("low", 1))
      
    queues := []string{"high", "medium", "low"}
   
    dispatcher := mjq.GetDispatcher()
    for i := 0; i < 100; i++ {
        randomQueue := rand.Intn(len(queues))
        testJob1 := &TestJob{}
        _ = dispatcher.Dispatch(testJob1, queues[randomQueue])
    
    }
    q.WaitUntilFinish()
}  
    	    

About

Multiple Job Queue provides an API to process jobs in an asynchronous way using one ore more queues.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages