public static bool OnDemandRequest(IXenConnection connection, Session session)
        {
            Dictionary <string, string> config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);

            if (BoolKey(config, HealthCheckSettings.STATUS) == false)
            {
                log.InfoFormat("Will not report on demand for XenServer {0} that was not Enroll", connection.Hostname);
                return(false);
            }

            //Check if there already some service doing the uploading already
            if (CanLock(Get(config, HealthCheckSettings.UPLOAD_LOCK), true) == false)
            {
                log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname);
                return(false);
            }

            var newUploadRequest = Get(config, HealthCheckSettings.NEW_UPLOAD_REQUEST);

            if (!string.IsNullOrEmpty(newUploadRequest))
            {
                DateTime newUploadRequestTime;
                try
                {
                    newUploadRequestTime = HealthCheckSettings.StringToDateTime(newUploadRequest);
                }
                catch (Exception exn)
                {
                    log.Error("Exception while parsing NEW_UPLOAD_REQUEST", exn);
                    return(false);
                }
                DateTime newUploadRequestDueTime = newUploadRequestTime.AddMinutes(HealthCheckSettings.UPLOAD_REQUEST_VALIDITY_INTERVAL);
                if (DateTime.Compare(newUploadRequestDueTime, DateTime.UtcNow) >= 1)
                {
                    log.InfoFormat("Will report on demand for XenServer {0} since the demand was requested on {1} (UTC time)", connection.Hostname, newUploadRequestTime);
                    return(getLock(connection, session));
                }
                else
                {
                    log.InfoFormat("Will not report on demand for XenServer {0} since the demand requested on {1} (UTC time) expired after {2} minutes",
                                   connection.Hostname, newUploadRequestTime, HealthCheckSettings.UPLOAD_REQUEST_VALIDITY_INTERVAL);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        public static bool Request(IXenConnection connection, Session session)
        {
            bool needRetry = false;
            Dictionary <string, string> config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);

            if (BoolKey(config, HealthCheckSettings.STATUS) == false)
            {
                ServerListHelper.instance.removeServerCredential(connection.Hostname);
                log.InfoFormat("Will not report for XenServer {0} that was not Enroll", connection.Hostname);
                return(false);
            }
            //Check if there already some service doing the uploading already
            if (CanLock(Get(config, HealthCheckSettings.UPLOAD_LOCK), false) == false)
            {
                log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname);
                return(false);
            }

            //No Lock has been set before, Check upload is due
            int      intervalInDays       = IntKey(config, HealthCheckSettings.INTERVAL_IN_DAYS, HealthCheckSettings.DEFAULT_INTERVAL_IN_DAYS);
            DateTime lastSuccessfulUpload = DateTime.UtcNow;
            bool     haveSuccessfulUpload = false;

            if (config.ContainsKey(HealthCheckSettings.LAST_SUCCESSFUL_UPLOAD))
            {
                try
                {
                    lastSuccessfulUpload = HealthCheckSettings.StringToDateTime(Get(config, HealthCheckSettings.LAST_SUCCESSFUL_UPLOAD));
                    haveSuccessfulUpload = true;
                }
                catch (Exception exn)
                {
                    log.Error("Catch exception when Parse LAST_SUCCESSFUL_UPLOAD", exn);
                }
            }

            if (haveSuccessfulUpload)
            {//If last successful update not due. return
                if (DateTime.Compare(DateTime.UtcNow, lastSuccessfulUpload.AddDays(intervalInDays)) < 0)
                {
                    log.InfoFormat("Will not report for XenServer {0} that was not due {1} days", connection.Hostname, intervalInDays);
                    return(false);
                }
            }

            if (config.ContainsKey(HealthCheckSettings.LAST_FAILED_UPLOAD))
            {
                try
                {
                    DateTime LastFailedUpload = HealthCheckSettings.StringToDateTime(Get(config, HealthCheckSettings.LAST_FAILED_UPLOAD));

                    if (haveSuccessfulUpload)
                    {
                        if (DateTime.Compare(lastSuccessfulUpload, LastFailedUpload) > 0)
                        {
                            return(false); //A retry is not needed
                        }
                    }

                    int retryInterval = IntKey(config, HealthCheckSettings.RETRY_INTERVAL, HealthCheckSettings.DEFAULT_RETRY_INTERVAL);
                    if (DateTime.Compare(LastFailedUpload.AddDays(retryInterval), DateTime.UtcNow) <= 0)
                    {
                        log.InfoFormat("Retry since retryInterval{0} - {1} > {2} meeted", LastFailedUpload, DateTime.UtcNow, retryInterval);
                        needRetry = true;
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch (Exception exn)
                {
                    log.Error("Catch exception when check if retry was needed", exn);
                }
            }

            DateTime currentTime = DateTime.Now;

            if (!needRetry)
            {//Check if uploading schedule meet only for new upload
                DayOfWeek dayOfWeek;
                if (!Enum.TryParse(Get(config, HealthCheckSettings.DAY_OF_WEEK), out dayOfWeek))
                {
                    log.Error("DAY_OF_WEEK not existed");
                    return(false);
                }
                if (!config.ContainsKey(HealthCheckSettings.TIME_OF_DAY))
                {
                    log.Error("TIME_OF_DAY not existed");
                    return(false);
                }

                int TimeOfDay = IntKey(config, HealthCheckSettings.TIME_OF_DAY, HealthCheckSettings.GetDefaultTime());
                if (currentTime.DayOfWeek != dayOfWeek || currentTime.Hour != TimeOfDay)
                {
                    log.InfoFormat("Will not report for XenServer {0} for incorrect schedule", connection.Hostname);
                    return(false);
                }
                log.InfoFormat("Upload schedule for {0} is {1}:{2}, meet current time {3}", connection.Hostname, dayOfWeek, TimeOfDay, currentTime.ToString());
            }
            return(getLock(connection, session));
        }