diff --git a/README.md b/README.md index 64b02e6..d16af57 100644 --- a/README.md +++ b/README.md @@ -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) { diff --git a/backends/postgres/postgres_backend.go b/backends/postgres/postgres_backend.go index b6f396f..c52da3e 100644 --- a/backends/postgres/postgres_backend.go +++ b/backends/postgres/postgres_backend.go @@ -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 diff --git a/backends/postgres/postgres_backend_test.go b/backends/postgres/postgres_backend_test.go index e68444f..cce01a0 100644 --- a/backends/postgres/postgres_backend_test.go +++ b/backends/postgres/postgres_backend_test.go @@ -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) } diff --git a/config/config.go b/config/config.go index e31b1ad..c58d9fe 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/examples/add_future_postgres_job/main.go b/examples/add_future_postgres_job/main.go index 344e005..1e7f833 100644 --- a/examples/add_future_postgres_job/main.go +++ b/examples/add_future_postgres_job/main.go @@ -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 { diff --git a/examples/add_postgres_job/main.go b/examples/add_postgres_job/main.go index 308bfb9..5aa3af0 100644 --- a/examples/add_postgres_job/main.go +++ b/examples/add_postgres_job/main.go @@ -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 { diff --git a/examples/start_processing_jobs/main.go b/examples/start_processing_jobs/main.go index fea70cb..b192c7f 100644 --- a/examples/start_processing_jobs/main.go +++ b/examples/start_processing_jobs/main.go @@ -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) } diff --git a/neoq_test.go b/neoq_test.go index 43ff9f9..6c95322 100644 --- a/neoq_test.go +++ b/neoq_test.go @@ -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