Skip to content

Commit

Permalink
Move check on zero workers from client constructor to Start
Browse files Browse the repository at this point in the history
This one's in pursuit of trying to solve #87, where it's difficult to
inject a River client into a worker because trying to initialize a
client with the workers bundle empty is an error, creating a chicken and
egg problem.

There's no real reason to disallow a zero worker bundle from the
constructor, and in fact a lot of our tests already add additional
workers after the client was originally initialized (although
`newTestClient` injects a default worker, which is why there's no
error). If it's a useful pattern for us, it's probably useful for other
users too.

Here, move the zero workers check from the constructor over to the
`Start` function instead. While it seems okay to initialize a client
without workers, starting it without any does seem like a potential
problem that we'd want to keep an eye out for.

Fixes #87.
  • Loading branch information
brandur committed Dec 2, 2023
1 parent 9dd20ec commit 015ee53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
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

0 comments on commit 015ee53

Please sign in to comment.