Saga Compensate vs Workflow CancellationScope

Saga is a way to dynamically accumulate compensations based on the code path of the workflow. Cancellation is a way to interrupt workflow execution at any point.

    Saga saga = new Saga(sagaOptions);
    try {
      String carReservationID = activities.reserveCar(name);
      saga.addCompensation(activities::cancelCar, carReservationID, name);

      if (bookHotel) {
        String hotelReservationID = activities.bookHotel(name);
        saga.addCompensation(activities::cancelHotel, hotelReservationID, name);
      }
      String flightReservationID = activities.bookFlight(name);
      saga.addCompensation(activities::cancelFlight, flightReservationID, name);
    } catch (TemporalFailure e) {
      Workflow.newDetachedCancellationScope(() -> saga.compensate()).run();
      throw e;
    }

In the sample above if bookHotel is false it is not going to include cancelHotel activity invocation when saga.compensate is called. Note that the TemporalFailure can be either an ActivityFailure or CanceledFailure if the workflow is canceled while executing any of the reservation activities.