Best way to repeat a process and continue even if it fails

110 views Asked by At

I have a program that restores databases. Basically the program reads from a file and restores one database at a time. The program runs 1 time per database. I need a way to repeat this until all databases are restored. Even if the database restore isn't successful, i need the process to repeat until all databases are attempted to be restored. I am thinking of having another program call the program I just mentioned. So something like this:

    public const string databaseAttemptedRestore = "C:\\Logs\\AttemptedDatabaseRestore.log";
    public const string databasesToRestore = "C:\\Logs\\DatabasesToRestore.log";
    static void Main(string[] args)
    {

        //Repeats restore, reading from databasesToRestore. Needs to be called by task scheduler
        Action toRepeat = () =>
        {
            var proc = new Process();

          //database restore program
            proc.StartInfo.FileName = @"C:\Projects\DbRestoreProdConsole\bin\Debug\DbRestoreProdConsole.exe";

            // set up output redirection

            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.EnableRaisingEvents = true;
            proc.StartInfo.CreateNoWindow = true;

            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.WorkingDirectory = @"C:\Windows\SysWOW64";

            // see below for output handler

            proc.Start();
            proc.WaitForExit();
        };
        double lineCount = File.ReadLines(databasesToRestore).Count();
        double repeat = Math.Ceiling(lineCount / 2);
        Enumerable.Range(0, (int)repeat).ToList().ForEach(i => toRepeat());


    }
}

}

I am trying to figure out the best solution to this repeating process where I can track errors and run the program smooth etc. The program runs after hours so it doesn't need to be a fast running program. The most databases I will restore per night is around 8. but it usually will be 1 or 2 per night. Any suggestions is greatly appreciated. Thank you.

0

There are 0 answers