Skip to content
Snippets Groups Projects
Select Git revision
  • 7fe47833df16a7189dc161378920f64a4a01b8c2
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

job-queues

  • Clone with SSH
  • Clone with HTTPS
  • 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