Giving up a step early
What you want to do
Some failures can't be mended by trying again: Ned's parcel is heavier than any carrier of the shop collects, and when the bank's network fails during a charge, the rescue step, which mends only an expired card, can't help either. You want the shop to give such a step up at once, before the retry or the rescue the workflow would otherwise run.
The code
The shipping first learns a rule of the shop's business: the shop ships no parcel over 80 kg, the most its backup carrier collects. It fails such an order with the reason over-parcel-limit, before it books anything:
/**
* Ships an order through the carrier, with its receipt in the parcel, gives back its tracking
* number, and says when the parcel has left. A parcel the carrier won't collect may be taken on
* another attempt. A gift card has nothing to ship, and the shop ships no parcel over 80 kg.
*/
@Step
final class Ship implements ItineraStep {
private final String order;
private final long weight;
private final String receipt;
private final boolean giftCard;
Ship(@Input String order, @Input long weight, @Input String receipt, @Input boolean giftCard) {
this.order = order;
this.weight = weight;
this.receipt = receipt;
this.giftCard = giftCard;
}
public Outcome run(@Service Carrier carrier, Contributor contributor, StepReporter reporter)
throws BookingFailed {
if (giftCard) return skipBecause("nothing-to-ship");
if (weight > 80) return fail("over-parcel-limit");
String tracking = carrier.book(order, weight);
contributor.contribute("tracking", tracking);
if (!carrier.collects(weight)) return fail("too-heavy");
reporter.info("the parcel leaves with receipt " + receipt);
return succeed();
}
}
The interface Carrier it asks for is chapter 12's.
Then the shop gets two new step policies. The first, heavyParcels, has a hook marked @OnStepRetry, from org.itineradev.itinera.policy. It asks for the reason the shipping's attempt failed with, and gives the shipping up when that reason is over-parcel-limit, since the limit is the same on every attempt.
/**
* Gives up the shipping of a parcel over the shop's limit, rather than try it again: the limit is
* the same on every attempt, whichever carrier comes.
*/
@StepPolicy(name = "heavyParcels")
final class HeavyParcels implements ItineraStepPolicy {
@OnStepRetry
public OnFailure beforeRetrying(Optional<Reason> reason) {
boolean overLimit = reason.map(Reason::code).filter("over-parcel-limit"::equals).isPresent();
return overLimit ? failWorkflow("no-carrier-can-take-it") : keepDefault();
}
}
The second, outage, has a hook marked @OnStepAbnormalTermination, from the same package. It asks for nothing, and gives up the step it is attached to whenever an exception escapes it.
/**
* Gives up a step that ends in an error at once: the bank's network is down, and neither a retry
* nor a rescue can mend that.
*/
@StepPolicy(name = "outage")
final class Outage implements ItineraStepPolicy {
@OnStepAbnormalTermination
public OnFailure endedInError() {
return failWorkflow("payment-network-down");
}
}
The workflow attaches Outage to the charge, beside the audit, and HeavyParcels to the shipping, in the policies of each step's @WithStep:
/**
* The shop's workflow: it charges, repairing a charge that fails and auditing one that succeeds,
* then ships, and shipping may be tried once more.
*/
@Workflow(
name = "orders",
reporters = {Log.class, Desk.class})
@WithStep(
step = Charge.class,
name = "charge",
rescue = RenewCard.class,
policies = {Audit.class, Outage.class})
@WithStep(
step = Ship.class,
name = "ship",
retryBudget = 1,
policies = {HeavyParcels.class})
final class Orders implements ItineraWorkflow {
/** The bank, as the payment client of the journey. */
@Provider(key = "payments")
public Provided<Payments> bank() {
return provide(new Bank(2026));
}
/** A carrier for every attempt of a step: the main carrier first, the backup carrier after. */
@Provider(perAttempt = true, key = "carrier")
public Provided<Carrier> carriers(Attempt attempt) {
return attempt.isFirst() ? provide(new MainCarrier()) : provide(new BackupCarrier());
}
/** Names each journey after its order. */
@IdGenerator
public String orderNumber(DataBag data) {
String order = data.read("order", TypeToken.of(String.class)).orNull();
if (order == null) throw new IllegalStateException("an order without a number");
return order;
}
}
The charge and the audit are chapter 15's, RenewCard chapter 14's and the desk chapter 11's. The bank's provider is chapter 12's, the interface Payments it lends chapter 14's, and the carriers' provider chapter 13's.
The log is chapter 11's, with chapter 12's detail and the tests chapters 14 to 16 added. Its test for StepGivenUp now calls a method of its own, which says which policy gave a step up, and why:
/** What the log says about a step given up: which policy gave it up, and why, when one did. */
static String givenUp(StepGivenUp event) {
if (event.cause() instanceof GiveUpCause.FailWorkflow failed) {
return event.step()
+ ": FailWorkflow from "
+ failed.decidedBy().policy()
+ ", "
+ failed.reason().code();
}
return event.step() + ": " + event.cause().getCause();
}It runs the same five orders as chapter 16, and Cat's again, whose 120 is more than the bank's network can take, as in chapter 12. It prints:
Ann's order:
log orders A-1 #1 journey_started order, price, weight, card, giftCard
log orders A-1 #2 attempt_started charge, attempt 1
log orders A-1 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 40 for order A-1
log orders A-1 #4 step_succeeded charge
log orders A-1 #5 contribution_committed receipt
log orders A-1 #6 hook_called audit: on step success of charge
log orders A-1 #7 contribution_committed audit
log orders A-1 #8 attempt_started ship, attempt 1
log orders A-1 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-1
log orders A-1 #10 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 #11 step_succeeded ship
log orders A-1 #12 contribution_committed tracking
log orders A-1 #13 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
audit: charge took 40, receipt R-A-1
Gil's order:
log orders A-7 #1 journey_started order, price, weight, card, giftCard
log orders A-7 #2 attempt_started charge, attempt 1
log orders A-7 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 45 for order A-7
log orders A-7 #4 step_succeeded charge
log orders A-7 #5 contribution_committed receipt
log orders A-7 #6 hook_called audit: on step success of charge
log orders A-7 #7 contribution_committed audit
log orders A-7 #8 attempt_started ship, attempt 1
log orders A-7 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-7
log orders A-7 #10 step_failed ship: too-heavy
log orders A-7 #11 hook_called heavyParcels: on step retry of ship
log orders A-7 #12 step_retrying ship
log orders A-7 #13 attempt_started ship, attempt 2
log orders A-7 #14 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.BackupCarrier
the backup carrier books order A-7
log orders A-7 #15 step_info ship: the parcel leaves with receipt R-A-7
log orders A-7 #16 step_succeeded ship
log orders A-7 #17 contribution_committed tracking
log orders A-7 #18 journey_succeeded
the journey succeeded, with receipt R-A-7 and tracking B-A-7
audit: charge took 45, receipt R-A-7
Jo's order:
log orders A-8 #1 journey_started order, price, weight, card, giftCard
log orders A-8 #2 attempt_started charge, attempt 1
log orders A-8 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank declines card jo-2024: it has expired
log orders A-8 #4 step_failed charge: card-expired
log orders A-8 #5 step_rescuing charge: failure, rescued by charge_rescue
log orders A-8 #6 attempt_started charge_rescue, attempt 1
the bank says card jo-2024 was replaced by jo-2029
log orders A-8 #7 step_succeeded charge_rescue
log orders A-8 #8 contribution_committed card
log orders A-8 #9 data_overwritten card
log orders A-8 #10 step_rescued charge by charge_rescue
log orders A-8 #11 attempt_started charge, attempt 2
the bank charges 30 for order A-8
log orders A-8 #12 step_succeeded charge
log orders A-8 #13 contribution_committed receipt
log orders A-8 #14 hook_called audit: on step success of charge
log orders A-8 #15 contribution_committed audit
log orders A-8 #16 attempt_started ship, attempt 1
log orders A-8 #17 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-8
log orders A-8 #18 step_info ship: the parcel leaves with receipt R-A-8
log orders A-8 #19 step_succeeded ship
log orders A-8 #20 contribution_committed tracking
log orders A-8 #21 journey_succeeded
the journey succeeded, with receipt R-A-8 and tracking T-A-8
audit: charge took 30, receipt R-A-8
Kim's order:
log orders A-9 #1 journey_started order, price, weight, card, giftCard
log orders A-9 #2 attempt_started charge, attempt 1
log orders A-9 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 25 for order A-9
log orders A-9 #4 step_succeeded charge
log orders A-9 #5 contribution_committed receipt
log orders A-9 #6 contribution_committed code
log orders A-9 #7 hook_called audit: on step success of charge
log orders A-9 #8 contribution_committed audit
log orders A-9 #9 attempt_started ship, attempt 1
log orders A-9 #10 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
log orders A-9 #11 step_skipped ship: nothing-to-ship
log orders A-9 #12 contributions_discarded ship
log orders A-9 #13 journey_succeeded
the journey succeeded, with receipt R-A-9 and tracking none
audit: charge took 25, receipt R-A-9
Ned's order:
log orders A-12 #1 journey_started order, price, weight, card, giftCard
log orders A-12 #2 attempt_started charge, attempt 1
log orders A-12 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 50 for order A-12
log orders A-12 #4 step_succeeded charge
log orders A-12 #5 contribution_committed receipt
log orders A-12 #6 hook_called audit: on step success of charge
log orders A-12 #7 contribution_committed audit
log orders A-12 #8 attempt_started ship, attempt 1
log orders A-12 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
log orders A-12 #10 step_failed ship: over-parcel-limit
log orders A-12 #11 hook_called heavyParcels: on step retry of ship
log orders A-12 #12 step_given_up ship: FailWorkflow from heavyParcels, no-carrier-can-take-it
log orders A-12 #13 journey_failed ship
desk: order A-12 failed at ship
the journey failed, with receipt R-A-12 and tracking none
audit: charge took 50, receipt R-A-12
Cat's order:
log orders A-3 #1 journey_started order, price, weight, card, giftCard
log orders A-3 #2 attempt_started charge, attempt 1
log orders A-3 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank's network fails while charging order A-3
log orders A-3 #4 step_abnormal_termination charge: the payment network is down
log orders A-3 #5 hook_called outage: on step abnormal termination of charge
log orders A-3 #6 step_given_up charge: FailWorkflow from outage, payment-network-down
log orders A-3 #7 journey_failed charge
desk: order A-3 failed at charge
the journey failed, with receipt none and tracking none
audit: noneHow it works
- A hook can end the journey. What a hook returns can hold a lifecycle: a decision that ends the journey there.
FailWorkflowfails it, with a reason, as a step's failure has one.failWorkflow(...), whichItineraStepPolicygives the class, returns it, andkeepDefault(), as in chapter 15, leaves what happens next unchanged. on step retryruns before a retry. Gil's parcel of 60 kg is within the limit, but the main carrier won't collect it, so the shipping fails withtoo-heavy, the hook keeps the default, and the backup carrier takes the parcel on attempt 2. Ned's parcel of 90 kg is over the limit, so the shipping fails withover-parcel-limit, and the hook fails the journey: the shipping is given up with the causeFailWorkflow, decided byheavyParcels. The retry budget says how many retries a step may have; a policy can still decide that none of them can help.- The step says what happened, and the policy decides what to do about it. The limit is a rule of the business, so it lives in the shipping; the hook only branches on the reason. It asks for the reason by its type, as the rescue step does in chapter 14, but as an
Optional<Reason>, since an attempt that ended in an exception reports none. on step abnormal terminationruns first after an exception. It is called as soon as an exception escapingrunends an attempt, before any retry, rescue or giving up. Cat's charge ended in the bank'sNetworkDown, chapter 12's, sooutagefailed the journey with the reasonpayment-network-down, and the charge was given up at once. Its rescue step never ran.- A step given up by a hook says who decided.
StepGivenUp'scause()is aGiveUpCause, fromorg.itineradev.itinera.event, here aGiveUpCause.FailWorkflow, which carries thereason()and, asdecidedBy(), the policy and the hook that returned it.givenUptests for it withinstanceof, and writes any other cause as chapter 12 does. Thenjourney_failedfollows, naming the step. Neither step decided to give up: the shipping only reported that the parcel was over the limit, and the charge only let the bank's exception escape. - Each hook may return only some lifecycles, and its type says which. A method marked
@OnStepRetryor@OnStepAbnormalTerminationreturns anOnFailure, fromorg.itineradev.itinera.policy, whose only values are whatkeepDefault()andfailWorkflow(...)return, since failing the journey is the only thing either hook may decide; the compiler refuses anything else.
So no order spends a retry or a rescue that can't help it. But none of the shop's journeys tells the customer how it went: the lines that say whether a journey succeeded are printed by main, and no customer ever reads them.
Outcomes
We have shown how a step policy gives a step up early, when no retry or rescue can help it.
| The usual way | With Itinera |
|---|---|
| Deciding that a retry is pointless usually means more conditions inside the retry loop, beside the call it retries. | A policy's on step retry hook decides it from the reason the step reported, attached to the step in the workflow's declaration. |
| What happens after an unexpected error is often decided wherever the error is caught. | on step abnormal termination decides it for the step, before any retry or rescue, and the event says which policy did. |