Method: Temporalio::Activity::Context#cancel

Defined in:
lib/temporalio/activity/context.rb

#cancel(reason, by_request: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Cancel the running activity by raising a provided error.

Parameters:

  • reason (String)

    Reason for cancellation.

  • by_request (Boolean) (defaults to: false)

    Cancellation requested by the server or not.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/temporalio/activity/context.rb', line 83

def cancel(reason, by_request: false)
  error = Temporalio::Error::ActivityCancelled.new(reason, by_request)
  @pending_cancellation = error

  locked = mutex.try_lock
  # @shielded inside the lock means the whole activity is shielded
  if locked && !@shielded
    thread.raise(error)
  end
ensure
  # only release the lock if we locked it
  mutex.unlock if locked
end