-
Volker Schukai authoredVolker Schukai authored
Job Queues
Overview
The jobQueue
library in Go aims to serve as a Cron replacement, enabling the scheduled and event-driven execution of tasks in an organized manner.
Getting Started
Prerequisites
- Go 1.20+
go get -u
Installation
git clone https://github.com/yourusername/GoJobQueue.git
cd GoJobQueue
go build
Usage
Import the package and create a new queue.
goCopy codeimport "github.com/yourusername/GoJobQueue"
queue := GoJobQueue.New()
Adding a job:
goCopy codejob := func() {
// Your code here
}
queue.Add(job)
API Reference
Manager
The Manager
is the central orchestrator for job execution, worker assignment, and event handling. It provides methods for managing workers, scheduling jobs, and handling the state of the job queue.
Initializing a Manager
To create a new Manager
instance, use the NewManager
function.
mng := jobqueue.NewManager()
Manager State
A Manager can be in one of two states:
-
ManagerStateStopped
: The manager is stopped. -
ManagerStateRunning
: The manager is running and actively scheduling jobs.
Configuration
You can configure the manager using various setter methods:
-
SetCronInstance(cronInstance *cron.Cron) *Manager
: Set a Cron instance for scheduled jobs. -
SetDB(db *gorm.DB) *Manager
: Set a Gorm DB instance for database storage.
mng.SetCronInstance(cronInstance).SetDB(db)
Managing Workers
You can add or remove workers to/from the manager using the following methods:
AddWorker(worker Worker) error
RemoveWorker(worker Worker) error
err := mng.AddWorker(worker)
err = mng.RemoveWorker(worker)
Starting and Stopping the Manager
The manager can be started or stopped using:
Start() error
Stop() error
err := mng.Start()
err = mng.Stop()
Scheduling Jobs
To schedule a job for execution, use the ScheduleJob
method.
err := mng.ScheduleJob(job, scheduler)
Canceling Job Schedules
To cancel a scheduled job, use CancelJobSchedule
.
err := mng.CancelJobSchedule(jobID)
Event Bus
To get the EventBus instance, use GetEventBus
.
eventBus := mng.GetEventBus()
Error Handling
Errors returned by the Manager methods should be handled appropriately to ensure smooth operation.
Tests
Run tests using:
go test ./...
Contributing
Please read CONTRIBUTING.md for the process for submitting pull requests.
License
This project is licensed under the AGPL-3.0 License - see the LICENSE.md file for details.
Contact
- schukai GmbH - schukai.de
- info@schukai.com