All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.service.JobServices Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha1
Show newest version
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.service;

import io.camunda.search.clients.CamundaSearchClient;
import io.camunda.service.security.auth.Authentication;
import io.camunda.service.transformers.ServiceTransformers;
import io.camunda.zeebe.broker.client.api.BrokerClient;
import io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest;
import io.camunda.zeebe.gateway.impl.broker.request.BrokerCompleteJobRequest;
import io.camunda.zeebe.gateway.impl.broker.request.BrokerFailJobRequest;
import io.camunda.zeebe.gateway.impl.broker.request.BrokerThrowErrorRequest;
import io.camunda.zeebe.gateway.impl.broker.request.BrokerUpdateJobRequest;
import io.camunda.zeebe.gateway.impl.job.ActivateJobsHandler;
import io.camunda.zeebe.gateway.impl.job.ResponseObserver;
import io.camunda.zeebe.protocol.impl.record.value.job.JobRecord;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public final class JobServices extends ApiServices> {

  private final ActivateJobsHandler activateJobsHandler;

  public JobServices(
      final BrokerClient brokerClient,
      final ActivateJobsHandler activateJobsHandler,
      final CamundaSearchClient dataStoreClient) {
    this(brokerClient, activateJobsHandler, dataStoreClient, null, null);
  }

  public JobServices(
      final BrokerClient brokerClient,
      final ActivateJobsHandler activateJobsHandler,
      final CamundaSearchClient searchClient,
      final ServiceTransformers transformers,
      final Authentication authentication) {
    super(brokerClient, searchClient, transformers, authentication);
    this.activateJobsHandler = activateJobsHandler;
  }

  @Override
  public JobServices withAuthentication(final Authentication authentication) {
    return new JobServices<>(
        brokerClient, activateJobsHandler, searchClient, transformers, authentication);
  }

  public void activateJobs(
      final ActivateJobsRequest request,
      final ResponseObserver responseObserver,
      final Consumer cancelationHandlerConsumer) {
    final var brokerRequest =
        new BrokerActivateJobsRequest(request.type())
            .setMaxJobsToActivate(request.maxJobsToActivate())
            .setTenantIds(request.tenantIds())
            .setTimeout(request.timeout())
            .setWorker(request.worker())
            .setVariables(request.fetchVariable());
    activateJobsHandler.activateJobs(
        brokerRequest, responseObserver, cancelationHandlerConsumer, request.requestTimeout());
  }

  public CompletableFuture failJob(
      final long jobKey,
      final int retries,
      final String errorMessage,
      final Long retryBackOff,
      final Map variables) {
    final var request =
        new BrokerFailJobRequest(jobKey, retries, retryBackOff)
            .setVariables(getDocumentOrEmpty(variables))
            .setErrorMessage(errorMessage);
    return sendBrokerRequest(request);
  }

  public CompletableFuture errorJob(
      final long jobKey,
      final String errorCode,
      final String errorMessage,
      final Map variables) {
    final var request =
        new BrokerThrowErrorRequest(jobKey, errorCode)
            .setErrorMessage(errorMessage)
            .setVariables(getDocumentOrEmpty(variables));
    return sendBrokerRequest(request);
  }

  public CompletableFuture completeJob(
      final long jobKey, final Map variables) {
    return sendBrokerRequest(new BrokerCompleteJobRequest(jobKey, getDocumentOrEmpty(variables)));
  }

  public CompletableFuture updateJob(
      final long jobKey, final UpdateJobChangeset changeset) {
    return sendBrokerRequest(
        new BrokerUpdateJobRequest(jobKey, changeset.retries(), changeset.timeout()));
  }

  public record ActivateJobsRequest(
      String type,
      int maxJobsToActivate,
      List tenantIds,
      long timeout,
      String worker,
      List fetchVariable,
      long requestTimeout) {}

  public record UpdateJobChangeset(Integer retries, Long timeout) {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy