Skip to content

Commit

Permalink
Move config.WithConnectionString -> postgres.WithConnectionString (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
acaloiaro authored Apr 2, 2023
1 parent ba0032e commit 219ea84
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ nq.Enqueue(ctx, &jobs.Job{
ctx := context.Background()
nq, _ := neoq.New(ctx,
neoq.WithBackend(postgres.Backend),
config.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
postgres.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
)

nq.Start(ctx, "hello_world", handler.New(func(ctx context.Context) (err error) {
Expand Down
9 changes: 8 additions & 1 deletion backends/postgres/postgres_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions backends/postgres/postgres_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 2 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
3 changes: 1 addition & 2 deletions examples/add_future_postgres_job/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (

"github.com/acaloiaro/neoq"
"github.com/acaloiaro/neoq/backends/postgres"
"github.com/acaloiaro/neoq/config"
"github.com/acaloiaro/neoq/jobs"
)

func main() {
const queue = "foobar"
ctx := context.Background()
nq, err := neoq.New(ctx,
config.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
neoq.WithBackend(postgres.Backend),
postgres.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
postgres.WithTransactionTimeout(1000), // nolint: mnd, gomnd
)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions examples/add_postgres_job/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -17,7 +16,7 @@ func main() {
const queue = "foobar"
ctx := context.Background()
nq, err := neoq.New(ctx,
config.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
postgres.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"),
neoq.WithBackend(postgres.Backend),
)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions examples/start_processing_jobs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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:[email protected]:5432/neoq"))
nq, err := neoq.New(ctx, neoq.WithBackend(postgres.Backend), postgres.WithConnectionString("postgres://postgres:[email protected]:5432/neoq"))
if err != nil {
log.Fatalf("error initializing postgres backend: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions neoq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 219ea84

Please sign in to comment.