hantu

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 5 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Hantu

Provides a simple configurable background worker library which works by using fire and forget principle. The library is also built with microservice in mind; thus every time the server restart, any idle job at that server will be transfered to another running instance.

Functions

  1. Start
        bgworker.Worker().Start()
    
  2. Stop
        bgworker.Worker().Stop()
    
  3. Register
    	bgworker.Worker().Register(
    	"test",
    	context.Background(),
        func (ctx context.Context, request any) (any,error) {
    		time.Sleep(5 * time.Second)
    		fmt.Println("Processing: " + request.(string))
    		wg.Done()
    		return nil,nil
        })	
    
  4. Queue
    	bgworker.Queue(schema.Job{
    		Id: "1",
    		Name: "test",
    		Request: "1",
    	})
    
  5. Dequeue
    	bgworker.Dequeue(schema.Job{
    		Id: "1",
    		Name: "test",
    		Request: "1",
    	})
    

Schema

type  Job  struct {
	Id string
	Name string
	Cheksum string
	Request string
	RequestTimestamp string
	Timestamp string
	Status string
	Details string
}

Options

Id string // unique process identifier

Domain string // worker name

RedisHost string // redis (support live beyond the service)

RedisPort string // redis (support live beyond the service)

RedisTTL uint64 // redis (support live beyond the service)

Interval time.Duration // checksum interval

TTL time.Duration // length of time to live 

Max int // max number of conccurencies

Basic Example

package main

import (
"context"
"time"
"sync"
"github.com/johnjerrico/hantu/schema"
"github.com/johnjerrico/hantu"
)
func main(){
  var wg sync.WaitGroup
  wg.Add(6)
  bgworker := hantu.New(hantu.Option{
   Domain: "test",
   Id: "1",
   Max: 7, // max conccurencies
   TTL: 5 * time.Second, // time to live
  })
  //register worker
  bgworker.Worker().Register(
   "test",
   context.Background(),
   func (ctx context.Context, request any) (any,error) {
   	time.Sleep(5 * time.Second)
   	fmt.Println("Processing: " + request.(string))
   	wg.Done()
   	return nil,nil
  })
  bgworker.Worker().Start()
  bgworker.Queue(schema.Job{
   	Id: "1",
   	Name: "test",
   	Request: "1",
  })
  bgworker.Queue(schema.Job{
   	Id: "2",
   	Name: "test",
   	Request: "2",
  })
  bgworker.Queue(schema.Job{
   	Id: "3",
   	Name: "test",
   	Request: "3",
  })
  bgworker.Queue(schema.Job{
   	Id: "4",
   	Name: "test",
   	Request: "4",
  })
  bgworker.Queue(schema.Job{
   	Id: "5",
   	Name: "test",
   	Request: "5",
  })
  bgworker.Queue(schema.Job{
   	Id: "6",
   	Name: "test",
   	Request: "6",
  })
  wg.Wait()
  bgworker.Worker().Stop()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	Id     string
	Domain string
	Max    int
}

type Server

type Server interface {
	Worker() worker.Worker
	Dequeue(job schema.Job) error
	Queue(job schema.Job) error
	Count() (int, error)
}

func New

func New(opt Option) Server

func Singleton added in v0.0.8

func Singleton(opt Option) Server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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