private void ServiceCannotBeScheduled(ServiceInstance request)
        {
            try
            {
                AsLockable(request).Unlock(_instanceLock);

                WriteLog(String.Format("Request '{0}' cannot be scheduled and its instance is aborted", request.DebugInfo()),
                         LogMessageType.Warning);
                request.SchedulingInfo.SchedulingStatus = SchedulingStatus.CouldNotBeScheduled;
                request.Abort();
            }
            catch (Exception ex)
            {
                WriteLog(String.Format("Failed to set service as unscheduled, ex: {0}", ex.Message), ex);
            }
            finally
            {
                // add to update UI with requests that cannot be scheduled, this request will be removed on the next Schedule() by RemoveNotRelevantRequests()
                _scheduledRequests.Add(request);
                AsLockable(request).Lock(_instanceLock);
            }
        }
        private void ScheduleServiceInstance(ServiceInstance serviceInstance, TimeSpan avgExecutionTime)
        {
            AsLockable(serviceInstance).Unlock(_instanceLock);
            serviceInstance.SchedulingInfo.SchedulingStatus = SchedulingStatus.Scheduled;
            serviceInstance.StateChanged += Instance_StateChanged;
            AsLockable(serviceInstance).Lock(_instanceLock);

            // set service instance max execution time
            serviceInstance.Configuration.Limits.MaxExecutionTime = TimeSpan.FromMilliseconds(avgExecutionTime.TotalMilliseconds * Configuration.MaxExecutionTimeFactor);

            if (!_scheduledRequests.ContainsSignature(serviceInstance))
            {
                WriteLog(String.Format("Move unscheduled request to scheduled list '{0}'", serviceInstance.DebugInfo()));
                _scheduledRequests.Add(serviceInstance);
                _unscheduledRequests.Remove(serviceInstance);
            }
            else
            {
                WriteLog(String.Format("Warning! Request '{0}' already exists in scheduled list", serviceInstance.DebugInfo()), LogMessageType.Warning);
            }
        }
        private void AddRequestToSchedule(ServiceInstance request)
        {
            try
            {
                // add scheduling info if not exists
                if (request.SchedulingInfo == null)
                {
                    AsLockable(request).Unlock(_instanceLock);
                    request.SchedulingInfo = new SchedulingInfo
                    {
                        SchedulingStatus = SchedulingStatus.New,
                        RequestedTime    = DateTime.Now,
                        SchedulingScope  = SchedulingScope.Unplanned
                    };
                    AsLockable(request).Lock(_instanceLock);
                }

                WriteLog(String.Format("Add unplanned request to unscheduled list '{0}'", request.DebugInfo()));
                lock (_unscheduledRequests)
                {
                    _unscheduledRequests.Add(request);
                }
                _needReschedule = true;
            }
            catch (Exception ex)
            {
                WriteLog(String.Format("Failed add unplanned request {0} to scheduler, ex: {1}", request.InstanceID, ex.Message), ex);
            }
        }