I have a service that use a worker to run a periodic task.
I will not use a concrete language because it is a generic question, so I will expose the general idea.
MainProgram
{
Worker.Start();
wait worker;
}
Worker
{
Execute(cancelationToken)
{
try
{
while(!CanelationToken)
{
//do the task;
wait(1000);
}
}
catch
{
_log("Some exception");
}
}
}
In the worker, I handle the exception to write in a file the problem and I don't stop the worker, I want it continues with the next iteration.
Also I would like to send an email with the error, but I am thinking where it would be the better place to send the email.
One option is in the worker, in the catch, after log the error, but perhaps it is much work than the worker should do and it would be better to handle in the main of the application, perhaps it is more a matter of the application than the core of the application.
But for that, I guess I would need to implement an event to notify from the worker to he main the exception, buecause I don't want to stop the service, I want to continue to try in the next iteration.
But this makes me to to think, if to send an email in the worker is not the best place because it is not the main responsability of the core of the worker, perhaps to log the exception it is not the best place too and perhaps it should be done in the main aplication, in the same plaec where I send the email.
So my doubt is how to struct the code, if it is better to leave the worker as light as possible or it is not a bad idea to send the email and log the exception in the worker because really it is its responsability?
Thanks.