# 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+

```bash
go get -u
```

### Installation

```bash
git clone https://github.com/yourusername/GoJobQueue.git
cd GoJobQueue
go build

```

## Usage

Import the package and create a new queue.

```bash
goCopy codeimport "github.com/yourusername/GoJobQueue"

queue := GoJobQueue.New()

```

Adding a job:

```bash
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.

```go
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.

```go
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`

```go
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`

```go
err := mng.Start()
err = mng.Stop()

```

#### Scheduling Jobs

To schedule a job for execution, use the `ScheduleJob` method.

```go
err := mng.ScheduleJob(job, scheduler)

```

#### Canceling Job Schedules

To cancel a scheduled job, use `CancelJobSchedule`.

```go
err := mng.CancelJobSchedule(jobID)

```

#### Event Bus

To get the EventBus instance, use `GetEventBus`.

```go
eventBus := mng.GetEventBus()

```

### Error Handling

Errors returned by the Manager methods should be handled appropriately to ensure smooth operation.

## Tests

Run tests using:

```bash
go test ./...
```

## Contributing

Please read [CONTRIBUTING.md](https://chat.openai.com/c/93eb4e0d-55a7-41a8-81a4-6e40c9d44dc2CONTRIBUTING.md) for the process for submitting pull requests.

## License

This project is licensed under the AGPL-3.0 License - see the [LICENSE.md](LICENSE.md) file for details.

## Contact

-   schukai GmbH - [schukai.de](https://www.schukai.com)
-   [info@schukai.com](mailto:info@schukai.com)