Module: Camunda::ExternalTaskJob

Included in:
CamundaJob
Defined in:
lib/camunda/external_task_job.rb

Overview

Camunda::ExternalTaskJob module is included in the generated bpmn_classes for ActiveJob and handles the task completion or failure for a given worker that has been locked to be performed.

See Also:

Instance Method Summary collapse

Instance Method Details

#bpmn_perform(_variables) ⇒ Object

Default bpmn_perform which raises an error. Forces user to create their own implementation

Raises:

  • (StandardError)


57
58
59
# File 'lib/camunda/external_task_job.rb', line 57

def bpmn_perform(_variables)
  raise StandardError, "Please define this method which takes a hash of variables and returns a hash of variables"
end

#perform(id, input_variables) ⇒ Object

Performs the external task for the process definition and processes completion or throws an error. The below example shows how to run a task based off of our generated classes from the bpmn_classes generator from the sample.bpmn file provided.

Examples:

task = Camunda::ExternalTask.fetch_and_lock('CamundaWorkflow').first
CamundaWorkflow::DoSomething.new.perform(task.id, x: 'abcd')

Parameters:

  • id (Integer)

    of the worker for the locked task

  • input_variables (Hash)

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/camunda/external_task_job.rb', line 14

def perform(id, input_variables)
  output_variables = bpmn_perform(input_variables)
  output_variables = {} unless output_variables.is_a?(Hash)

  report_completion id, output_variables
rescue Camunda::BpmnError => e
  report_bpmn_error id, e
rescue Camunda::ExternalTask::SubmissionError => e
  # We re-raise this so it is not rescued below
  raise e
rescue StandardError => e
  report_failure id, e, input_variables
end

#report_bpmn_error(id, exception) ⇒ Object

Reports an error if there is a problem with bpmn_perform

Parameters:

  • id (Integer)

    of the process instance

  • exception (Camunda::BpmnError)

    instance of Camunda::BpmnError



50
51
52
53
54
# File 'lib/camunda/external_task_job.rb', line 50

def report_bpmn_error(id, exception)
  # Submit bpmn error state to Camunda using
  # POST /external-task/{id}/bpmnError
  Camunda::ExternalTask.new(id: id).bpmn_error(exception)
end

#report_completion(id, variables) ⇒ Object

Reports completion for an external task with output variable set in bpmn_perform.

Parameters:

  • id (Integer)

    of the worker

  • variables (Hash)

    output variables declared in bpmn_perform



31
32
33
34
35
# File 'lib/camunda/external_task_job.rb', line 31

def report_completion(id, variables)
  # Submit to Camunda using
  # POST /external-task/{id}/complete
  Camunda::ExternalTask.new(id: id).complete(variables)
end

#report_failure(id, exception, input_variables) ⇒ Object

Reports external task failure to the Camunda process definition and creates an incident report

Parameters:

  • id (Integer)

    of the worker for the process instance

  • exception (Object)

    the exception for the failed task

  • input_variables (Hash)

    given to the process definition



41
42
43
44
45
# File 'lib/camunda/external_task_job.rb', line 41

def report_failure(id, exception, input_variables)
  # Submit error state to Camunda using
  # POST /external-task/{id}/failure
  Camunda::ExternalTask.new(id: id).failure(exception, input_variables)
end