/// <summary>
        /// Get this time line services
        /// </summary>
        /// <param name="useCurrentTimeframe">if it's for reschedule then the time line is the same as the last schedule</param>
        /// <returns></returns>
        private IEnumerable <ServiceInstance> GetServicesInTimeframe(bool useCurrentTimeframe)
        {
            // Take next timeframe if false
            if (!useCurrentTimeframe)
            {
                _timeframeFrom = DateTime.Now;
                _timeframeTo   = DateTime.Now.Add(Configuration.Timeframe);
                WriteLog(String.Format("Current timeframe {0} - {1}", _timeframeFrom, _timeframeTo));
            }

            lock (_serviceConfigurationsToSchedule)
            {
                foreach (ServiceConfiguration configuration in _serviceConfigurationsToSchedule)
                {
                    foreach (SchedulingRule schedulingRule in configuration.SchedulingRules)
                    {
                        foreach (TimeSpan time in schedulingRule.Times)
                        {
                            DateTime requestedTime = (_timeframeFrom.Date + time);

                            while (requestedTime.Date <= _timeframeTo.Date)
                            {
                                if (IsRuleInTimeframe(schedulingRule, requestedTime))
                                {
                                    // create service instance, check if it already exists, if not return it
                                    ServiceInstance request = CreateServiceInstance(configuration, schedulingRule, requestedTime);
                                    if (request != null)
                                    {
                                        if (!_unscheduledRequests.ContainsSignature(request) && !_scheduledRequests.ContainsSignature(request))
                                        {
                                            yield return(request);
                                        }
                                    }
                                }
                                requestedTime = requestedTime.AddDays(1);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Get this time line services
        /// </summary>
        /// <param name="useCurrentTimeline">if it's for reschedule then the time line is the same as the last schedule</param>
        /// <returns></returns>
        private IEnumerable <ServiceInstance> GetServicesForTimeLine(bool useCurrentTimeline)
        {
            // Take next timeline if false
            if (!useCurrentTimeline)
            {
                _timeLineFrom = DateTime.Now;
                _timeLineTo   = DateTime.Now.Add(_neededScheduleTimeLine);
            }

            lock (_serviceConfigurationsToSchedule)
            {
                for (int i = 0; i < _serviceConfigurationsToSchedule.Count; i++)
                {
                    ServiceConfiguration configuration = _serviceConfigurationsToSchedule[i];

                    foreach (SchedulingRule schedulingRule in configuration.SchedulingRules)
                    {
                        bool ruleSuitable = false;
                        foreach (TimeSpan time in schedulingRule.Times)
                        {
                            DateTime requestedTime = (_timeLineFrom.Date + time).RemoveSeconds();

                            while (requestedTime.Date <= _timeLineTo.Date)
                            {
                                switch (schedulingRule.Scope)
                                {
                                case SchedulingScope.Day:
                                    ruleSuitable = true;
                                    break;

                                case SchedulingScope.Week:
                                    int dayOfWeek = (int)requestedTime.DayOfWeek + 1;
                                    if (schedulingRule.Days.Contains(dayOfWeek))
                                    {
                                        ruleSuitable = true;
                                    }
                                    break;

                                case SchedulingScope.Month:
                                    int dayOfMonth = requestedTime.Day;
                                    if (schedulingRule.Days.Contains(dayOfMonth))
                                    {
                                        ruleSuitable = true;
                                    }
                                    break;
                                }

                                if ((ruleSuitable) &&
                                    (requestedTime >= _timeLineFrom && requestedTime <= _timeLineTo) ||
                                    (requestedTime <= _timeLineFrom && (schedulingRule.MaxDeviationAfter == TimeSpan.Zero || requestedTime.Add(schedulingRule.MaxDeviationAfter) >= DateTime.Now))
                                    )
                                {
                                    ServiceInstance request = Environment.NewServiceInstance(configuration);
                                    request.SchedulingInfo = new SchedulingInfo()
                                    {
                                        SchedulingStatus   = SchedulingStatus.New,
                                        SchedulingScope    = schedulingRule.Scope,
                                        RequestedTime      = requestedTime,
                                        MaxDeviationBefore = schedulingRule.MaxDeviationBefore,
                                        MaxDeviationAfter  = schedulingRule.MaxDeviationAfter,
                                    };
                                    AsLockable(request).Lock(_instanceLock);

                                    lock (_scheduledRequests)
                                    {
                                        lock (_unscheduledRequests)
                                        {
                                            if (!(_unscheduledRequests.ContainsSignature(request) && _scheduledRequests.ContainsSignature(request)))
                                            {
                                                yield return(request);
                                            }
                                        }
                                    }
                                }
                                requestedTime = requestedTime.AddDays(1);
                            }
                        }
                    }
                }
            }
        }