module ZeebeBpmnRspec::Helpers

Public Instance Methods

activate_job(type, fetch_variables: nil, validate: true, worker: " click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 72
def activate_job(type, fetch_variables: nil, validate: true, worker: "#{type}-#{SecureRandom.hex}")
  raise ArgumentError.new("'worker' cannot be blank") if worker.blank?

  stream = _zeebe_client.activate_jobs(ActivateJobsRequest.new({
    type: type,
    worker: worker,
    maxJobsToActivate: 1,
    timeout: 1000,
    fetchVariable: fetch_variables&.then { |v| Array(v) },
    requestTimeout: ZeebeBpmnRspec.activate_request_timeout,
  }.compact))

  job = nil
  stream.find { |response| job = response.jobs.first }
  # puts job.inspect # support debug logging?

  ActivatedJob.new(job,
                   type: type,
                   process_instance_key: process_instance_key,
                   client: _zeebe_client,
                   context: self,
                   validate: validate)
end
Also aliased as: process_job
activate_jobs(type, max_jobs: nil, fetch_variables: nil) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 106
def activate_jobs(type, max_jobs: nil, fetch_variables: nil)
  stream = _zeebe_client.activate_jobs(ActivateJobsRequest.new({
    type: type,
    worker: "#{type}-#{SecureRandom.hex}",
    maxJobsToActivate: max_jobs,
    timeout: 1000,
    fetchVariable: fetch_variables&.then { |v| Array(v) },
    requestTimeout: ZeebeBpmnRspec.activate_request_timeout,
  }.compact))

  Enumerator.new do |yielder|
    stream.each do |response|
      response.jobs.each do |job|
        yielder << ActivatedJob.new(job,
                                    type: type,
                                    process_instance_key: process_instance_key,
                                    client: _zeebe_client,
                                    context: self,
                                    validate: true)
      end
    end
  end
end
deploy_process(path, name = nil) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 11
def deploy_process(path, name = nil)
  _zeebe_client.deploy_process(DeployProcessRequest.new(
                                 processes: [ProcessRequestObject.new(
                                   name: (name && "#{name}.bpmn") || File.basename(path),
                                   definition: File.read(path)
                                 )]
                               ))
rescue StandardError => e
  raise "Failed to deploy precess: #{e}"
end
expect_job_of_type(type, fetch_variables: nil) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 102
def expect_job_of_type(type, fetch_variables: nil)
  expect(job_with_type(type, fetch_variables: fetch_variables))
end
job_with_type(type, fetch_variables: nil) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 98
def job_with_type(type, fetch_variables: nil)
  activate_job(type, fetch_variables: fetch_variables, validate: false)
end
process_complete!(wait_seconds: 0.25) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 52
def process_complete!(wait_seconds: 0.25)
  error = nil
  sleep(wait_seconds)
  begin
    _zeebe_client.cancel_process_instance(CancelProcessInstanceRequest.new(
                                            processInstanceKey: process_instance_key
                                          ))
  rescue GRPC::NotFound => e
    error = e
  end

  raise "Expected process instance #{process_instance_key} to be complete" if error.nil?
end
process_instance_key() click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 67
def process_instance_key
  @__process_instance_key
end
process_job(type, fetch_variables: nil, validate: true, worker: "
Alias for: activate_job
publish_message(name, correlation_key:, variables: nil, ttl_ms: 5000) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 130
def publish_message(name, correlation_key:, variables: nil, ttl_ms: 5000)
  _zeebe_client.publish_message(PublishMessageRequest.new(
                                  {
                                    name: name,
                                    correlationKey: correlation_key,
                                    timeToLive: ttl_ms,
                                    variables: variables&.to_json,
                                  }.compact
                                ))
end
reset_zeebe!() click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 149
def reset_zeebe!
  @__process_instance_key = nil
end
set_variables(key, variables, local: true) click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 141
def set_variables(key, variables, local: true)
  _zeebe_client.set_variables(SetVariablesRequest.new(
                                elementInstanceKey: key,
                                variables: variables.to_json,
                                local: local
                              ))
end
with_process_instance(name, variables = {}) { |processInstanceKey| ... } click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 23
def with_process_instance(name, variables = {})
  system_error = nil
  process = _zeebe_client.create_process_instance(CreateProcessInstanceRequest.new(
                                                    bpmnProcessId: name,
                                                    version: -1, # always latest
                                                    variables: variables.to_json
                                                  ))
  @__process_instance_key = process.processInstanceKey
  yield(process.processInstanceKey) if block_given?
rescue Exception => e # rubocop:disable Lint/RescueException
  # exceptions are rescued to ensure that instances are cancelled
  # any error is re-raised below
  system_error = e
ensure
  if process&.processInstanceKey
    begin
      _zeebe_client.cancel_process_instance(CancelProcessInstanceRequest.new(
                                              processInstanceKey: process.processInstanceKey
                                            ))
    rescue GRPC::NotFound => _e
      # expected
    rescue StandardError => _e
      puts "Cancelled instance #{ex.inspect}" # TODO
    end
  end
  raise system_error if system_error
end

Private Instance Methods

_zeebe_client() click to toggle source
# File lib/zeebe_bpmn_rspec/helpers.rb, line 155
def _zeebe_client
  ZeebeBpmnRspec.client
end