|
| 1 | +package io.dapr.jobs.client; |
| 2 | + |
| 3 | +import com.google.protobuf.Any; |
| 4 | +import com.google.protobuf.ByteString; |
| 5 | +import io.dapr.client.resiliency.ResiliencyOptions; |
| 6 | +import io.dapr.config.Properties; |
| 7 | +import io.dapr.exceptions.DaprException; |
| 8 | +import io.dapr.internal.exceptions.DaprHttpException; |
| 9 | +import io.dapr.internal.grpc.interceptors.DaprTimeoutInterceptor; |
| 10 | +import io.dapr.internal.grpc.interceptors.DaprTracingInterceptor; |
| 11 | +import io.dapr.internal.resiliency.RetryPolicy; |
| 12 | +import io.dapr.internal.resiliency.TimeoutPolicy; |
| 13 | +import io.dapr.utils.NetworkUtils; |
| 14 | +import io.dapr.v1.DaprGrpc; |
| 15 | +import io.dapr.v1.DaprProtos; |
| 16 | +import io.grpc.ManagedChannel; |
| 17 | +import io.grpc.stub.StreamObserver; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | +import reactor.core.publisher.MonoSink; |
| 20 | +import reactor.util.context.ContextView; |
| 21 | + |
| 22 | +import java.time.OffsetDateTime; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +public class DaprJobsClient implements AutoCloseable { |
| 26 | + |
| 27 | + /** |
| 28 | + * Stub that has the method to call the conversation apis. |
| 29 | + */ |
| 30 | + private final DaprGrpc.DaprStub asyncStub; |
| 31 | + |
| 32 | + /** |
| 33 | + * The retry policy. |
| 34 | + */ |
| 35 | + private final RetryPolicy retryPolicy; |
| 36 | + |
| 37 | + /** |
| 38 | + * The timeout policy. |
| 39 | + */ |
| 40 | + private final TimeoutPolicy timeoutPolicy; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor to create conversation client. |
| 44 | + */ |
| 45 | + public DaprJobsClient() { |
| 46 | + this(DaprGrpc.newStub(NetworkUtils.buildGrpcManagedChannel(new Properties())), null); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor. |
| 51 | + * |
| 52 | + * @param properties with client configuration options. |
| 53 | + * @param resiliencyOptions retry options. |
| 54 | + */ |
| 55 | + public DaprJobsClient( |
| 56 | + Properties properties, |
| 57 | + ResiliencyOptions resiliencyOptions) { |
| 58 | + this(DaprGrpc.newStub(NetworkUtils.buildGrpcManagedChannel(properties)), resiliencyOptions); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * ConversationClient constructor. |
| 63 | + * |
| 64 | + * @param resiliencyOptions timeout and retry policies. |
| 65 | + */ |
| 66 | + protected DaprJobsClient( |
| 67 | + DaprGrpc.DaprStub asyncStub, |
| 68 | + ResiliencyOptions resiliencyOptions) { |
| 69 | + this.asyncStub = asyncStub; |
| 70 | + this.retryPolicy = new RetryPolicy(resiliencyOptions == null ? null : resiliencyOptions.getMaxRetries()); |
| 71 | + this.timeoutPolicy = new TimeoutPolicy(resiliencyOptions == null ? null : resiliencyOptions.getTimeout()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Schedules a job using the provided job request details. |
| 76 | + * |
| 77 | + * @param createJobRequest The request containing the details of the job to schedule. |
| 78 | + * Must include a name and optional schedule, data, and other related properties. |
| 79 | + * @return A {@link Mono} that completes when the job scheduling operation is successful or raises an error. |
| 80 | + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. |
| 81 | + */ |
| 82 | + public Mono<Void> scheduleJob(ScheduleJobRequest createJobRequest) { |
| 83 | + try { |
| 84 | + if (createJobRequest == null) { |
| 85 | + throw new IllegalArgumentException("scheduleJobRequest cannot be null"); |
| 86 | + } |
| 87 | + |
| 88 | + if (createJobRequest.getName() == null || createJobRequest.getName().isEmpty()) { |
| 89 | + throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
| 90 | + } |
| 91 | + |
| 92 | + if (createJobRequest.getSchedule() == null && createJobRequest.getDueTime() == null) { |
| 93 | + throw new IllegalArgumentException("At least one of schedule or dueTime must be provided"); |
| 94 | + } |
| 95 | + |
| 96 | + DaprProtos.Job.Builder scheduleJobRequestBuilder = DaprProtos.Job.newBuilder(); |
| 97 | + scheduleJobRequestBuilder.setName(createJobRequest.getName()); |
| 98 | + |
| 99 | + if (createJobRequest.getData() != null) { |
| 100 | + scheduleJobRequestBuilder.setData(Any.newBuilder() |
| 101 | + .setValue(ByteString.copyFrom(createJobRequest.getData())).build()); |
| 102 | + } |
| 103 | + |
| 104 | + if (createJobRequest.getSchedule() != null) { |
| 105 | + scheduleJobRequestBuilder.setSchedule(createJobRequest.getSchedule().getExpression()); |
| 106 | + } |
| 107 | + |
| 108 | + if (createJobRequest.getTtl() != null) { |
| 109 | + scheduleJobRequestBuilder.setTtl(createJobRequest.getTtl().toString()); |
| 110 | + } |
| 111 | + |
| 112 | + if (createJobRequest.getRepeats() != null) { |
| 113 | + scheduleJobRequestBuilder.setRepeats(createJobRequest.getRepeats()); |
| 114 | + } |
| 115 | + |
| 116 | + if (createJobRequest.getDueTime() != null) { |
| 117 | + scheduleJobRequestBuilder.setDueTime(createJobRequest.getDueTime().toString()); |
| 118 | + } |
| 119 | + |
| 120 | + DaprProtos.ScheduleJobRequest scheduleJobRequest = DaprProtos.ScheduleJobRequest.newBuilder() |
| 121 | + .setJob(scheduleJobRequestBuilder.build()).build(); |
| 122 | + |
| 123 | + Mono<DaprProtos.ScheduleJobResponse> scheduleJobResponseMono = |
| 124 | + Mono.deferContextual(context -> this.createMono( |
| 125 | + it -> intercept(context, asyncStub) |
| 126 | + .scheduleJobAlpha1(scheduleJobRequest, it) |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + return scheduleJobResponseMono.then(); |
| 131 | + } catch (Exception ex) { |
| 132 | + return DaprException.wrapMono(ex); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Retrieves details of a specific job. |
| 138 | + * |
| 139 | + * @param getJobRequest The request containing the job name for which the details are to be fetched. |
| 140 | + * The name property is mandatory. |
| 141 | + * @return A {@link Mono} that emits the {@link GetJobResponse} containing job details or raises an |
| 142 | + * error if the job is not found. |
| 143 | + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. |
| 144 | + */ |
| 145 | + |
| 146 | + public Mono<GetJobResponse> getJob(GetJobRequest getJobRequest) { |
| 147 | + try { |
| 148 | + if (getJobRequest == null) { |
| 149 | + throw new IllegalArgumentException("getJobRequest cannot be null"); |
| 150 | + } |
| 151 | + |
| 152 | + if (getJobRequest.getName() == null || getJobRequest.getName().isEmpty()) { |
| 153 | + throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
| 154 | + } |
| 155 | + |
| 156 | + Mono<DaprProtos.GetJobResponse> getJobResponseMono = |
| 157 | + Mono.deferContextual(context -> this.createMono( |
| 158 | + it -> intercept(context, asyncStub) |
| 159 | + .getJobAlpha1(DaprProtos.GetJobRequest.newBuilder() |
| 160 | + .setName(getJobRequest.getName()).build(), it) |
| 161 | + ) |
| 162 | + ); |
| 163 | + |
| 164 | + return getJobResponseMono.map(getJobResponse -> { |
| 165 | + DaprProtos.Job job = getJobResponse.getJob(); |
| 166 | + return GetJobResponse.builder() |
| 167 | + .setName(job.getName()) |
| 168 | + .setTtl(job.hasTtl() ? OffsetDateTime.parse(job.getTtl()) : null) |
| 169 | + .setData(job.hasData() ? job.getData().getValue().toByteArray() : null) |
| 170 | + .setRepeat(job.hasRepeats() ? job.getRepeats() : null) |
| 171 | + .setSchedule(job.hasSchedule() ? JobSchedule.fromString(job.getSchedule()) : null) |
| 172 | + .setDueTime(job.hasDueTime() ? OffsetDateTime.parse(job.getDueTime()) : null) |
| 173 | + .build(); |
| 174 | + }); |
| 175 | + } catch (Exception ex) { |
| 176 | + return DaprException.wrapMono(ex); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Deletes a job based on the given request. |
| 182 | + * |
| 183 | + * @param deleteJobRequest The request containing the job name to be deleted. |
| 184 | + * The name property is mandatory. |
| 185 | + * @return A {@link Mono} that completes when the job is successfully deleted or raises an error. |
| 186 | + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. |
| 187 | + */ |
| 188 | + public Mono<Void> deleteJob(DeleteJobRequest deleteJobRequest) { |
| 189 | + try { |
| 190 | + if (deleteJobRequest == null) { |
| 191 | + throw new IllegalArgumentException("deleteJobRequest cannot be null"); |
| 192 | + } |
| 193 | + |
| 194 | + if (deleteJobRequest.getName() == null || deleteJobRequest.getName().isEmpty()) { |
| 195 | + throw new IllegalArgumentException("Name in the request cannot be null or empty"); |
| 196 | + } |
| 197 | + |
| 198 | + Mono<DaprProtos.DeleteJobResponse> deleteJobResponseMono = |
| 199 | + Mono.deferContextual(context -> this.createMono( |
| 200 | + it -> intercept(context, asyncStub) |
| 201 | + .deleteJobAlpha1(DaprProtos.DeleteJobRequest.newBuilder() |
| 202 | + .setName(deleteJobRequest.getName()).build(), it) |
| 203 | + ) |
| 204 | + ); |
| 205 | + |
| 206 | + return deleteJobResponseMono.then(); |
| 207 | + } catch (Exception ex) { |
| 208 | + return DaprException.wrapMono(ex); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void close() throws Exception { |
| 214 | + ManagedChannel channel = (ManagedChannel) this.asyncStub.getChannel(); |
| 215 | + |
| 216 | + DaprException.wrap(() -> { |
| 217 | + if (channel != null && !channel.isShutdown()) { |
| 218 | + channel.shutdown(); |
| 219 | + } |
| 220 | + |
| 221 | + return true; |
| 222 | + }).call(); |
| 223 | + } |
| 224 | + |
| 225 | + private DaprGrpc.DaprStub intercept( |
| 226 | + ContextView context, DaprGrpc.DaprStub client) { |
| 227 | + return client.withInterceptors(new DaprTimeoutInterceptor(this.timeoutPolicy), new DaprTracingInterceptor(context)); |
| 228 | + } |
| 229 | + |
| 230 | + private <T> Mono<T> createMono(Consumer<StreamObserver<T>> consumer) { |
| 231 | + return retryPolicy.apply( |
| 232 | + Mono.create(sink -> DaprException.wrap(() -> consumer.accept( |
| 233 | + createStreamObserver(sink))).run())); |
| 234 | + } |
| 235 | + |
| 236 | + private <T> StreamObserver<T> createStreamObserver(MonoSink<T> sink) { |
| 237 | + return new StreamObserver<T>() { |
| 238 | + @Override |
| 239 | + public void onNext(T value) { |
| 240 | + sink.success(value); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public void onError(Throwable t) { |
| 245 | + sink.error(DaprException.propagate(DaprHttpException.fromGrpcExecutionException(null, t))); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public void onCompleted() { |
| 250 | + sink.success(); |
| 251 | + } |
| 252 | + }; |
| 253 | + } |
| 254 | +} |
0 commit comments