murder

package module
v0.0.0-...-9b89e5d Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2018 License: GPL-3.0 Imports: 8 Imported by: 0

README

Murder of crows

Codacy Badge

Miniature extendable queue orchestration library, capable of creating multiple equal sized queues that can be consumed in parallel without needing to lock the entire queue for processing from a single worker.

Usage

Create a Murder object to manage queues, assigning a Crow object as an adapter to communicate with any atomic-enabled system, a RedisCrow is built into the library.

import "github.com/mohamed-essam/murder-of-crows"
import "redis.v5"
import "fmt"

func main() {
  redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379", Password: "", DB: 0})
  redisCrow := &murder.RedisCrow{Redis: redisClient}
  // Create a new murder instance with 100 as a bulk size, 1 second TTL and a worker_group_name
  // Worker group name separates different queue systems by prefixing them
  // 100 is a rough number as no locking is used in enqueueing to speed this operation up, the actual queue size will be >= 100
  murderInstance := murder.NewMurder(100, 1, redisCrow, "worker_group_name") 

  for (i := 0; i < 100; i++) {
    murderInstance.Add(i)
  }

  lockKey, locked := murderInstance.Lock() // This locks a full queue giving only the worker with lockKey access to it

  if (locked) { // this may be false if there are no full queues
    jobs := murderInstance.Get(lockKey) // jobs are always returned as an array of strings

    for _, obj := range jobs { // Prints out 0 through 99
      fmt.Printf("%s\n", obj)
    }
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crow

type Crow interface {
	QueueSize(string) int                   // Query main queue size
	QueueTimeSinceCreation(string) int      // Query time since queue creation
	AddToQueue(string, interface{}, bool)   // Add object to queue
	GetQueueContents(string) []string       // Retrieve all contents of queue
	ClearQueue(string, string) error        // Clear all queue contents
	CreateLockKey(string, string, int) bool // Create lock key for a queue, confirm if lock acquired, and set TTL
	IsLocked(string) bool                   // Check if a queue is locked
	FindQueueByKey(string) (string, bool)   // Get the queue for a lock key if exists
	ExtendLockKey(string, int)              // Extend TTL of lock key to value provided
	RemoveLockKey(string)                   // Removes a lock key if exists
	MoveToReady(string, string)             // Move a queue to ready to process queues
	GetReadyQueues(string) []string         // Get full queues
}

Crow : Interface for any storage system for the orchestrator to use

type Murder

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

Murder : Orchestra for queueing systems Provides availability via introducing multiple queues, locking and clearing

func NewMurder

func NewMurder(bulkSize, TTL int, crow Crow, groupID string) *Murder

NewMurder : Returns a new instance of murder with the given options

func NewMurderWithAge

func NewMurderWithAge(bulkSize, TTL int, crow Crow, groupID string, queueAge int) *Murder

func (*Murder) Ack

func (m *Murder) Ack(lockKey string)

Ack : Acknowledge processing of a queue lock extending its time to kill Useful for long running jobs

func (*Murder) Add

func (m *Murder) Add(obj interface{})

Add : Create a job in any queue

func (*Murder) AgeConfigured

func (m *Murder) AgeConfigured() bool

func (*Murder) Get

func (m *Murder) Get(lockKey string) []string

Get : Get contents of a queue given its lock key Ensuring the worker locked the queue and acquired the lock key

func (*Murder) Lock

func (m *Murder) Lock() (string, bool)

Lock : Lock a queue returning a lock key that is needed for acknowledging the processing of the queue If no queue is ready to process, returns empty string and false

func (*Murder) Mark

func (m *Murder) Mark(lockKey string)

Mark : Mark a locked queue as done, and its jobs disposable

func (*Murder) Unlock

func (m *Murder) Unlock(lockKey string)

Unlock : Unlock a queue, but not marking it as done Useful when a worker knows it is being killed and won't be able to finish the job

type RedisCrow

type RedisCrow struct {
	Redis *redis.Client
}

func (*RedisCrow) AddToQueue

func (c *RedisCrow) AddToQueue(groupName string, obj interface{}, ageConfigured bool)

func (*RedisCrow) ClearQueue

func (c *RedisCrow) ClearQueue(queueName string, groupID string) error

func (*RedisCrow) CreateLockKey

func (c *RedisCrow) CreateLockKey(queueName string, lockKey string, TTL int) bool

func (*RedisCrow) CurrentQueue

func (c *RedisCrow) CurrentQueue(groupName string) string

func (*RedisCrow) ExtendLockKey

func (c *RedisCrow) ExtendLockKey(lockKey string, TTL int)

func (*RedisCrow) FindQueueByKey

func (c *RedisCrow) FindQueueByKey(lockKey string) (string, bool)

func (*RedisCrow) GetQueueContents

func (c *RedisCrow) GetQueueContents(queueName string) []string

func (*RedisCrow) GetReadyQueues

func (c *RedisCrow) GetReadyQueues(groupID string) []string

func (*RedisCrow) IsLocked

func (c *RedisCrow) IsLocked(queueName string) bool

func (*RedisCrow) MoveToReady

func (c *RedisCrow) MoveToReady(groupName, newName string)

func (*RedisCrow) QueueSize

func (c *RedisCrow) QueueSize(groupName string) int

func (*RedisCrow) QueueTimeSinceCreation

func (c *RedisCrow) QueueTimeSinceCreation(groupName string) int

func (*RedisCrow) RemoveLockKey

func (c *RedisCrow) RemoveLockKey(lockKey string)

Jump to

Keyboard shortcuts

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