Skip to content

Better error in panic when accessing periodic jobs bundle in client that won't work#938

Merged
brandur merged 1 commit into
masterfrom
brandur-better-periodic-job-error
May 30, 2025
Merged

Better error in panic when accessing periodic jobs bundle in client that won't work#938
brandur merged 1 commit into
masterfrom
brandur-better-periodic-job-error

Conversation

@brandur

@brandur brandur commented May 29, 2025

Copy link
Copy Markdown
Contributor

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.

@brandur brandur force-pushed the brandur-better-periodic-job-error branch from 6b66ce2 to ce424e1 Compare May 29, 2025 05:26
@brandur brandur requested a review from bgentry May 29, 2025 05:30
Comment thread client.go
Comment on lines 2087 to +2095
// PeriodicJobs returns the currently configured set of periodic jobs for the
// client, and can be used to add new ones or remove existing ones.
func (c *Client[TTx]) PeriodicJobs() *PeriodicJobBundle { return c.periodicJobs }
func (c *Client[TTx]) PeriodicJobs() *PeriodicJobBundle {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to add a note to the doc string about this behavior too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good point. Just added a new paragraph:

// This function should only be invoked on clients capable of running perioidc
// jobs. Running periodic jobs requires that the client be electable as leader
// to run maintenance services, and being electable as leader requires that a
// client be started. To be startable, a client must have Queues and Workers
// configured. Invoking this function will panic if these conditions aren't met.

…hat won't work

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.
@brandur brandur force-pushed the brandur-better-periodic-job-error branch from ce424e1 to 94d5e0b Compare May 30, 2025 06:20
@brandur brandur merged commit fd01492 into master May 30, 2025
10 checks passed
@brandur brandur deleted the brandur-better-periodic-job-error branch May 30, 2025 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to insert a periodic job from external service

2 participants