Use the database to save the status of executions, it will serve not only to show the user what is happening, but also the execution history.
Create a Table (something like Processoexecucaostatus) where you can have a Description field and another Status (I always have an ID field that is a primary key with auto-increment, makes the control very objective and easy to know which record I am working on).
The description field can be text to describe the routine that was executed and the status field controls whether it was executed or not and whether it was successful.
In the service, change the method you run daily (in our Example, let’s call Rotinadiariabackup) to at each stage of the Method perform the status update in the database.
Example:
void RotinaDiariaBackup()
{
try
{
rep.InsereStatusProcessoIniciado();
this.ExecutaBackup(); //este metodo é a sua rotina (aquilo que o serviço ja executa de tempos em tempos.
rep.InsereStatusProcessoExecutadoSucesso();
}
catch(Exception)
{
rep.InsereStatusProcessoExecucaoFalha();
}
}
Logically, there are some flaws in this method, from many points of view so keep in mind the design of the solution, not this code itself.
You should better consider error handling, consider that the methods you enter into the database may also fail (one should have an alternate error log for tracking - a txt or windows log).
But, in general, let’s explain the logic:
rep = repository for access to the database that stores the process data, to simplify, I did not create a process objectExecucao, but it is highly recommended as you may need to represent more than the status and description.
Therefore, the methods would receive an instance of Processoexecucao (that does not yet exist) to carry out the actions.
insertion.(); = basically this snippet inserts in the database a row in the table Processoexecucaostatus with the description you want as default and status (say status = 0 is running in progress).
rep.Inserts status Executed process(); = Update the row you have now entered by updating the status of the table to success (let’s say Status = 1).
rep.Inserts status Run failed(); = If one of the previous methods fails, an Exception will be launched and you update the status record to 2 (status with error).
You see, it should be noted that other methods may fail that are not your routine (the successful update may fail), so you should think about how to control this flow (transaction?)
After the modifications in the service, implement a screen that shows the records of the table Processoexecucaostatus and you will have the status of what was executed and also of what is being executed, because the screen only shows the records.
This control table approach is quite common, however, there are some problems:
the status of the table is not the status of the service, so if the service fails for any reason other than routine, the screen does not show this.
The screen shows a photo at the time of what was inserted, there is no communication between the processes, so it does not show the status of the service at that instant, shows the last record change that was possible to do, if you intend to use the screen to monitor the service, not that way, will have to implement another form of access to the service itself (which is usually a little more complex).
Performance of the database, currently your service has no connection to the database, performing fewer requests and as a consequence, the database (which may be a simple machine) does not suffer from it, you will be going to the bank periodically (both to read to the screen, and to perform their routines).
Please don’t make one Select * from Processxecucaostatus on the screen, put a conditional or a top, with time the table will grow. This solves itself that you correctly implement the repository and access the screen of the same classes treating the service as if it were a GUI, but, not everyone thinks so, many do not have layers or treat as another mini-system.
Finally, if you need to include new routines in this systematic, just put a Typorotin column in the table and create a new Method for the new routine, following the same structure and the screen will already reflect the new status, it is easy to maintain and expand, something like this:
void RotinaDiariaEmails()
{
try
{
int tipoRotina = 1; // 1 = envio diario de emails - 0 = backup
rep.InsereStatusProcessoIniciado(tipoRotina);
this.ExecutaEnvioEMails(); //este metodo é a sua rotina
rep.InsereStatusProcessoExecutadoSucesso(tipoRotina);
}
catch(Exception)
{
rep.InsereStatusProcessoExecucaoFalha(tipoRotina);
}
}
In the service, you would call the methods in sequence or have another queue structure that executes the methods:
void StartSomeTimes()
{
RotinaDiariaEmails();
RotinaDiariaBackup();
}
You probably have a timer on your service that runs from time to time, you would call Startsometimes in the body of the method that is run by the timer.
Do you need to show the status of what’s happening in the service? You need to save the history of what was executed to log?
– Intruso
@Intruso Look, I hadn’t thought about the status of the service. But it’s a good one. Logs yes, I think about saving. What I need initially is to know how to do this. So what I can research. Initially showing the way, would already be an excellent help =)
– user37440