|
13 | 13 |
|
14 | 14 | package io.dapr.client; |
15 | 15 |
|
16 | | -import com.google.protobuf.Any; |
17 | 16 | import com.google.protobuf.ByteString; |
18 | 17 | import com.google.protobuf.Empty; |
19 | 18 | import io.dapr.client.domain.ActorMetadata; |
|
25 | 24 | import io.dapr.client.domain.BulkPublishResponse; |
26 | 25 | import io.dapr.client.domain.ComponentMetadata; |
27 | 26 | import io.dapr.client.domain.ConfigurationItem; |
28 | | -import io.dapr.client.domain.ConstantFailurePolicy; |
29 | 27 | import io.dapr.client.domain.ConversationRequest; |
30 | 28 | import io.dapr.client.domain.ConversationRequestAlpha2; |
31 | 29 | import io.dapr.client.domain.ConversationResponse; |
|
36 | 34 | import io.dapr.client.domain.DeleteJobRequest; |
37 | 35 | import io.dapr.client.domain.DeleteStateRequest; |
38 | 36 | import io.dapr.client.domain.DeleteVectorsRequestAlpha1; |
39 | | -import io.dapr.client.domain.DropFailurePolicy; |
40 | 37 | import io.dapr.client.domain.EncryptRequestAlpha1; |
41 | 38 | import io.dapr.client.domain.ExecuteStateTransactionRequest; |
42 | | -import io.dapr.client.domain.FailurePolicy; |
43 | 39 | import io.dapr.client.domain.GetBulkSecretRequest; |
44 | 40 | import io.dapr.client.domain.GetBulkStateRequest; |
45 | 41 | import io.dapr.client.domain.GetConfigurationRequest; |
|
54 | 50 | import io.dapr.client.domain.IndexAck; |
55 | 51 | import io.dapr.client.domain.InvokeBindingRequest; |
56 | 52 | import io.dapr.client.domain.InvokeMethodRequest; |
57 | | -import io.dapr.client.domain.JobSchedule; |
58 | 53 | import io.dapr.client.domain.LockRequest; |
59 | 54 | import io.dapr.client.domain.PublishEventRequest; |
60 | 55 | import io.dapr.client.domain.QueryStateRequest; |
|
83 | 78 | import io.dapr.internal.resiliency.RetryPolicy; |
84 | 79 | import io.dapr.internal.resiliency.TimeoutPolicy; |
85 | 80 | import io.dapr.serializer.DaprObjectSerializer; |
86 | | -import io.dapr.utils.FailurePolicyUtils; |
87 | 81 | import io.dapr.utils.TypeRef; |
88 | 82 | import io.dapr.v1.CommonProtos; |
89 | 83 | import io.dapr.v1.DaprBindingsProtos; |
90 | 84 | import io.dapr.v1.DaprConfigurationProtos; |
91 | 85 | import io.dapr.v1.DaprCryptoProtos; |
92 | 86 | import io.dapr.v1.DaprGrpc; |
93 | | -import io.dapr.v1.DaprJobsProtos; |
94 | 87 | import io.dapr.v1.DaprLockProtos; |
95 | 88 | import io.dapr.v1.DaprMetadataProtos; |
96 | 89 | import io.dapr.v1.DaprProtos; |
|
108 | 101 | import java.io.IOException; |
109 | 102 | import java.net.URI; |
110 | 103 | import java.time.Duration; |
111 | | -import java.time.Instant; |
112 | | -import java.time.ZoneOffset; |
113 | | -import java.time.format.DateTimeFormatter; |
114 | | -import java.time.temporal.ChronoUnit; |
115 | 104 | import java.util.ArrayList; |
116 | 105 | import java.util.Arrays; |
117 | 106 | import java.util.Collections; |
@@ -155,6 +144,8 @@ public class DaprClientImpl extends AbstractDaprClient { |
155 | 144 |
|
156 | 145 | private final DaprConversationClient conversationClient; |
157 | 146 |
|
| 147 | + private final DaprJobsClient jobsClient; |
| 148 | + |
158 | 149 | private final DaprBinaryFileClient binaryFileClient; |
159 | 150 |
|
160 | 151 | private final DaprPubSubClient pubSubClient; |
@@ -260,6 +251,7 @@ private DaprClientImpl( |
260 | 251 | this.grpcInterceptors = new DaprClientGrpcInterceptors(daprApiToken, timeoutPolicy); |
261 | 252 | this.grpcCalls = new DaprGrpcCalls(asyncStub, this.grpcInterceptors, retryPolicy); |
262 | 253 | this.conversationClient = new DaprConversationClient(this.grpcCalls, objectSerializer); |
| 254 | + this.jobsClient = new DaprJobsClient(this.grpcCalls); |
263 | 255 | this.binaryFileClient = new DaprBinaryFileClient(this.grpcCalls); |
264 | 256 | this.pubSubClient = new DaprPubSubClient(this.grpcCalls, objectSerializer); |
265 | 257 | this.stateClient = new DaprStateClient(this.grpcCalls, stateSerializer); |
@@ -925,211 +917,21 @@ public Mono<UnsubscribeConfigurationResponse> unsubscribeConfiguration(Unsubscri |
925 | 917 | * {@inheritDoc} |
926 | 918 | */ |
927 | 919 | public Mono<Void> scheduleJob(ScheduleJobRequest scheduleJobRequest) { |
928 | | - try { |
929 | | - validateScheduleJobRequest(scheduleJobRequest); |
930 | | - |
931 | | - DaprJobsProtos.Job.Builder jobBuilder = DaprJobsProtos.Job.newBuilder(); |
932 | | - jobBuilder.setName(scheduleJobRequest.getName()); |
933 | | - |
934 | | - DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") |
935 | | - .withZone(ZoneOffset.UTC); |
936 | | - |
937 | | - if (scheduleJobRequest.getData() != null) { |
938 | | - jobBuilder.setData(Any.newBuilder() |
939 | | - .setValue(ByteString.copyFrom(scheduleJobRequest.getData())).build()); |
940 | | - } |
941 | | - |
942 | | - if (scheduleJobRequest.getSchedule() != null) { |
943 | | - jobBuilder.setSchedule(scheduleJobRequest.getSchedule().getExpression()); |
944 | | - } |
945 | | - |
946 | | - if (scheduleJobRequest.getTtl() != null) { |
947 | | - jobBuilder.setTtl(iso8601Formatter.format(scheduleJobRequest.getTtl())); |
948 | | - } |
949 | | - |
950 | | - if (scheduleJobRequest.getRepeats() != null) { |
951 | | - jobBuilder.setRepeats(scheduleJobRequest.getRepeats()); |
952 | | - } |
953 | | - |
954 | | - if (scheduleJobRequest.getDueTime() != null) { |
955 | | - jobBuilder.setDueTime(iso8601Formatter.format(scheduleJobRequest.getDueTime())); |
956 | | - } |
957 | | - |
958 | | - if (scheduleJobRequest.getFailurePolicy() != null) { |
959 | | - jobBuilder.setFailurePolicy(FailurePolicyUtils.getJobFailurePolicy(scheduleJobRequest.getFailurePolicy())); |
960 | | - } |
961 | | - |
962 | | - DaprJobsProtos.ScheduleJobRequest request = DaprJobsProtos.ScheduleJobRequest.newBuilder() |
963 | | - .setOverwrite(scheduleJobRequest.getOverwrite()) |
964 | | - .setJob(jobBuilder.build()) |
965 | | - .build(); |
966 | | - |
967 | | - Mono<DaprJobsProtos.ScheduleJobResponse> scheduleJobResponseMono = |
968 | | - Mono.deferContextual(context -> |
969 | | - grpcCalls.<DaprJobsProtos.ScheduleJobResponse>createMono( |
970 | | - it -> grpcCalls.intercept(context).scheduleJob(request, it) |
971 | | - ).onErrorResume(throwable -> { |
972 | | - // The stable ScheduleJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1. |
973 | | - if (isUnimplemented(throwable)) { |
974 | | - return grpcCalls.<DaprJobsProtos.ScheduleJobResponse>createMono( |
975 | | - it -> grpcCalls.intercept(context).scheduleJobAlpha1(request, it)); |
976 | | - } |
977 | | - return Mono.error(throwable); |
978 | | - }) |
979 | | - ); |
980 | | - |
981 | | - return scheduleJobResponseMono.then(); |
982 | | - } catch (Exception ex) { |
983 | | - return DaprException.wrapMono(ex); |
984 | | - } |
| 920 | + return this.jobsClient.scheduleJob(scheduleJobRequest); |
985 | 921 | } |
986 | 922 |
|
987 | 923 | /** |
988 | 924 | * {@inheritDoc} |
989 | 925 | */ |
990 | 926 | public Mono<GetJobResponse> getJob(GetJobRequest getJobRequest) { |
991 | | - try { |
992 | | - validateGetJobRequest(getJobRequest); |
993 | | - |
994 | | - DaprJobsProtos.GetJobRequest request = DaprJobsProtos.GetJobRequest.newBuilder() |
995 | | - .setName(getJobRequest.getName()) |
996 | | - .build(); |
997 | | - |
998 | | - Mono<DaprJobsProtos.GetJobResponse> getJobResponseMono = |
999 | | - Mono.deferContextual(context -> |
1000 | | - grpcCalls.<DaprJobsProtos.GetJobResponse>createMono( |
1001 | | - it -> grpcCalls.intercept(context).getJob(request, it) |
1002 | | - ).onErrorResume(throwable -> { |
1003 | | - // The stable GetJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1. |
1004 | | - if (isUnimplemented(throwable)) { |
1005 | | - return grpcCalls.<DaprJobsProtos.GetJobResponse>createMono( |
1006 | | - it -> grpcCalls.intercept(context).getJobAlpha1(request, it)); |
1007 | | - } |
1008 | | - return Mono.error(throwable); |
1009 | | - }) |
1010 | | - ); |
1011 | | - |
1012 | | - return getJobResponseMono.map(response -> { |
1013 | | - DaprJobsProtos.Job job = response.getJob(); |
1014 | | - GetJobResponse getJobResponse = null; |
1015 | | - |
1016 | | - if (job.hasSchedule() && job.hasDueTime()) { |
1017 | | - getJobResponse = new GetJobResponse(job.getName(), JobSchedule.fromString(job.getSchedule())); |
1018 | | - getJobResponse.setDueTime(Instant.parse(job.getDueTime())); |
1019 | | - } else if (job.hasSchedule()) { |
1020 | | - getJobResponse = new GetJobResponse(job.getName(), JobSchedule.fromString(job.getSchedule())); |
1021 | | - } else { |
1022 | | - getJobResponse = new GetJobResponse(job.getName(), Instant.parse(job.getDueTime())); |
1023 | | - } |
1024 | | - |
1025 | | - if (job.hasFailurePolicy()) { |
1026 | | - getJobResponse.setFailurePolicy(getJobFailurePolicy(job.getFailurePolicy())); |
1027 | | - } |
1028 | | - |
1029 | | - return getJobResponse |
1030 | | - .setTtl(job.hasTtl() ? Instant.parse(job.getTtl()) : null) |
1031 | | - .setData(job.hasData() ? job.getData().getValue().toByteArray() : null) |
1032 | | - .setRepeat(job.hasRepeats() ? job.getRepeats() : null); |
1033 | | - }); |
1034 | | - } catch (Exception ex) { |
1035 | | - return DaprException.wrapMono(ex); |
1036 | | - } |
1037 | | - } |
1038 | | - |
1039 | | - private FailurePolicy getJobFailurePolicy(CommonProtos.JobFailurePolicy jobFailurePolicy) { |
1040 | | - if (jobFailurePolicy.hasDrop()) { |
1041 | | - return new DropFailurePolicy(); |
1042 | | - } |
1043 | | - |
1044 | | - CommonProtos.JobFailurePolicyConstant jobFailurePolicyConstant = jobFailurePolicy.getConstant(); |
1045 | | - if (jobFailurePolicyConstant.hasInterval() && jobFailurePolicyConstant.hasMaxRetries()) { |
1046 | | - return new ConstantFailurePolicy(jobFailurePolicyConstant.getMaxRetries()) |
1047 | | - .setDurationBetweenRetries(Duration.of(jobFailurePolicyConstant.getInterval().getNanos(), |
1048 | | - ChronoUnit.NANOS)); |
1049 | | - } |
1050 | | - |
1051 | | - if (jobFailurePolicyConstant.hasMaxRetries()) { |
1052 | | - return new ConstantFailurePolicy(jobFailurePolicyConstant.getMaxRetries()); |
1053 | | - } |
1054 | | - |
1055 | | - return new ConstantFailurePolicy( |
1056 | | - Duration.of(jobFailurePolicyConstant.getInterval().getNanos(), |
1057 | | - ChronoUnit.NANOS)); |
| 927 | + return this.jobsClient.getJob(getJobRequest); |
1058 | 928 | } |
1059 | 929 |
|
1060 | 930 | /** |
1061 | 931 | * {@inheritDoc} |
1062 | 932 | */ |
1063 | 933 | public Mono<Void> deleteJob(DeleteJobRequest deleteJobRequest) { |
1064 | | - try { |
1065 | | - validateDeleteJobRequest(deleteJobRequest); |
1066 | | - |
1067 | | - DaprJobsProtos.DeleteJobRequest request = DaprJobsProtos.DeleteJobRequest.newBuilder() |
1068 | | - .setName(deleteJobRequest.getName()) |
1069 | | - .build(); |
1070 | | - |
1071 | | - Mono<DaprJobsProtos.DeleteJobResponse> deleteJobResponseMono = |
1072 | | - Mono.deferContextual(context -> |
1073 | | - grpcCalls.<DaprJobsProtos.DeleteJobResponse>createMono( |
1074 | | - it -> grpcCalls.intercept(context).deleteJob(request, it) |
1075 | | - ).onErrorResume(throwable -> { |
1076 | | - // The stable DeleteJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1. |
1077 | | - if (isUnimplemented(throwable)) { |
1078 | | - return grpcCalls.<DaprJobsProtos.DeleteJobResponse>createMono( |
1079 | | - it -> grpcCalls.intercept(context).deleteJobAlpha1(request, it)); |
1080 | | - } |
1081 | | - return Mono.error(throwable); |
1082 | | - }) |
1083 | | - ); |
1084 | | - |
1085 | | - return deleteJobResponseMono.then(); |
1086 | | - } catch (Exception ex) { |
1087 | | - return DaprException.wrapMono(ex); |
1088 | | - } |
1089 | | - } |
1090 | | - |
1091 | | - /** |
1092 | | - * Returns true when a failed RPC indicates the sidecar has not implemented the stable RPC, |
1093 | | - * so the caller should retry against the deprecated Alpha1 RPC. The gRPC status is wrapped in a |
1094 | | - * {@link DaprException} (and an {@link java.util.concurrent.ExecutionException}), so we let gRPC |
1095 | | - * walk the cause chain to recover the original status code. |
1096 | | - */ |
1097 | | - private static boolean isUnimplemented(Throwable throwable) { |
1098 | | - return io.grpc.Status.fromThrowable(throwable).getCode() == io.grpc.Status.Code.UNIMPLEMENTED; |
1099 | | - } |
1100 | | - |
1101 | | - private void validateScheduleJobRequest(ScheduleJobRequest scheduleJobRequest) { |
1102 | | - if (scheduleJobRequest == null) { |
1103 | | - throw new IllegalArgumentException("scheduleJobRequest cannot be null"); |
1104 | | - } |
1105 | | - |
1106 | | - if (scheduleJobRequest.getName() == null || scheduleJobRequest.getName().isEmpty()) { |
1107 | | - throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
1108 | | - } |
1109 | | - |
1110 | | - if (scheduleJobRequest.getSchedule() == null && scheduleJobRequest.getDueTime() == null) { |
1111 | | - throw new IllegalArgumentException("At least one of schedule or dueTime must be provided"); |
1112 | | - } |
1113 | | - } |
1114 | | - |
1115 | | - private void validateGetJobRequest(GetJobRequest getJobRequest) { |
1116 | | - if (getJobRequest == null) { |
1117 | | - throw new IllegalArgumentException("getJobRequest cannot be null"); |
1118 | | - } |
1119 | | - |
1120 | | - if (getJobRequest.getName() == null || getJobRequest.getName().isEmpty()) { |
1121 | | - throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
1122 | | - } |
1123 | | - } |
1124 | | - |
1125 | | - private void validateDeleteJobRequest(DeleteJobRequest deleteJobRequest) { |
1126 | | - if (deleteJobRequest == null) { |
1127 | | - throw new IllegalArgumentException("deleteJobRequest cannot be null"); |
1128 | | - } |
1129 | | - |
1130 | | - if (deleteJobRequest.getName() == null || deleteJobRequest.getName().isEmpty()) { |
1131 | | - throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
1132 | | - } |
| 934 | + return this.jobsClient.deleteJob(deleteJobRequest); |
1133 | 935 | } |
1134 | 936 |
|
1135 | 937 | /** |
|
0 commit comments