queue

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

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

Go to latest
Published: May 26, 2024 License: MIT Imports: 10 Imported by: 0

README

Super Super Simple Queue

This readme doc was wrote by AI.

This is a simple super implementation of a queue management system using SQLite. It can be used to handle job queuing, processing, and retry mechanisms.

Table of Contents

Getting Started

To get started with the Queue Management System, follow these steps:

  1. Install dependencies:

    • Install Go 1.22 or later
    • Sqlite library
  2. Install the packages:

    Run the following command to install the required packages in the go.mod file.

    go get github.com/supersupersimple/queue
    

Configuration

QueueConfig

type QueueConfig struct {
 Logger Logger

 DefaultQueueSetting QueueSetting
 QueueSetting        map[string]QueueSetting

 WorkerPoolSize   int
 PickJobsInterval time.Duration

 RePickProcessingJobInterval time.Duration
 RePickProcessingJobDelay    time.Duration

 DeleteJobSetting DeleteJobSetting
}

Running the Example Application

To run the application, perform the following steps:

  1. Navigate to the project's directory.

  2. Install the dependencies, if you haven't done so already.

    go mod download
    
  3. Run the binary:

    go run example/main.go
    

Usage

Init and Start Queue

Call New and Start functions to initialize and start the queue.

Enqueuing Jobs

Enqueuing jobs can be done by calling the Enqueue function. The function takes a Job struct as an argument. And pass Job when job was executed.

MENTION: the queue name + ref id should be unique for all jobs.

EnqueueJob structure
  • JobBody: ([]byte) Job data or payload in bytes. You can define your job body format.
Example
enqueueJob := &queue.EnqueueJob{
    QueueName:     "SampleQueue", // if not set, will be default
    RefID:        "SampleRefID", // if not set, will generate automatically
    Priority:      0, // if not set, will be 0. Higher priority will be executed first
    JobBody:       []byte("Sample job body"), // must set
    ScheduledTime: time.Now().Add(10 * time.Minute), // if not set, will be scheduled immediately
}
err := queue.Enqueue(context.Background(), enqueueJob)
if err != nil {
    log.Printf("Failed to enqueue job: %v", err)
}
Handling Jobs

When job was triggered, the handle func will be executed. The handle func takes a ctx, queueName, refID, and jobBody as arguments.

func Handler(ctx context.Context, queueName, refID string, jobBody []byte) error {
	// process the job
	return nil // if return err or panic, will mark this job as failed or retry this job that depends on the queue config
}

Contributing

Contributions are welcome! Please submit pull requests or open issues for any improvements or additions you would like to see in the project. Ensure your changes are tested thoroughly and follow the existing coding style before submitting a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

This project was built leveraging the following libraries:

Feel free to suggest any updates or edits by submitting a pull request. Your contributions are welcome!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeleteJobSetting

type DeleteJobSetting struct {
	DeleteSuccessfulJob bool
	JobInterval         time.Duration
	StoreTime           time.Duration
}

type EnqueueJob

type EnqueueJob struct {
	QueueName     string
	RefID         string
	Priority      uint
	JobBody       []byte
	ScheduledTime *time.Time
}

type Handler

type Handler func(ctx context.Context, queueName string, refId string, jobBody []byte) error

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

type Queue

type Queue interface {
	Start()
	Stop()

	Enqueue(ctx context.Context, j *EnqueueJob) error
}

func New

func New(db *sql.DB, config *QueueConfig, h Handler) (Queue, error)

type QueueConfig

type QueueConfig struct {
	Logger Logger

	DefaultQueueSetting QueueSetting
	QueueSetting        map[string]QueueSetting

	WorkerPoolSize   int
	PickJobsInterval time.Duration

	RePickProcessingJobInterval time.Duration
	RePickProcessingJobDelay    time.Duration

	DeleteJobSetting DeleteJobSetting
}

func NewDefaultConfig

func NewDefaultConfig() *QueueConfig

type QueueSetting

type QueueSetting struct {
	Disable       bool
	RetryTimes    uint
	RetryInterval time.Duration
}

func NewDefaultQueueSetting

func NewDefaultQueueSetting() QueueSetting

Directories

Path Synopsis
ent
job

Jump to

Keyboard shortcuts

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