Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move check on zero workers from client constructor to Start #88

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `NewClient` no longer errors if it was provided a workers bundle with zero workers. Instead, that check's been moved to `Client.Start` instead. This allows adding workers to a bundle that'd like to reference a River client by letting `AddWorker` be invoked after a client reference is available from `NewClient`. PR #87.
- `Stop` and `StopAndCancel` have been changed to respect the provided context argument. When that context is cancelled or times out, those methods will now immediately return with the context's error, even if the Client's shutdown has not yet completed. Apps may need to adjust their graceful shutdown logic to account for this. PR #79.

## [0.0.10] - 2023-11-26
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ func (c *Config) validate() error {
if c.Workers == nil && c.Queues != nil {
return errors.New("Workers must be set if Queues is set")
}
if c.Workers != nil && len(c.Workers.workersMap) < 1 {
return errors.New("at least one Worker must be added to the Workers bundle")
}

return nil
}
Expand Down Expand Up @@ -554,6 +551,9 @@ func (c *Client[TTx]) Start(ctx context.Context) error {
if !c.config.willExecuteJobs() {
return fmt.Errorf("client Queues and Workers must be configured for a client to start working")
}
if c.config.Workers != nil && len(c.config.Workers.workersMap) < 1 {
return errors.New("at least one Worker must be added to the Workers bundle")
}

// We use separate contexts for fetching and working to allow for a graceful
// stop. However, both inherit from the provided context so if it is
Expand Down
22 changes: 16 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,17 @@ func Test_Client_Start_Error(t *testing.T) {
require.EqualError(t, err, "client Queues and Workers must be configured for a client to start working")
})

t.Run("NoRegisteredWorkers", func(t *testing.T) {
t.Parallel()

config := newTestConfig(t, nil)
config.Workers = NewWorkers() // initialized, but empty

client := newTestClient(ctx, t, config)
err := client.Start(ctx)
require.EqualError(t, err, "at least one Worker must be added to the Workers bundle")
})

t.Run("DatabaseError", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2465,18 +2476,17 @@ func Test_NewClient_Validations(t *testing.T) {
},
},
{
name: "Workers cannot be empty if Queues is set",
name: "Workers can be empty", // but notably, not allowed to be empty if started
configFunc: func(config *Config) {
config.Workers = nil
config.Workers = NewWorkers()
},
wantErr: errors.New("Workers must be set if Queues is set"),
},
{
name: "Workers must contain at least one worker",
name: "Workers cannot be empty if Queues is set",
configFunc: func(config *Config) {
config.Workers = NewWorkers()
config.Workers = nil
},
wantErr: errors.New("at least one Worker must be added to the Workers bundle"),
wantErr: errors.New("Workers must be set if Queues is set"),
},
}

Expand Down
Loading