Skip to content

Commit 137da85

Browse files
javier-aliagaJoshVanLdapr-botcicoyle
authored
feat: Introduce new overwrite flag to jobs (dapr#8701)
* feat: Introcude new overwrite flag to jobs The new flags can be use to specify if the job can overwrite an existing one. The default value is `false` Signed-off-by: Javier Aliaga <[email protected]> * fix: Lint errors Signed-off-by: Javier Aliaga <[email protected]> * fix: Actors reminder overrides existing jobs when using scheduler as storage Signed-off-by: Javier Aliaga <[email protected]> * fix: Reviewers comments Move overwrite tests to its own files Signed-off-by: Javier Aliaga <[email protected]> * chore: Use grpc error code if job already exist Signed-off-by: Javier Aliaga <[email protected]> * chore: Add Copyright headers Signed-off-by: Javier Aliaga <[email protected]> * chore: Schedule job return correct http code Convert grc error code to http error code on http requests for schedule jobs Signed-off-by: Javier Aliaga <[email protected]> * fix: linting Signed-off-by: Javier Aliaga <[email protected]> * Update pkg/scheduler/server/api.go Co-authored-by: Josh van Leeuwen <[email protected]> Signed-off-by: Javier Aliaga <[email protected]> * Update pkg/scheduler/server/api.go Co-authored-by: Josh van Leeuwen <[email protected]> Signed-off-by: Javier Aliaga <[email protected]> * fix: Review comments Better assertions Import ordering Signed-off-by: Javier Aliaga <[email protected]> * chore: Add test and fix lint Signed-off-by: Javier Aliaga <[email protected]> --------- Signed-off-by: Javier Aliaga <[email protected]> Co-authored-by: Josh van Leeuwen <[email protected]> Co-authored-by: Dapr Bot <[email protected]> Co-authored-by: Cassie Coyle <[email protected]>
1 parent bbf2e48 commit 137da85

File tree

16 files changed

+983
-668
lines changed

16 files changed

+983
-668
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ test-integration-parallel: test-deps
404404
lint: check-linter
405405
$(GOLANGCI_LINT) run --build-tags=$(GOLANGCI_LINT_TAGS) --timeout=20m --max-same-issues 0 --max-issues-per-linter 0
406406

407+
.PHONY: lint-fix
408+
lint-fix: check-linter
409+
$(GOLANGCI_LINT) run --build-tags=$(GOLANGCI_LINT_TAGS) --timeout=20m --max-same-issues 0 --max-issues-per-linter 0 --fix
407410

408411
################################################################################
409412
# Target: check-linter #

dapr/proto/internals/v1/jobs.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ message JobHTTPRequest {
3030
optional string due_time = 4 [json_name = "dueTime"];
3131
optional string ttl = 5 [json_name = "ttl"];
3232
google.protobuf.Value data = 6 [json_name = "data"];
33+
optional bool overwrite = 7 [json_name = "overwrite"];
3334
}

dapr/proto/runtime/v1/dapr.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,9 @@ message Job {
12651265
// payload is the serialized job payload that will be sent to the recipient
12661266
// when the job is triggered.
12671267
google.protobuf.Any data = 6 [json_name = "data"];
1268+
1269+
// If true, allows this job to overwrite an existing job with the same name.
1270+
bool overwrite = 7 [json_name = "overwrite"];
12681271
}
12691272

12701273
// ScheduleJobRequest is the message to create/schedule the job.

dapr/proto/scheduler/v1/scheduler.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ message Job {
4646
// By default, the failure policy is FailurePolicyConstant with a 1s interval
4747
// and 3 maximum retries.
4848
optional FailurePolicy failure_policy = 6;
49+
50+
// If true, allows this job to overwrite an existing job with the same name.
51+
bool overwrite = 7;
4952
}
5053

5154
// TargetJob is the message used by the daprd sidecar to schedule a job

pkg/actors/internal/reminders/storage/scheduler/scheduler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func (s *scheduler) Create(ctx context.Context, reminder *api.CreateReminderRequ
120120
Ttl: ttl,
121121
Data: reminder.Data,
122122
FailurePolicy: failurePolicy,
123+
Overwrite: true,
123124
},
124125
Metadata: &schedulerv1pb.JobMetadata{
125126
AppId: s.appID,

pkg/api/errors/scheduler.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"net/http"
1818

1919
"google.golang.org/grpc/codes"
20+
"google.golang.org/grpc/status"
2021

2122
"github.com/dapr/dapr/pkg/messages/errorcodes"
2223
kiterrors "github.com/dapr/kit/errors"
24+
"github.com/dapr/kit/grpccodes"
2325
)
2426

2527
func SchedulerURLName(metadata map[string]string) error {
@@ -36,9 +38,16 @@ func SchedulerURLName(metadata map[string]string) error {
3638
}
3739

3840
func SchedulerScheduleJob(metadata map[string]string, err error) error {
41+
code := status.Code(err)
42+
if code == codes.Unknown {
43+
code = codes.Internal
44+
}
45+
46+
httpCode := grpccodes.HTTPStatusFromCode(code)
47+
3948
return kiterrors.NewBuilder(
40-
codes.Internal,
41-
http.StatusInternalServerError,
49+
code,
50+
httpCode,
4251
"failed to schedule job due to: "+err.Error(),
4352
"",
4453
string(errorcodes.SchedulerScheduleJob.Category),

pkg/api/universal/jobs.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ func (a *Universal) ScheduleJobAlpha1HTTP(ctx context.Context, job *internalsv1p
4545

4646
//nolint:protogetter
4747
return a.scheduleJob(ctx, &runtimev1pb.Job{
48-
Name: job.GetName(),
49-
Schedule: job.Schedule,
50-
Repeats: job.Repeats,
51-
DueTime: job.DueTime,
52-
Ttl: job.Ttl,
53-
Data: data,
48+
Name: job.GetName(),
49+
Schedule: job.Schedule,
50+
Repeats: job.Repeats,
51+
DueTime: job.DueTime,
52+
Ttl: job.Ttl,
53+
Data: data,
54+
Overwrite: job.GetOverwrite(),
5455
})
5556
}
5657

@@ -85,11 +86,12 @@ func (a *Universal) scheduleJob(ctx context.Context, job *runtimev1pb.Job) (*run
8586
},
8687
//nolint:protogetter
8788
Job: &schedulerv1pb.Job{
88-
Schedule: job.Schedule,
89-
Data: job.GetData(),
90-
Repeats: job.Repeats,
91-
DueTime: job.DueTime,
92-
Ttl: job.Ttl,
89+
Schedule: job.Schedule,
90+
Data: job.GetData(),
91+
Repeats: job.Repeats,
92+
DueTime: job.DueTime,
93+
Ttl: job.Ttl,
94+
Overwrite: job.GetOverwrite(),
9395
},
9496
}
9597

pkg/proto/internals/v1/jobs.pb.go

Lines changed: 26 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)