public BrokerFailJobRequest setErrorMessage(String errorMessage) { requestDto.setErrorMessage(errorMessage); return this; }
private void activateJobs(TypedStreamWriter streamWriter, JobBatchRecord value) { final Iterator<JobRecord> iterator = value.jobs().iterator(); final Iterator<LongValue> keyIt = value.jobKeys().iterator(); while (iterator.hasNext() && keyIt.hasNext()) { final JobRecord jobRecord = iterator.next(); final LongValue next1 = keyIt.next(); final long key = next1.getValue(); // update state and write follow up event for job record final long elementInstanceKey = jobRecord.getHeaders().getElementInstanceKey(); if (elementInstanceKey >= 0) { final DirectBuffer payload = collectPayload(variableNames, elementInstanceKey); jobRecord.setPayload(payload); } else { jobRecord.setPayload(WorkflowInstanceRecord.EMPTY_PAYLOAD); } // we have to copy the job record because #write will reset the iterator state final ExpandableArrayBuffer copy = new ExpandableArrayBuffer(); jobRecord.write(copy, 0); final JobRecord copiedJob = new JobRecord(); copiedJob.wrap(copy, 0, jobRecord.getLength()); state.activate(key, copiedJob); streamWriter.appendFollowUpEvent(key, JobIntent.ACTIVATED, copiedJob); } }
private void assertJobRecordIsEqualTo(final JobRecord jobRecord, final JobRecord expected) { assertThat(jobRecord.getDeadline()).isEqualTo(expected.getDeadline()); assertThat(jobRecord.getWorker()).isEqualTo(expected.getWorker()); assertThat(jobRecord.getRetries()).isEqualTo(expected.getRetries()); assertThat(jobRecord.getType()).isEqualTo(expected.getType()); assertThat(jobRecord.getCustomHeaders()).isEqualTo(expected.getCustomHeaders()); assertThat(jobRecord.getPayload()).isEqualTo(expected.getPayload()); }
private JobRecord newJobRecord() { final JobRecord jobRecord = new JobRecord(); jobRecord.setRetries(2); jobRecord.setDeadline(256L); jobRecord.setType("test"); return jobRecord; }
new JobRecord() .setWorker(wrapString(worker)) .setType(wrapString(type)) .setPayload(PAYLOAD_MSGPACK) .setRetries(retries) .setDeadline(deadline) .setErrorMessage("failed message"); record .getHeaders() .setBpmnProcessId(wrapString(bpmnProcessId)) .setWorkflowKey(workflowKey) .setElementInstanceKey(activityInstanceKey); record.setCustomHeaders(CUSTOM_HEADERS_MSGPACK);
.setWorker(wrapString(worker)) .setType(wrapString(type)) .setPayload(PAYLOAD_MSGPACK) .setRetries(3) .setErrorMessage("failed message") .setDeadline(1000L); .getHeaders() .setBpmnProcessId(wrapString(bpmnProcessId)) .setWorkflowKey(workflowKey)
@Test public void shouldDeleteFailedJob() { // given final long key = 1L; final JobRecord jobRecord = newJobRecord(); // when jobState.create(key, jobRecord); jobState.activate(key, jobRecord); jobState.fail(key, jobRecord.setRetries(0)); jobState.delete(key, jobRecord); // then assertThat(jobState.exists(key)).isFalse(); assertThat(jobState.isInState(key, State.NOT_FOUND)).isTrue(); assertThat(jobState.getJob(key)).isNull(); refuteListedAsActivatable(key, jobRecord.getType()); refuteListedAsTimedOut(key, jobRecord.getDeadline() + 1); }
public void timeout(final long key, final JobRecord record) { final DirectBuffer type = record.getType(); final long deadline = record.getDeadline(); validateParameters(type, deadline); zeebeDb.batch( () -> { createJob(key, record, type); removeJobDeadline(deadline); }); }
private void populateJobFromTask( BpmnStepContext<T> context, WorkflowInstanceRecord value, ExecutableServiceTask serviceTask) { final DirectBuffer headers = serviceTask.getEncodedHeaders(); jobCommand.reset(); jobCommand .setType(serviceTask.getType()) .setRetries(serviceTask.getRetries()) .setPayload(value.getPayload()) .setCustomHeaders(headers) .getHeaders() .setBpmnProcessId(value.getBpmnProcessId()) .setWorkflowDefinitionVersion(value.getVersion()) .setWorkflowKey(value.getWorkflowKey()) .setWorkflowInstanceKey(value.getWorkflowInstanceKey()) .setElementId(serviceTask.getId()) .setElementInstanceKey(context.getRecord().getKey()); } }
@Override public void onCommand(TypedRecord<JobRecord> command, CommandControl<JobRecord> commandControl) { final long key = command.getKey(); final JobState.State jobState = state.getState(key); if (jobState == State.ACTIVATED) { final JobRecord failedJob = state.getJob(key); failedJob.setRetries(command.getValue().getRetries()); failedJob.setErrorMessage(command.getValue().getErrorMessage()); state.fail(key, failedJob); commandControl.accept(JobIntent.FAILED, failedJob); } else if (jobState == State.ACTIVATABLE) { commandControl.reject( RejectionType.INVALID_STATE, String.format(NOT_ACTIVATED_JOB_MESSAGE, key, "must be activated first")); } else if (jobState == State.FAILED) { commandControl.reject( RejectionType.INVALID_STATE, String.format(NOT_ACTIVATED_JOB_MESSAGE, key, "is marked as failed")); } else { commandControl.reject( RejectionType.NOT_FOUND, String.format(NOT_ACTIVATED_JOB_MESSAGE, key, "does not exist")); } } }
@Setup public void setUp() { jobRecord.setDeadline(System.currentTimeMillis()); jobRecord.setType(BufferUtil.wrapString("someTaskType")); w.writeString(BufferUtil.wrapString("yetAnotherValue")); }); jobRecord.setPayload(payload); w.writeString(BufferUtil.wrapString("value")); }); jobRecord.setCustomHeaders(headers); optimalOrderMsgPack = new UnsafeBuffer(new byte[jobRecord.getLength()]); jobRecord.write(optimalOrderMsgPack, 0);
public void fail(long key, JobRecord updatedValue) { final DirectBuffer type = updatedValue.getType(); final long deadline = updatedValue.getDeadline(); validateParameters(type, deadline); zeebeDb.batch( () -> { updateJobRecord(key, updatedValue); final State newState = updatedValue.getRetries() > 0 ? State.ACTIVATABLE : State.FAILED; updateJobState(newState); if (newState == State.ACTIVATABLE) { makeJobActivatable(type); } removeJobDeadline(deadline); }); }
@Test public void shouldNotOverwritePreviousRecord() { // given final long key = 1L; final JobRecord writtenRecord = newJobRecord(); // when jobState.create(key, writtenRecord); writtenRecord.setType("foo"); // then final JobRecord readRecord = jobState.getJob(key); assertThat(readRecord.getType()).isNotEqualTo(writtenRecord.getType()); assertThat(readRecord.getType()).isEqualTo(BufferUtil.wrapString("test")); assertThat(writtenRecord.getType()).isEqualTo(BufferUtil.wrapString("foo")); }
@Override public void processRecord( TypedRecord<JobRecord> event, TypedResponseWriter responseWriter, TypedStreamWriter streamWriter) { final JobRecord value = event.getValue(); if (value.getRetries() <= 0) { final JobHeaders jobHeaders = value.getHeaders(); final DirectBuffer jobErrorMessage = value.getErrorMessage(); DirectBuffer incidentErrorMessage = DEFAULT_ERROR_MESSAGE; if (jobErrorMessage.capacity() > 0) { incidentErrorMessage = jobErrorMessage; } incidentEvent.reset(); incidentEvent .setErrorType(ErrorType.JOB_NO_RETRIES) .setErrorMessage(incidentErrorMessage) .setBpmnProcessId(jobHeaders.getBpmnProcessId()) .setWorkflowInstanceKey(jobHeaders.getWorkflowInstanceKey()) .setElementId(jobHeaders.getElementId()) .setElementInstanceKey(jobHeaders.getElementInstanceKey()) .setJobKey(event.getKey()); streamWriter.appendNewCommand(IncidentIntent.CREATE, incidentEvent); } } }
@Override public void onCommand(TypedRecord<JobRecord> command, CommandControl<JobRecord> commandControl) { final long jobKey = command.getKey(); final JobState.State jobState = state.getState(jobKey); if (jobState == State.NOT_FOUND) { commandControl.reject(RejectionType.NOT_FOUND, String.format(NO_JOB_FOUND_MESSAGE, jobKey)); } else if (jobState == State.FAILED) { commandControl.reject(RejectionType.INVALID_STATE, String.format(FAILED_JOB_MESSAGE, jobKey)); } else { final JobRecord job = state.getJob(jobKey); job.setPayload(command.getValue().getPayload()); state.delete(jobKey, job); commandControl.accept(JobIntent.COMPLETED, job); } } }
@Override public void processRecord( final TypedRecord<JobRecord> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter) { final JobRecord jobEvent = record.getValue(); final JobHeaders jobHeaders = jobEvent.getHeaders(); final long elementInstanceKey = jobHeaders.getElementInstanceKey(); final ElementInstance elementInstance = workflowState.getElementInstanceState().getInstance(elementInstanceKey); if (elementInstance != null) { final WorkflowInstanceRecord value = elementInstance.getValue(); value.setPayload(jobEvent.getPayload()); streamWriter.appendFollowUpEvent( elementInstanceKey, WorkflowInstanceIntent.ELEMENT_COMPLETING, value); elementInstance.setState(WorkflowInstanceIntent.ELEMENT_COMPLETING); elementInstance.setJobKey(-1); elementInstance.setValue(value); workflowState .getElementInstanceState() .getVariablesState() .setPayload(elementInstanceKey, jobEvent.getPayload()); } } }
private JobRecordValue ofJobRecord(final LoggedEvent event) { final JobRecord record = new JobRecord(); event.readValue(record); return ofJobRecord(record); }
public void create(final long key, final JobRecord record) { final DirectBuffer type = record.getType(); zeebeDb.batch(() -> createJob(key, record, type)); }
@Override public void processRecord( TypedRecord<JobRecord> record, TypedResponseWriter responseWriter, TypedStreamWriter streamWriter) { final JobHeaders jobHeaders = record.getValue().getHeaders(); final long elementInstanceKey = jobHeaders.getElementInstanceKey(); if (elementInstanceKey > 0) { final ElementInstance elementInstance = workflowState.getElementInstanceState().getInstance(elementInstanceKey); if (elementInstance != null) { elementInstance.setJobKey(record.getKey()); } } } }