Skip to content

Latest commit

 

History

History
93 lines (72 loc) · 2.42 KB

README.md

File metadata and controls

93 lines (72 loc) · 2.42 KB

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()
}