Dealing with Cancelation of Tasks, Builds and Activities in TFS.

Rafael Nunes
4 min readMar 30, 2016

--

Some conclusions retrieved from my recent odyssey trying to cancel a build running in TFS. Which does not mean that I’m 100% correct, but…

The question is:

How cancel correctly a MSBuild process? What about the children processes of it?

First things first, if you are working on MSBuild in TFS you have to know that:

  • Cancelling the build via TFS interface by clicking in stop, the TFS will execute an End Process in the MSBuild process. This behaviour cannot be override.
  • Microsoft itself does not guarantee that when you stop a MSBuild process its children will be also killed.
  • The End Process only kills the root process, ideally we should want an End Tree Process this way killing also the children of the root process.

Cancelation in Task level

A Task in MSBuild is an operation to be performed by the MSBuild process, you can implement and pass it to be executed as an argument of MSBuild. You can create Tasks to build other projects, execute some stuff and you can even create Custom Tasks programmatically.

To deal with cancelation in a Task level you can create your custom Task which should implement ICancelableTask. This will provide you the implementation of a Cancel() function which will describe the behaviour of this Task when it is canceled. I tried to kill the remaining children processes of the MSBuild… However this only will work when you are dealing inside de MSBuild cancel, which means, another Task trigged the cancel or the execution of the program lead to a cancel of the Task.

Even when you implement your Task using ICancelableTask if the MSBuild was canceled by TFS interface or other type of End Process action your Task won’t trigger the Cancel() behaviour. Because the process itself has been stopped.

Sometimes would be useful to cancel a Task after the some other Task output.

Cancelation in Activity level

An Activity in TFS is an operation to be performed in the Workflow (Example of database app here) of the MSBuild process. They are building blocks normally used to prepare the environment, start the MSBuild process, save output, run tests, clean environment etc.

Activities are pretty much useful to perform those operations mentioned above, click here for a list of them. To deal with cancelation of the TFS Build process some of them already support the Cancellation Scope. Cancellation Scope is an Activity where you can encapsulate any work and perform a set of Activities in its HandleCancel which means you can do whatever you want when those Activities are canceled.

The problem is the RunMSBuild Activity (the main activity for running programs and application) does not support CancelationScope, then again, if you stop the build on TFS nothing will be performed, in this case by the Handler. If any Activity does not support CancelationScope, but it is inside of it, when you cancel the log will present you an alert showing that CancelationScope was aborted by timeout (It tried to perform the behaviour of cancelation, but none was founded).

Well, if the Activities that MS provides you are enough for your needs and they work fine with CancelationScope that’s awesome! If not, you can always customize and implement yourself a CustomActivity. These links can be useful if you need to create an Activity:

Then you can customize and create your own Activity that holds CancelationScope. I truly recommend you the read of Understanding Cancellation, really nice article that explain the Activities behaviour, types of Activities as Activity, CodeActivity, AsycnCodeActivity or NativeActivity, and what happens when you cancel each one of them.

Some goods other Activities

You don’t need to implement all the job in Activities, Tasks, c# programs… You are always able to Run Scripts Activities for doing part of the job for you, for example a PowerShell Activity that kill processes for you or do some stuff. Run Scripts is a useful Activity in TFS Workflow.

A Try/Catch Activity is nice to encapsulate some job that can thrown an exception, then you can perform anything in the HandleException scope and in the Finally Scope.

Summarising

  • Understand where is your problem to perform the correct solution.
  • Use ICancelableTask if you just need to hold a cancelation between Tasks.
  • Use CancelationScope to encapsulate your job if you need to hold cancelation in a Activity level. Create your own Activity if it is necessary.

Finally I recommend this reading: http://coldwatersoftware.com/msbuild-versus-tfs-workflow-code-Activity/, when you really need a MSBuild or a Activity in your workflow it might be useful.

I hope this post will be helpful to someone one day haha.

Cya o/

--

--

Rafael Nunes
Rafael Nunes

Written by Rafael Nunes

Software Engineer @ Airtasker | casual blog post writer & open sourcerer https://peaonunes.com

No responses yet