Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was being troubled a lot by Robocopy(or maybe anti-virus or maybe Network Hardware). I copy files from the Dynamic View of Clearcase to the local machine for a fresh build. The copy would frequently fail due to:

2012/12/06 15:35:07 ERROR 64 (0x00000040)...

The specified network name is no longer available.

The server and all network hardware resides in another geographical location. So there is no way to ascertain the hardware issues. The anti-virus can never be disabled as per some policy. The Snapshot View cannot be used as per another weird policy or prejudice.

I am left with just one option. To make Robocopy resilient. The following is what I have come up with. This is a wrapper for Robocopy. Please review this.

SUMMARY:

1) Call Robocopy with parameters

2) Give it some time and certain number of tries for proper execution

3) First try is allowed 10 minutes. The subsequent tries will have an increment of 5 minutes. Maximum 5 tries and 30 minutes are allowed.

4) Catch all Robocopy error codes(0, 1 and 2 are success codes) and re-try.

namespace ResilientRobocopy
    {
        class Program
        {
            static int Main(string[] args)
            {
                string commandLine = string.Empty;
                int count = 0;
                foreach (string str in args)
                {
                    string temp = string.Empty;
                    if ((count == 0) || (count == 1))
                    {
                        temp = "\"" + str + "\"" + " ";
                    }
                    else
                    {
                        temp = str + " ";
                    }
                    commandLine = commandLine + temp;
                    count += 1;
                }
                Console.WriteLine("--------------------------------------------------------------------");
                Console.WriteLine("Robocopy Command Line: " + commandLine);

                int returnCode = -1;
                int tries = 0;

                while (((returnCode == -1) || (returnCode == -2)) && (tries != 5))
                {
                    Console.WriteLine("Calling Robocopy");
                    returnCode = StartCopy(commandLine, tries);
                    tries += 1;
                }
                Console.WriteLine("--------------------------------------------------------------------");
                return returnCode;
            }

            static int StartCopy(string commandLine, int tries)
            {
                Process robocopy = new Process();
                try
                {
                    robocopy = Process.Start("C:\\Windows\\SysWOW64\\robocopy.exe", commandLine);
                    int timeLimit = 120 + (60 * tries);
                    for (int i = 0; i <= timeLimit; i++)
                    {
                        Thread.Sleep(1000 * 5);
                        if (!robocopy.HasExited)
                        {
                            continue;
                        }
                        else
                        {
                            if (robocopy.ExitCode > 2)
                            {
                                Console.WriteLine("Robocopy exited with code: " + robocopy.ExitCode.ToString());
                                Console.WriteLine("Retrying...");
                                return -1;
                            }
                            else
                            {
                                Console.WriteLine("Robocopy exited with code: " + robocopy.ExitCode.ToString());
                                Console.WriteLine("Robocopy Done!");
                                return robocopy.ExitCode;
                            }
                        }
                    }

                    if (!robocopy.HasExited)
                    {
                        Console.WriteLine("Killing Robocopy. Took too much time. Try try again till you succeed...");
                        robocopy.Kill();
                        return -1;
                    }
                    else
                    {
                        if (robocopy.ExitCode > 2)
                        {
                            Console.WriteLine("Robocopy exited with code: " + robocopy.ExitCode.ToString());
                            Console.WriteLine("Retrying...");
                            return -1;
                        }
                        else
                        {
                            Console.WriteLine("Robocopy exited with code: " + robocopy.ExitCode.ToString());
                            Console.WriteLine("Robocopy Done!");
                            return robocopy.ExitCode;
                        }
                    }
                }

                catch(Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.ToString());
                    return -2;
                }
                finally
                {
                    robocopy.Close();
                }
            }
        }
    }
share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.