views:

210

answers:

1

I want my timer job to display failed status on certain condition. Should I just throw an exception or what?

+1  A: 

Hasan,

If an exception bubbles out of the code, it will indeed show a failed status. If you want to explicitly "fail" your run with the timer job instance, you can and should throw an exception out. This can be done in conjunction with setting the SPJobDefinition.Retry flag value to TRUE if you feel that another run through would get around your "problem condition."

By the same token, if you're trapping exceptions for logging purposes (e.g., to write additional detail out to the ULS logs -- never a bad idea), rethrow any exceptions you catch unless you truly compensate for them. You want to avoid eating exceptions just to avoid having exceptions bubble out for the reasons stated (exception status is tied to job success/failure reporting).

I hope that helps!

Sean McDonough
Btw do jobs with OneTimeSchedule disappear from job status page on failure? If so how would user know that the job failed?
Hasan Khan
Excellent question, Hasan. It's been a while since I've written a one-shot timer job (the nav update job I wrote for this Feature was the last one: http://blobcachefarmflush.codeplex.com), but I seem to recall that failed jobs do sit in the status list - at least for some period of time. If memory serves me correctly, it didn't stay long -- I had to more or less poll for it (by refreshing the status page) before it disappeared.
Sean McDonough