wrq

package module
v0.0.0-...-ef2023e Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: MIT Imports: 1 Imported by: 2

README

wrq

A simple worker queue, as explained in this blog by marcio with funcionality to gracefully finish the jobs when the worker exits

Usage

package main

import (
  "time"
  "fmt"
  "github.com/sankalpjonn/wrq"
)

type job struct {
  name string
  delay time.Duration
}

func(j *job) Name() string {
  return j.name
}

func(j *job) Execute() error {
  time.Sleep(j.delay)
  fmt.Println("executed job:", j.name)
  return nil
}

func main() {
  // returns a dispatcher with 100 workers and a queue size of 100
  w := wrq.New()
  defer w.Stop()

  // to customise the number of workers and queue size, use
  // w := wrq.NewWithSettings(NAME, QUEUE_SIZE, MAX_WORKERS)
  
  j1 := &job{
    name: "test job 1 sec",
    delay: time.Second * 1,
  }

  j2 := &job{
    name: "test job 2 sec",
    delay: time.Second * 2,
  }

  w.AddJob(j1)
  w.AddJob(j2)

  // wait for jobs to execute
  time.Sleep(time.Second * 5)
}

Documentation

Index

Constants

View Source
const (
	DEFAULT_NAME       = "wrq"
	DEFAULT_QUEUE_SIZE = 100
	DEFAULT_WORKERS    = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

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

func New

func New() *Dispatcher

func NewWithSettings

func NewWithSettings(name string, queueSize int, maxWorkers int) *Dispatcher

func (*Dispatcher) AddJob

func (d *Dispatcher) AddJob(job Job)

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

type Job

type Job interface {
	Execute() error
	Name() string
}

type Worker

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

func NewWorker

func NewWorker(id int, workerPool chan chan Job, wg *sync.WaitGroup) *Worker

Jump to

Keyboard shortcuts

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