using System;
using System.Threading;
using edu.jhu.Cas.Util;
namespace edu.jhu.Cas.Jobs {
    /**
     * this is for calls that cause BatchAdmin corruption if not completed succesfully.
     * this if for when it is worth having a job take an inordinate amount of time for completion, so long as the status of that job is correct.
     * these calls should be used sparingly.
     */
    public abstract class ToughJobs{
        public const int ATTEMPTS = 3;//number of times to attempt to change a jobs status
        public const int DELAY = 10000;//wait 10 seconds between tries
        
        public static void FinishedJob(int attemptNum, long wsid, long jobid, string message, long rows, string tab) {
            CasJobsAdmin ja = new CasJobsAdmin();
            try {
                ja.FinishedJob(wsid, jobid, message, rows, tab);
            } catch(Exception ex) {
                if (++attemptNum >= ATTEMPTS) throw new CasJobsException("Job " + jobid + " cannot currently be finished", ex);
                Thread.Sleep(DELAY);
                FinishedJob(attemptNum, wsid, jobid, message, rows, tab);
            }
        }
        public static void FailJob(int attemptNum, long wsid, long jobid, string message, string tab) {
            CasJobsAdmin ja = new CasJobsAdmin();
            try {
                ja.FailJob(wsid, jobid, message, tab);
            } catch (Exception ex){
                if (++attemptNum >= ATTEMPTS) throw;
                Thread.Sleep(DELAY);
                FailJob(attemptNum, wsid, jobid, message, tab);
            }
        }
    }
}