# Stopping a graph midway

**URL:** <https://forum.cloverdx.com/t/stopping-a-graph-midway/542>\
**Category:** CloverDX Platform\
**Created:** [June 25, 2009, 12:00am UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542 "2009-06-25T00:00:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasoncoeho](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@jasoncoeho](https://forum.cloverdx.com/u/jasoncoeho)\
**Post date:** [June 25, 2009, 12:00am UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/1 "2009-06-25T00:00:00Z")

</div>

I have a graph where I read data from a file. If there is no data read - I would like to stop execution of the remainder of the graph. Any ideas on how to do this ?

---

<div class="post-metadata">

**Author:** ![avackova](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@avackova](https://forum.cloverdx.com/u/avackova)\
**Post date:** [June 26, 2009, 9:09am UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/2 "2009-06-26T09:09:27Z")

</div>

Hello,  
you should check the file before running the graph and, if it is empty, don’t run such graph at all.  
But if you persist on aborting graph by itself, it can be done in Reformat component. See following example class you can use in Reformat component:

```auto
import org.jetel.component.DataRecordTransform;
import org.jetel.data.DataRecord;
import org.jetel.exception.TransformException;

public class StopGraph extends DataRecordTransform {

	int recNo=0;
	
	public int transform(DataRecord[] arg0, DataRecord[] arg1)
			throws TransformException {
		defaultTransform(arg0, arg1);
		recNo++;
		return ALL;
	}

	public void finished() {
		if (recNo == 0) {
		   System.err.println("There is no data. Killing the graph.");
			getGraph().getWatchDog().abort();
		}
	}
}

```

In transform method you just copy input record to output record and in finish method you kill the running graph. The Reader and Reformat should be in earlier phase than the rest of graph. Note that such solution make your graph slower for not empty files while you have to split the graph into the phases and add one more Reformat.

---

<div class="post-metadata">

**Author:** ![mukunku](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mukunku](https://forum.cloverdx.com/u/mukunku)\
**Post date:** [November 9, 2017, 3:07pm UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/3 "2017-11-09T15:07:32Z")

</div>

Thanks for the abort logic:

```auto
getGraph().getWatchDog().abort();

```

There should definitely be an **Abort** component by default that does this

---

<div class="post-metadata">

**Author:** ![imriskal](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@imriskal](https://forum.cloverdx.com/u/imriskal)\
**Post date:** [November 10, 2017, 7:23am UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/4 "2017-11-10T07:23:31Z")

</div>

We actually do have a component called Fail now. It requires at least one record flowing into it to work but this can be worked around by using a dummy record.

---

<div class="post-metadata">

**Author:** ![mukunku](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mukunku](https://forum.cloverdx.com/u/mukunku)\
**Post date:** [November 10, 2017, 4:09pm UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/5 "2017-11-10T16:09:34Z")

</div>

Hi @imriskal,

Could a **status code** option be added to the fail component? Such as “End as Failed” or “End as Aborted”.

One example scenario I have is that I have a failed graph event listener that emails when something goes wrong. But there are times when I just want to abort the graph without triggering an “error”.

An option to just end the graph as “Success” would be helpful in this regard as well. Maybe an option to the existing **Success** component can be added that says “Cancel future phases and end graph after current phase finishes”.

---

<div class="post-metadata">

**Author:** ![Vladimir\_Barton](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.cloverdx.com/vladimir_barton/32/167_2.png) [@Vladimir\_Barton](https://forum.cloverdx.com/u/Vladimir_Barton)\
**Post date:** [November 30, 2017, 2:26pm UTC](https://forum.cloverdx.com/t/stopping-a-graph-midway/542/6 "2017-11-30T14:26:06Z")

</div>

Hi Mukunku,  
the fail component is designed in a way that it can abort the parent jobflow or graph with a user-specified error message and at the same time return some results through the dictionary. Having said that, you could use the Fail component for both purposes. Let me present you with an example:

- Adding a Status Code  
[list:o3t2v0sj][\*:o3t2v0sj]You would place Fail as a standalone component in the very last phase of your jobflow.

- You could assign a user-specified error message, “End as Aborted” for instance.

- This would help you distinguish between a jobflow that ended by this intentional user abort and a jobflow that finished with an actual error.  
[/\*Ⓜo3t2v0sj]

- Cancel future phases and end graph after current phase finishes  
[\*:o3t2v0sj]Let’s say you have at least 2 different phases in your jobflow.

- If you placed the Fail component in between those 2 existing phases, you could intentionally prevent the higher phase from being executed.  
[/\*Ⓜo3t2v0sj][/list:o:o3t2v0sj]  
Note: this could not be achieved by using the Success component as it does not function as a stopper and the graph/jobflow would simply continue to run.

Please also note that the aforementioned examples would answer just the basic questions. If they do not address your particular situation sufficiently, feel free to provide us with the graph in question so that we can get a better insight into the specific case. In similar situations, a minor redesign of a graph might do the trick.

Regards,
