Move config.WithConnectionString -> postgres.WithConnectionString (#43)

This commit is contained in:
Adriano Caloiaro 2023-04-01 18:03:55 -07:00 committed by GitHub
parent ba0032eaba
commit 219ea84d76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 25 deletions

View file

@ -78,7 +78,7 @@ nq.Enqueue(ctx, &jobs.Job{
ctx := context.Background()
nq, _ := neoq.New(ctx,
neoq.WithBackend(postgres.Backend),
config.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
postgres.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
)
nq.Start(ctx, "hello_world", handler.New(func(ctx context.Context) (err error) {

View file

@ -79,7 +79,7 @@ type PgBackend struct {
//
// If the database does not yet exist, Neoq will attempt to create the database and related tables by default.
//
// Backend requires that one of the [config.ConfigOption] is [config.WithConnectionString]
// Backend requires that one of the [config.ConfigOption] is [WithConnectionString]
//
// Connection strings may be a URL or DSN-style connection strings. The connection string supports multiple
// options detailed below.
@ -158,6 +158,13 @@ func Backend(ctx context.Context, opts ...config.Option) (pb types.Backend, err
return pb, nil
}
// WithConnectionString configures neoq postgres backend to use the specified connection string when connecting to a backend
func WithConnectionString(connectionString string) config.Option {
return func(c *config.Config) {
c.ConnectionString = connectionString
}
}
// WithTransactionTimeout sets the time that PgBackend's transactions may be idle before its underlying connection is
// closed
// The timeout is the number of milliseconds that a transaction may sit idle before postgres terminates the

View file

@ -10,7 +10,6 @@ import (
"github.com/acaloiaro/neoq"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/handler"
"github.com/acaloiaro/neoq/internal"
"github.com/acaloiaro/neoq/jobs"
@ -34,7 +33,7 @@ func TestBasicJobProcessing(t *testing.T) {
}
ctx := context.TODO()
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), config.WithConnectionString(connString))
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
if err != nil {
t.Fatal(err)
}
@ -87,7 +86,7 @@ func TestBasicJobMultipleQueue(t *testing.T) {
}
ctx := context.TODO()
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), config.WithConnectionString(connString))
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
if err != nil {
t.Fatal(err)
}
@ -163,7 +162,7 @@ func TestCron(t *testing.T) {
}
ctx := context.TODO()
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), config.WithConnectionString(connString))
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString(connString))
if err != nil {
t.Fatal(err)
}

View file

@ -13,9 +13,8 @@ const (
// schdule the job for execution.
// E.g. right now is 16:00 and a job's RunAfter is 16:30 of the same date. This job will get a dedicated goroutine to
// wait until the job's RunAfter, scheduling the job to be run exactly at RunAfter
DefaultFutureJobWindow = 30 * time.Second
DefaultJobCheckInterval = 5 * time.Second
DefaultTransactionTimeout = time.Minute
DefaultFutureJobWindow = 30 * time.Second
DefaultJobCheckInterval = 5 * time.Second
)
type Config struct {
@ -38,12 +37,5 @@ func New() *Config {
}
}
// WithConnectionString configures neoq to use the specified connection string when connecting to a backend
func WithConnectionString(connectionString string) Option {
return func(c *Config) {
c.ConnectionString = connectionString
}
}
// BackendInitializer is a function that initializes a backend
type BackendInitializer func(ctx context.Context, opts ...Option) (backend types.Backend, err error)

View file

@ -7,7 +7,6 @@ import (
"github.com/acaloiaro/neoq"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/jobs"
)
@ -15,8 +14,8 @@ func main() {
const queue = "foobar"
ctx := context.Background()
nq, err := neoq.New(ctx,
config.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
neoq.WithBackend(postgres.Backend),
postgres.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
postgres.WithTransactionTimeout(1000), // nolint: mnd, gomnd
)
if err != nil {

View file

@ -7,7 +7,6 @@ import (
"github.com/acaloiaro/neoq"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/handler"
"github.com/acaloiaro/neoq/jobs"
)
@ -17,7 +16,7 @@ func main() {
const queue = "foobar"
ctx := context.Background()
nq, err := neoq.New(ctx,
config.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
postgres.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"),
neoq.WithBackend(postgres.Backend),
)
if err != nil {

View file

@ -6,7 +6,6 @@ import (
"github.com/acaloiaro/neoq"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/handler"
"github.com/acaloiaro/neoq/jobs"
)
@ -16,7 +15,7 @@ func main() {
const queue = "foobar"
ctx := context.Background()
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), config.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"))
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString("postgres://postgres:postgres@127.0.0.1:5432/neoq"))
if err != nil {
log.Fatalf("error initializing postgres backend: %v", err)
}

View file

@ -12,7 +12,6 @@ import (
"github.com/acaloiaro/neoq/backends/memory"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/handler"
"github.com/acaloiaro/neoq/jobs"
"github.com/acaloiaro/neoq/testutils"
@ -43,7 +42,7 @@ func ExampleNew_postgres() {
return
}
nq, err := New(ctx, WithBackend(postgres.Backend), config.WithConnectionString(pgURL))
nq, err := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
if err != nil {
fmt.Println("neoq's postgres backend failed to initialize:", err)
return
@ -76,7 +75,7 @@ func ExampleWithBackend_postgres() {
return
}
nq, err := New(ctx, WithBackend(postgres.Backend), config.WithConnectionString(pgURL))
nq, err := New(ctx, WithBackend(postgres.Backend), postgres.WithConnectionString(pgURL))
if err != nil {
fmt.Println("initializing a new Neoq with no params should not return an error:", err)
return