/*=========================*/
        #endregion

        #region Private Methods
        /*=========================*/

        /// <summary>
        /// Create a service object by the function parameters.
        /// </summary>
        /// <param name="serviceElement"></param>
        /// <param name="serviceInstanceType"></param>
        /// <param name="serviceTimeScheduled"></param>
        /// <returns></returns>
        private ServiceInstance CreateNewService(AccountServiceElement accountServiceElement,
                                                 DateTime serviceTimeScheduled,
                                                 SchedulingRuleElement activeSchedulingRule, int accountID)
        {
            //// Create new service list if needed.
            //if (!_scheduleTable.ContainsKey(accountServiceElement.Uses.Element.Name))
            //{
            //    _scheduleTable.Add(accountServiceElement.Uses.Element.Name, new ServiceList(accountServiceElement.Uses.Element));
            //}
            ServiceInstance service;

            try
            {
                service = Service.CreateInstance(accountServiceElement, accountID);
            }
            catch (Exception ex)
            {
                Log.Write(String.Format("Exception occured while performing CreateInstance.", accountServiceElement.Uses.Element != null ? accountServiceElement.Uses.Element.Name : string.Empty), ex);
                return(null);
            }

            service.StateChanged          += _stateChangedHandler;
            service.OutcomeReported       += _outcomeReportedHandler;
            service.ChildServiceRequested += _childServiceRequestedHandler;
            service.ActiveSchedulingRule   = activeSchedulingRule;
            service.TimeScheduled          = serviceTimeScheduled;

            return(service);
        }
Exemplo n.º 2
0
        public bool AddToSchedule(string serviceName, int accountID, DateTime targetTime, Edge.Core.SettingsCollection options)
        {
            AccountElement account = EdgeServicesConfiguration.Current.Accounts.GetAccount(accountID);

            if (account == null)
            {
                Log.Write(Program.LS, String.Format("Ignoring AddToSchedule request for account {0} which does not exist.", accountID), LogMessageType.Warning);
                return(false);
            }

            AccountServiceElement service = account.Services[serviceName];

            if (service == null)
            {
                Log.Write(Program.LS, String.Format("Ignoring AddToSchedule request for service {0} which does not exist in account {1}.", serviceName, accountID), LogMessageType.Warning);
                return(false);
            }

            var             activeServiceElement = new ActiveServiceElement(service);
            ServicePriority priority             = activeServiceElement.Options.ContainsKey("ServicePriority") ?
                                                   (ServicePriority)Enum.Parse(typeof(ServicePriority), activeServiceElement.Options["ServicePriority"]) :
                                                   ServicePriority.Normal;

            AddToSchedule(activeServiceElement, account, targetTime, options, priority);
            return(true);
        }
        /// <summary>
        /// Check the service rule and add it if needed.
        /// </summary>
        /// <param name="serviceElement"></param>
        /// <remarks>
        /// We schedule the table for next day.
        /// For example on sunday 23:00 we schedule the table for monday.
        /// </remarks>
        /// <param name="childService">if we add a service request this service is the requested child service to schedule.</param>
        public void AddServicesByRules(AccountServiceElement accountServiceElement, int accountID, ServiceInstance childService)
        {
            ActiveServiceElement element = new ActiveServiceElement(accountServiceElement);

            //TODO: check if there is parent to the service

            foreach (SchedulingRuleElement rule in element.SchedulingRules)
            {
                // TODO: to check if there are same scheduling rules to account and service.
                HandleRule(accountServiceElement, accountID, childService, rule);
            }
        }
Exemplo n.º 4
0
        public bool AddToSchedule(string serviceName, int accountID, DateTime targetTime, SettingsCollection options)
        {
            // YANIV:
            // if cannot perform at requested time, or can only perform after max deviation expires -
            // throw exception with a message

            ActiveServiceElement startupElement = null;

            if (accountID < 0)
            {
                AccountServiceElement elem = ServicesConfiguration.SystemAccount.Services[serviceName];
                if (elem != null)
                {
                    startupElement = new ActiveServiceElement(elem);
                }
                else
                {
                    startupElement = new ActiveServiceElement(ServicesConfiguration.Services[serviceName]);
                }
            }
            else
            {
                AccountElement account = ServicesConfiguration.Accounts.GetAccount(accountID);
                if (account == null)
                {
                    // EXCEPTION:
                    return(false);
                    //throw new ArgumentException(
                    //    String.Format("Account with ID {0} does not exist.", accountID),
                    //    "accountID");
                }

                AccountServiceElement accountService = account.Services[serviceName];
                if (accountService == null)
                {
                    // EXCEPTION
                    // throw new ArgumentException(String.Format("Service {0} is not defined for account {1}", serviceName, account.Name), "serviceName");
                    return(false);
                    // Log.Write(String.Format("Service {0} is not defined for account {1}", serviceName, account.Name), LogMessageType.Warning);
                }
                else
                {
                    startupElement = new ActiveServiceElement(accountService);
                }
            }


            // Couldn't find exception
            if (startupElement == null)
            {
                // EXCEPTION:
                return(false);
                //throw new ArgumentException(
                //    String.Format("The requested service \"{0}\" could not be found.", serviceName),
                //    "serviceName");
            }

            // Merge the options
            if (options != null)
            {
                foreach (KeyValuePair <string, string> pair in options)
                {
                    startupElement.Options[pair.Key] = pair.Value;
                }
            }

            // Add the manual request.
            if (targetTime < DateTime.Now)
            {
                _builder.AddManualRequest(startupElement, accountID);
            }
            else
            {
                _builder.AddManualRequest(startupElement, targetTime, accountID);
            }

            return(true);
        }
        /// <summary>
        /// Add the service to scheduleTable.
        /// </summary>
        /// <remarks>We schedule the table for next day.
        /// For example on sunday 23:00 we schedule the table for monday.</remarks>
        /// <param name="serviceElement">The service to add</param>
        /// <param name="rule">The specific rule of the service that will add for this service instance.</param>
        /// <param name="childService">If we add a service request this service is the requested child service to schedule.</param>
        //      private void AddServiceRule(AccountServiceElement accountServiceElement,
        //	SchedulingRuleElement rule, ServiceInstance childService, int accountID)
        private void AddServiceRule(AccountServiceElement accountServiceElement,
                                    SchedulingRuleElement rule, ServiceInstance childService, int accountID)
        {
            ServiceInstance service;
            DateTime        serviceScheduledTime;

            // ExactTimes case
            if (rule.ExactTimes.Length > 0)
            {
                foreach (TimeSpan hour in rule.ExactTimes)
                {
                    // Convet TimeSpan to datetime.
                    // We schedule the table for next day - we add 1 day.

                    if (!rule.NextDay)
                    {
                        serviceScheduledTime = DateTime.Today;
                        serviceScheduledTime = serviceScheduledTime.Add(hour);

                        // The service pass is deviation time.
                        if ((rule.MaxDeviation.TotalMilliseconds > 0) &&
                            (DateTime.Now - serviceScheduledTime > rule.MaxDeviation))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        serviceScheduledTime = DateTime.Today.AddDays(1);
                        serviceScheduledTime = serviceScheduledTime.Add(hour);
                    }

                    if (childService == null)
                    {
                        service = CreateNewService(accountServiceElement, serviceScheduledTime, rule, accountID);
                    }
                    else
                    {
                        service = childService;
                    }

                    // Add the service to the schedule table.
                    if (service != null)
                    {
                        _scheduleTable.AddService(service);
                    }

                    // Relevent for service request which have rules that were
                    // suppose to be shceduled eariler.
                    if (serviceScheduledTime < DateTime.Now)
                    {
                        // TODO: maybe add this log to error file.
                        // TODO: might need more info on the log like service runtime ID
                        //Log.Write(String.Format("The service {0} of account {1} can't schedule the service cause its scheduled for eariler time."
                        //     , accountServiceElement != null ? accountServiceElement.Uses.Element.Name : childService.Configuration.Name,
                        //     accountServiceElement != null ? string.Empty : childService.AccountID.ToString()),
                        //     LogMessageType.Warning);

                        continue;
                    }
                }
            }
            else // Frequency case
            {
                // Get last run from the DB for this service.
                DateTime lastServiceRun = GetLastServiceRun
                                              (accountServiceElement != null ? accountServiceElement.Uses.Element.Name : childService.Configuration.Name);

                // Init time with the end of the next day.
                DateTime nextDayEnd = DateTime.Today.AddDays(2).AddTicks(-1);

                if (lastServiceRun.AddMinutes(rule.Frequency.TotalMinutes) > DateTime.Now)
                {
                    serviceScheduledTime = lastServiceRun.AddMinutes(rule.Frequency.TotalMinutes);
                }
                else
                {
                    serviceScheduledTime = DateTime.Now;
                }

                // We keep to schedule sevices till we pass the current day.
                while (serviceScheduledTime < nextDayEnd)
                {
                    if (childService == null)
                    {
                        service = CreateNewService(accountServiceElement, serviceScheduledTime, rule, accountID);
                    }
                    else
                    {
                        service = childService;
                    }

                    // Add the service to the schedule table.
                    if (service != null)
                    {
                        _scheduleTable.AddService(service);
                    }

                    // Increase time with Frequency value.
                    serviceScheduledTime = serviceScheduledTime.AddMinutes(rule.Frequency.TotalMinutes);
                }
            }
        }
        /// <summary>
        /// Create a service According to the service rule.
        /// </summary>
        /// <param name="accountServiceElement"></param>
        /// <param name="accountID"></param>
        /// <param name="childService"></param>
        /// <param name="rule"></parammmmm>
        private void HandleRule(AccountServiceElement accountServiceElement, int accountID, ServiceInstance childService, SchedulingRuleElement rule)
        {
            switch (rule.CalendarUnit)
            {
            case CalendarUnit.ReRun:
                if (ScheduleConvertor.CheckFullSchedule(rule.FullSchedule))
                {
                    AddServiceRule(accountServiceElement, rule, childService, accountID);
                }

                break;

            // Month
            case CalendarUnit.Month:
                // Loop on all the days values in the rule.
                foreach (int day in rule.SubUnits)
                {
                    if (day == DateTime.Now.Day)
                    {
                        AddServiceRule(accountServiceElement, rule, childService, accountID);
                    }
                }
                break;

            // Week
            case CalendarUnit.Week:
                // Loop on all the days values in the rule.
                foreach (int day in rule.SubUnits)
                {
                    // DayOfWeek return values of 0-6 and because of it we -1 on left side.
                    if (day == (int)DateTime.Now.DayOfWeek + 1)
                    {
                        AddServiceRule(accountServiceElement, rule, childService, accountID);
                    }
                }
                break;

            // Day
            case CalendarUnit.Day:
                AddServiceRule(accountServiceElement, rule, childService, accountID);
                break;

            // AlwaysOn
            case CalendarUnit.AlwaysOn:

                ServiceInstance service;
                if (childService == null && _firstRun)
                {
                    try
                    {
                        service = CreateNewService(accountServiceElement, DateTime.Now, rule, accountID);
                    }
                    catch (Exception ex)
                    {
                        Log.Write(string.Format("Can't add the always on service {0} for accoutID {1}", accountServiceElement.ToString(), accountID.ToString()), ex, LogMessageType.Error);
                        return;
                    }
                }
                else
                {
                    service = childService;
                }

                // Add the service to the scheduleTable.
                if (service != null)
                {
                    _scheduleTable.AddService(service);
                }
                break;

            // Never should be here
            default:
                Log.Write(String.Format("The service {0}  don't have calendar unit, can't schedule the service."
                                        , accountServiceElement != null ? accountServiceElement.Uses.Element.Name : childService.Configuration.Name), LogMessageType.Warning);
                break;
            }
        }
 /// <summary>
 /// Check the service rule and add it if needed.
 /// </summary>
 /// <param name="serviceElement"></param>
 //public void AddServicesByRules(ServiceElement serviceElement,int accountID)
 public void AddServicesByRules(AccountServiceElement accountServiceElement, int accountID)
 {
     AddServicesByRules(accountServiceElement, accountID, null);
 }