2023-02-13 20:24:49 +00:00
# Neoq
2023-02-22 15:39:59 +00:00
Background job processing for Go
2023-02-18 17:45:17 +00:00
[](https://pkg.go.dev/github.com/acaloiaro/neoq) [](https://app.gitter.im/#/room/#neoq:gitter.im)
2023-02-13 20:24:49 +00:00
# Installation
`go get github.com/acaloiaro/neoq`
# About
2023-02-22 15:39:59 +00:00
Neoq is a background job framework for Go applications. Its purpose is to minimize the infrastructure necessary to run production applications. It does so by implementing queue durability with modular backends.
2023-02-13 20:24:49 +00:00
2023-02-22 15:39:59 +00:00
This allows application to use the same type of data store for both application data and backround job processing. At the moment an in-memory and Postgres backends are provided. However, the goal is to have backends for every major datastore: Postgres, Redis, MySQL, etc.
2023-02-13 20:24:49 +00:00
Neoq does not aim to be the _fastest_ background job processor. It aims to be _fast_ , _reliable_ , and demand a minimal infrastructure footprint.
2023-02-22 15:39:59 +00:00
# What it does
2023-02-13 20:24:49 +00:00
2023-02-22 15:39:59 +00:00
- **Background job Processing**: Neoq has an in-memory and Postgres backend out of the box. Users may supply their own without changing neoq directly.
2023-02-13 20:24:49 +00:00
- **Retries**: Jobs may be retried a configurable number of times with exponential backoff and jitter to prevent thundering herds
2023-02-17 19:11:55 +00:00
- **Job uniqueness**: jobs are fingerprinted based on their payload and status to prevent job duplication (multiple unprocessed jobs with the same payload cannot be queued)
2023-02-13 20:24:49 +00:00
- **Deadlines**: Queue handlers can be configured with per-job time deadlines with millisecond accuracy
- **Configurable transaction idle time**: Don't let your background worker transactions run away with db resources. By default, worker transactions may idle for 60 seconds.
2023-02-17 19:11:55 +00:00
- **Periodic Jobs**: Jobs can be scheduled periodically using standard cron syntax
2023-02-13 20:24:49 +00:00
- **Future Jobs**: Jobs can be scheduled either for the future or immediate execution
- **Concurrency**: Concurrency is configurable for every queue
# Getting Started
Getting started is as simple as declaring queue handlers and adding jobs.
Additional documentation can be found in the wiki: https://github.com/acaloiaro/neoq/wiki
Error handling in this section is excluded for simplicity.
2023-02-14 00:18:13 +00:00
## Add queue handlers
2023-02-13 20:24:49 +00:00
2023-02-22 15:39:59 +00:00
Queue handlers listen for Jobs on queues. Jobs may consist of any payload that is JSON-serializable.
2023-02-13 20:24:49 +00:00
Queue Handlers are simple Go functions that accept a `Context` parameter.
2023-02-25 03:33:38 +00:00
**Example**: Add a listener on the `hello_world` queue using the default in-memory backend
2023-02-13 20:24:49 +00:00
```go
2023-02-22 15:39:59 +00:00
ctx := context.Background()
nq, _ := neoq.New(ctx)
2023-02-25 03:33:38 +00:00
nq.Start(ctx, "hello_world", handler.New(func(ctx context.Context) (err error) {
2023-04-02 00:54:05 +00:00
j, _ := jobs.FromContext(ctx)
2023-02-13 20:24:49 +00:00
log.Println("got job id:", j.ID, "messsage:", j.Payload["message"])
return
}))
```
2023-02-14 00:18:13 +00:00
## Enqueue jobs
2023-02-13 20:24:49 +00:00
2023-02-25 03:33:38 +00:00
Enqueuing jobs adds jobs to the specified queue to be processed asynchronously.
**Example**: Add a "Hello World" job to the `hello_world` queue using the default in-memory backend.
2023-02-13 20:24:49 +00:00
```go
2023-02-22 15:39:59 +00:00
ctx := context.Background()
nq, _ := neoq.New(ctx)
2023-03-01 18:08:05 +00:00
nq.Enqueue(ctx, & jobs.Job{
2023-02-13 20:24:49 +00:00
Queue: "hello_world",
Payload: map[string]interface{}{
"message": "hello world",
},
})
```
2023-02-25 03:33:38 +00:00
## Postgres
**Example**: Process jobs on the "hello_world" queue and add a job to it using the postgres backend
```go
ctx := context.Background()
nq, _ := neoq.New(ctx,
neoq.WithBackend(postgres.Backend),
2023-04-02 01:03:55 +00:00
postgres.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
2023-02-25 03:33:38 +00:00
)
nq.Start(ctx, "hello_world", handler.New(func(ctx context.Context) (err error) {
2023-04-02 00:54:05 +00:00
j, _ := jobs.FromContext(ctx)
2023-02-25 03:33:38 +00:00
log.Println("got job id:", j.ID, "messsage:", j.Payload["message"])
return
}))
nq.Enqueue(ctx, & jobs.Job{
Queue: "hello_world",
Payload: map[string]interface{}{
"message": "hello world",
},
})
```
2023-02-13 23:40:20 +00:00
# Example Code
Additional example integration code can be found at https://github.com/acaloiaro/neoq/tree/main/examples
# Status
This project is currently in alpha. Future releases may change the API. It currently leaks some resources. It can handle unimportant workloads.