use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.
the class GrpcResponseWriter method createJobBatchResponse.
private GeneratedMessageV3 createJobBatchResponse() {
final JobBatchRecord jobBatch = new JobBatchRecord();
jobBatch.wrap(valueBufferView);
final Map<Long, JobRecord> jobsWithKeys = new HashMap<>();
for (int index = 0; index < jobBatch.getJobKeys().size(); index++) {
final Long key = jobBatch.getJobKeys().get(index);
final JobRecord value = (JobRecord) jobBatch.getJobs().get(index);
jobsWithKeys.put(key, value);
}
return ActivateJobsResponse.newBuilder().addAllJobs(jobsWithKeys.entrySet().stream().map((entry) -> {
final JobRecord job = entry.getValue();
return ActivatedJob.newBuilder().setKey(entry.getKey()).setType(job.getType()).setRetries(job.getRetries()).setWorker(job.getWorker()).setDeadline(job.getDeadline()).setProcessDefinitionKey(job.getProcessDefinitionKey()).setBpmnProcessId(job.getBpmnProcessId()).setProcessDefinitionVersion(job.getProcessDefinitionVersion()).setProcessInstanceKey(job.getProcessInstanceKey()).setElementId(job.getElementId()).setElementInstanceKey(job.getElementInstanceKey()).setCustomHeaders(MsgPackConverter.convertToJson(job.getCustomHeadersBuffer())).setVariables(MsgPackConverter.convertToJson(job.getVariablesBuffer())).build();
}).collect(Collectors.toList())).build();
}
use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.
the class GrpcToLogStreamGateway method failJob.
@Override
public void failJob(final FailJobRequest request, final StreamObserver<FailJobResponse> responseObserver) {
final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.FAIL);
final JobRecord jobRecord = new JobRecord();
jobRecord.setRetries(request.getRetries());
jobRecord.setErrorMessage(request.getErrorMessage());
writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.
the class GrpcToLogStreamGateway method updateJobRetries.
@Override
public void updateJobRetries(final UpdateJobRetriesRequest request, final StreamObserver<UpdateJobRetriesResponse> responseObserver) {
final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.UPDATE_RETRIES);
final JobRecord jobRecord = new JobRecord();
jobRecord.setRetries(request.getRetries());
writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.
the class GrpcToLogStreamGateway method throwError.
@Override
public void throwError(final ThrowErrorRequest request, final StreamObserver<ThrowErrorResponse> responseObserver) {
final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.THROW_ERROR);
final JobRecord jobRecord = new JobRecord();
jobRecord.setErrorCode(BufferUtil.wrapString(request.getErrorCode()));
jobRecord.setErrorMessage(request.getErrorMessage());
writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe by zeebe-io.
the class JobBatchCollector method collectJobs.
/**
* Collects jobs to be added to the given {@code record}. The jobs and their keys are added
* directly to the given record.
*
* <p>This method will fail only if it could not activate anything because the batch would be too
* large, but there was at least one job to activate. On failure, it will return that job and its
* key. On success, it will return the amount of jobs activated.
*
* @param record the batch activate command; jobs and their keys will be added directly into it
* @return the amount of activated jobs on success, or a job which was too large to activate
*/
Either<TooLargeJob, Integer> collectJobs(final TypedRecord<JobBatchRecord> record) {
final JobBatchRecord value = record.getValue();
final ValueArray<JobRecord> jobIterator = value.jobs();
final ValueArray<LongValue> jobKeyIterator = value.jobKeys();
final Collection<DirectBuffer> requestedVariables = collectVariableNames(value);
final var maxActivatedCount = value.getMaxJobsToActivate();
final var activatedCount = new MutableInteger(0);
final var jobCopyBuffer = new ExpandableArrayBuffer();
final var unwritableJob = new MutableReference<TooLargeJob>();
jobState.forEachActivatableJobs(value.getTypeBuffer(), (key, jobRecord) -> {
// fill in the job record properties first in order to accurately estimate its size before
// adding it to the batch
final var deadline = record.getTimestamp() + value.getTimeout();
jobRecord.setDeadline(deadline).setWorker(value.getWorkerBuffer());
setJobVariables(requestedVariables, jobRecord, jobRecord.getElementInstanceKey());
// the expected length is based on the current record's length plus the length of the job
// record we would add to the batch, the number of bytes taken by the additional job key,
// as well as one byte required per job key for its type header. if we ever add more, this
// should be updated accordingly.
final var jobRecordLength = jobRecord.getLength();
final var expectedEventLength = record.getLength() + jobRecordLength + Long.BYTES + 1;
if (activatedCount.value <= maxActivatedCount && canWriteEventOfLength.test(expectedEventLength)) {
appendJobToBatch(jobIterator, jobKeyIterator, jobCopyBuffer, key, jobRecord);
activatedCount.increment();
} else {
// activate it
if (activatedCount.value == 0) {
unwritableJob.set(new TooLargeJob(key, jobRecord));
}
value.setTruncated(true);
return false;
}
return activatedCount.value < maxActivatedCount;
});
if (unwritableJob.ref != null) {
return Either.left(unwritableJob.ref);
}
return Either.right(activatedCount.value);
}
Aggregations