Skip to content

Execution branches

Gurmit Teotia edited this page Dec 11, 2017 · 9 revisions

In a workflow you can schedule multiple activities or timers in parallel or in sequence according to your need. Set of "After.." APIs (e.g. AfterActivity, AfterTimer) helps you to set up dependencies between activities and timers.

Let us look at holiday booking workflow in which you want to schedule the activities in the order as shown in following diagram:

        BookRoom
           |
           v
        AddDinner
           |
           v
     ChargeCustomer
           |
           v
     SendConfirmation

Above described workflow can be implemented in Guflow as below:

    [WorkflowDescription("1.0")]
    public class BookHolidaysWorkflow : Workflow
    {
    
      public BookHolidaysWorkflow()
      {
        ScheduleActivity<BookRoom>();
       	ScheduleActivity<AddDinner>().AfterActivity<BookRoom>();
    	ScheduleActivity<ChargeCustomer>().AfterActivity<AddDinner>();
    	ScheduleActivity<SendConfirmation>().AfterActivity<ChargeCustomer>();
      }
    }

Things to note in above example:

  • BookRoom is the startup activity because it does not depends on any other activity or timer. BookRoom activity will be scheduled when workflow started.
  • AddDinner activity will be scheduled after "BookRoom" activity as configured by "AfterActivity" API.
  • "SendConfirmation" is the last activity in workflow, there are no more children to be scheduled after it, hence workflow will be completed after "SendConfirmation" is successfully completed.

Let us now extend the above workflow to support the booking of flights as shown in following diagram:

        BookRoom            BookFlight
           |                   |
           v                   v
        AddDinner           ChooseSeat
           |                   |
           `````````|```````````
                    v       
              ChargeCustomer
                    |
                    v
             SendConfirmation

You can implement above workflow as shown below:

    [WorkflowDescription("1.0")]
    public class BookHolidaysWorkflow : Workflow
    {
      public BookHolidaysWorkflow()
      {
        ScheduleActivity<BookRoom>();
    	ScheduleActivity<AddDinner>().AfterActivity<BookRoom>();
    	
	ScheduleActivity<BookFlight>();
	ScheduleActivity<ChooseSeat>().AfterActivity<BookFlight>();
		
	ScheduleActivity<ChargeCustomer>().AfterActivity<AddDinner>()
	 .AfterActivity<ChooseSeat>();
    	ScheduleActivity<SendConfirmation>().AfterActivity<ChargeCustomer>();
      }
    }

Things to note in above example:

  • BookRoom and BookFlight are startup activities and will be scheduled in parallel.
  • Up to ChargeCustomer activity, there will be two parallel executing branches of activities.
  • ChargeCustomer activity will be scheduled only when both AddDinner and ChooseSeat activities are completed, irrespective of their order of completion.
  • After SendConfirmation activity is completed, workflow is marked as completed.

Let schedule the BookHolidaysWorkflow further by including the user choice. e.g. some user my choose to just book hotels while some may book both hotels and flight.

    [WorkflowDescription("1.0")]
    public class BookHolidaysWorkflow : Workflow
    {
      public BookHolidaysWorkflow()
      {
        ScheduleActivity<BookRoom>().When(_=>Input.BookHotel);
    	ScheduleActivity<AddDinner>().AfterActivity<BookRoom>();
    	
	ScheduleActivity<BookFlight>().When(_=>Input.BookFlight);
	ScheduleActivity<ChooseSeat>().AfterActivity<BookFlight>();
		
	ScheduleActivity<ChargeCustomer>().AfterActivity<AddDinner>()
	    .AfterActivity<ChooseSeat>();
    	ScheduleActivity<SendConfirmation>().AfterActivity<ChargeCustomer>();
      }
    }

Things to note in above example:

  • BookRoom or BookFlight can schedule
Clone this wiki locally