Skip to content

Exposes FailurePolicy to the Jobs api #737

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

Merged
merged 14 commits into from
Jul 14, 2025

Conversation

acroca
Copy link
Contributor

@acroca acroca commented Jun 18, 2025

Description

dapr/dapr#8785 has been merged into master. That PR exposes FailurePolicy field in the Jobs API

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

@acroca acroca requested review from a team as code owners June 18, 2025 14:47
Copy link
Contributor

@cicoyle cicoyle left a comment

Choose a reason for hiding this comment

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

Thanks for the name change to jobs for consistency across dapr repos - I've been wanting to update that for a while now 🙌🏻

client/jobs.go Outdated
Comment on lines 75 to 80
Name string
Schedule string // Optional
Repeats uint32 // Optional
DueTime string // Optional
TTL string // Optional
Data *anypb.Any
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional fields should be pointers.

client/jobs.go Outdated
Comment on lines 92 to 109
if job.Schedule != "" {
jobRequest.Schedule = &job.Schedule
}

if job.Repeats != 0 {
jobRequest.Repeats = &job.Repeats
}

if job.DueTime != "" {
jobRequest.DueTime = &job.DueTime
}

if job.TTL != "" {
jobRequest.Ttl = &job.TTL
}

if job.FailurePolicy != nil {
jobRequest.FailurePolicy = job.FailurePolicy.GetPBFailurePolicy()
Copy link
Contributor

Choose a reason for hiding this comment

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

Assigned pointers to pointers. We shouldn't empty string match.

client/jobs.go Outdated
resp, err := c.protoClient.GetJobAlpha1(ctx, &runtimepb.GetJobRequest{
Name: name,
})
log.Println(resp)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove.

client/jobs.go Outdated
Comment on lines 37 to 50
func (f *JobFailurePolicyConstant) GetPBFailurePolicy() *commonpb.JobFailurePolicy {
policy := &commonpb.JobFailurePolicy{
Policy: &commonpb.JobFailurePolicy_Constant{
Constant: &commonpb.JobFailurePolicyConstant{},
},
}
if f.maxRetries != nil {
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.MaxRetries = f.maxRetries
}
if f.interval != nil {
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.Interval = &durationpb.Duration{Seconds: int64(f.interval.Seconds())}
}
return policy
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why and what is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JobFailurePolicyConstant is the exposed type the SDK users can use to assign FailurePolicies. This function then converts this exposed type into the corresponding policy from the commonpb package.

client/jobs.go Outdated
Comment on lines 37 to 48
func (f *JobFailurePolicyConstant) GetPBFailurePolicy() *commonpb.JobFailurePolicy {
policy := &commonpb.JobFailurePolicy{
Policy: &commonpb.JobFailurePolicy_Constant{
Constant: &commonpb.JobFailurePolicyConstant{},
},
}
if f.maxRetries != nil {
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.MaxRetries = f.maxRetries
}
if f.interval != nil {
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.Interval = &durationpb.Duration{Seconds: int64(f.interval.Seconds())}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this not be built in a better way to avoid type casting the proto pointer?

Also:
- Refactor how exposed type is converted to proto type
- Remove unnecesary log
- Input validation for job api calls

Signed-off-by: Albert Callarisa <[email protected]>
@acroca acroca force-pushed the expose-jobs-failure-policy branch from ae9a14e to 88a5872 Compare June 19, 2025 16:22
examples/go.sum Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Examples folder needs a go mod tidy as v1.15.5 is still vendored it seems

dueTime = "10s"
repeats uint32 = 4
ttl = "10s"
maxRetries = uint32(4)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
maxRetries = uint32(4)
maxRetries uint32 = 4

client/jobs.go Outdated
}
}

func NewFailurePolicyConstant(maxRetries *uint32, interval *time.Duration) FailurePolicy {
Copy link
Member

Choose a reason for hiding this comment

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

This function signature doesn't appear idiomatic to me... having to pass an address of an unsigned in and again the address of a duration
I would have liked to see the values passed directly e.g. NewFailurePolicyConstant(4, time.Second * 5)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll actually remove those 'constructor' functions, as they are now unnecessary (those types were not exposed previously). I think it'll cleaner just to provide the appropriate struct directly, like the following:

&Job{
	Name:     "name",
	Schedule: &schedule,
	Repeats:  &repeats,
	DueTime:  &dueTime,
	TTL:      &ttl,
	Data:     nil,
	FailurePolicy: &JobFailurePolicyConstant{
		maxRetries: &maxRetries,
		interval:   &interval,
	},
}

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I missed this! I posted #737 (comment)

I think the functional options pattern is more idiomatic whether applied directly on the Job or as an optional overload to the ScheduleJobAlpha1 method

Comment on lines 52 to 62
maxRetries := uint32(4)
interval := time.Second * 3
job := daprc.Job{
Name: "prod-db-backup",
Schedule: "@every 1s",
Repeats: 10,
Data: &anypb.Any{
Value: jobData,
},
FailurePolicy: daprc.NewFailurePolicyConstant(&maxRetries, &interval),
}
Copy link
Member

Choose a reason for hiding this comment

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

I think from this example it's clear that using the addresses of an int or duration adds unnecessary faff in my opinion

acroca added 5 commits June 20, 2025 09:35
Signed-off-by: Albert Callarisa <[email protected]>
Signed-off-by: Albert Callarisa <[email protected]>
We can pass the right types directly when building jobs, there's no need for a constructor. The constructor was used previously when the types were private, but now they are exposed so the constructor is unnecessary.

Signed-off-by: Albert Callarisa <[email protected]>
Copy link
Member

@mikeee mikeee left a comment

Choose a reason for hiding this comment

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

Looking good, would be good to see some test assertions for those functional options just to cover any future upstream changes for this Alpha api

Signed-off-by: Albert Callarisa <[email protected]>
@acroca acroca requested a review from mikeee June 30, 2025 06:14
@acroca
Copy link
Contributor Author

acroca commented Jul 4, 2025

@mikeee What do you think now?

Copy link

codecov bot commented Jul 8, 2025

Codecov Report

Attention: Patch coverage is 67.62590% with 45 lines in your changes missing coverage. Please review.

Project coverage is 57.18%. Comparing base (6c59092) to head (62f9b56).
Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
client/jobs.go 67.62% 34 Missing and 11 partials ⚠️

❗ There is a different number of reports uploaded between BASE (6c59092) and HEAD (62f9b56). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (6c59092) HEAD (62f9b56)
4 1
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #737      +/-   ##
==========================================
- Coverage   62.52%   57.18%   -5.35%     
==========================================
  Files          56       58       +2     
  Lines        4139     4428     +289     
==========================================
- Hits         2588     2532      -56     
- Misses       1425     1761     +336     
- Partials      126      135       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@mikeee mikeee left a comment

Choose a reason for hiding this comment

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

@acroca Looking good, I think the linter isn't happy with the way you're accessing the proto fields but aside from that it looks fine to me.

Could you please address two things also:

acroca added 3 commits July 8, 2025 12:50
Signed-off-by: Albert Callarisa <[email protected]>
Signed-off-by: Albert Callarisa <[email protected]>
Signed-off-by: Albert Callarisa <[email protected]>
@acroca acroca requested a review from mikeee July 8, 2025 11:08
acroca added 2 commits July 8, 2025 14:38
Signed-off-by: Albert Callarisa <[email protected]>
Signed-off-by: Albert Callarisa <[email protected]>
@acroca
Copy link
Contributor Author

acroca commented Jul 11, 2025

@mikeee better now? The failing test seems to be github related, re-running it should work (works on my machine ™)

Copy link
Member

@mikeee mikeee left a comment

Choose a reason for hiding this comment

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

Lgtm- the tests are flaky when initialising dapr.

@acroca
Copy link
Contributor Author

acroca commented Jul 14, 2025

Cool, what's left to merge this PR?

@yaron2 yaron2 merged commit 0c36331 into dapr:main Jul 14, 2025
31 of 33 checks passed
@acroca acroca deleted the expose-jobs-failure-policy branch July 14, 2025 13:56
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.

5 participants