Skip to content

Commit fd01492

Browse files
authored
Better error in panic when accessing periodic jobs bundle in client that won't work (#938)
Related to #937. If an insert-only client is created and the `PeriodicJobs()` bundle is accessed, it'll panic on a nil pointer error. The error is appropriate because if the client will never work jobs, it can never be started, which means it can never win leader election, which means that adding periodic jobs to it will never have any effect, but the bad error message is definitely not ideal. Here, detect this condition and panic with an error that better explains the situation to the caller. Fixes #937.
1 parent f89fc22 commit fd01492

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
- Correct handling an explicit schema in the reindexer maintenance service. [PR #916](https://github.com/riverqueue/river/pull/916).
3232
- Return specific explanatory error when attempting to use `JobListParams.Metadata` with `JobListTx` on SQLite. [PR #924](https://github.com/riverqueue/river/pull/924).
3333
- The reindexer now skips work if artifacts from a failed reindex are present under the assumption that if they are, a new reindex build is likely to fail again. Context cancel timeout is increased from 15 seconds to 1 minute, allowing more time for reindexes to finish. Timeout becomes configurable with `Config.ReindexerTimeout`. [PR #935](https://github.com/riverqueue/river/pull/935).
34+
- Accessing `Client.PeriodicJobs()` on an insert-only client now panics with a more helpful explanatory error message rather than an unhelpful nil pointer panic. [PR #938](https://github.com/riverqueue/river/pull/938).
3435

3536
## [0.22.0] - 2025-05-10
3637

client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,20 @@ func (c *Client[TTx]) JobListTx(ctx context.Context, tx TTx, params *JobListPara
20852085
}
20862086

20872087
// PeriodicJobs returns the currently configured set of periodic jobs for the
2088-
// client, and can be used to add new ones or remove existing ones.
2089-
func (c *Client[TTx]) PeriodicJobs() *PeriodicJobBundle { return c.periodicJobs }
2088+
// client, and can be used to add new or remove existing ones.
2089+
//
2090+
// This function should only be invoked on clients capable of running perioidc
2091+
// jobs. Running periodic jobs requires that the client be electable as leader
2092+
// to run maintenance services, and being electable as leader requires that a
2093+
// client be started. To be startable, a client must have Queues and Workers
2094+
// configured. Invoking this function will panic if these conditions aren't met.
2095+
func (c *Client[TTx]) PeriodicJobs() *PeriodicJobBundle {
2096+
if !c.config.willExecuteJobs() {
2097+
panic("client Queues and Workers must be configured to modify periodic jobs (otherwise, they'll have no effect because a client not configured to work jobs can't be started)")
2098+
}
2099+
2100+
return c.periodicJobs
2101+
}
20902102

20912103
// Driver exposes the underlying pilot used by the client.
20922104
//

client_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4479,6 +4479,20 @@ func Test_Client_Maintenance(t *testing.T) {
44794479
}
44804480
})
44814481

4482+
t.Run("PeriodicJobsPanicIfClientNotConfiguredToExecuteJobs", func(t *testing.T) {
4483+
t.Parallel()
4484+
4485+
config := newTestConfig(t, "")
4486+
config.Queues = nil
4487+
config.Workers = nil
4488+
4489+
client := newTestClient(t, nil, config)
4490+
4491+
require.PanicsWithValue(t, "client Queues and Workers must be configured to modify periodic jobs (otherwise, they'll have no effect because a client not configured to work jobs can't be started)", func() {
4492+
client.PeriodicJobs()
4493+
})
4494+
})
4495+
44824496
t.Run("PeriodicJobEnqueuerUsesMiddleware", func(t *testing.T) {
44834497
t.Parallel()
44844498

0 commit comments

Comments
 (0)