Esempio n. 1
0
        public bool AddToSchedule(string serviceName, int accountID, DateTime targetTime, Easynet.Edge.Core.SettingsCollection options)
        {
            bool respond = true;

            try
            {
                ActiveServiceElement activeServiceElement;
                ServiceConfiguration myServiceConfiguration = new ServiceConfiguration();
                ServiceConfiguration baseConfiguration      = new ServiceConfiguration();
                if (accountID == null || accountID == 0)
                {
                    throw new Exception("Account id must be set!");
                }

                if (ServicesConfiguration.Services[serviceName] == null)
                {
                    throw new Exception(string.Format("Service: {0} not exists in configuration", serviceName));
                }
                if (accountID == -1)
                {
                    activeServiceElement = new ActiveServiceElement(ServicesConfiguration.Services[serviceName]);
                }
                else
                {
                    activeServiceElement = new ActiveServiceElement(ServicesConfiguration.Accounts.GetAccount(accountID).Services[serviceName]);
                }
                if (options != null)
                {
                    foreach (string option in options.Keys)
                    {
                        activeServiceElement.Options[option] = options[option];
                    }
                }
                ServiceElement serviceElement = ServicesConfiguration.Services[serviceName];

                //base configuration;
                baseConfiguration.Name                    = serviceElement.Name;
                baseConfiguration.MaxConcurrent           = serviceElement.MaxInstances;
                baseConfiguration.MaxCuncurrentPerProfile = serviceElement.MaxInstancesPerAccount;


                //configuration per profile
                myServiceConfiguration      = new ServiceConfiguration();
                myServiceConfiguration.Name = activeServiceElement.Name;
                if (activeServiceElement.Options.ContainsKey("ServicePriority"))
                {
                    myServiceConfiguration.priority = int.Parse(activeServiceElement.Options["ServicePriority"]);
                }
                myServiceConfiguration.MaxConcurrent           = (activeServiceElement.MaxInstances == 0) ? 9999 : activeServiceElement.MaxInstances;
                myServiceConfiguration.MaxCuncurrentPerProfile = (activeServiceElement.MaxInstancesPerAccount == 0) ? 9999 : activeServiceElement.MaxInstancesPerAccount;
                myServiceConfiguration.LegacyConfiguration     = activeServiceElement;
                //        //scheduling rules
                myServiceConfiguration.SchedulingRules.Add(new SchedulingRule()
                {
                    Scope             = SchedulingScope.UnPlanned,
                    SpecificDateTime  = DateTime.Now,
                    MaxDeviationAfter = new TimeSpan(0, 0, 45, 0, 0),
                    Hours             = new List <TimeSpan>(),
                    GuidForUnplaned   = Guid.NewGuid()
                });
                myServiceConfiguration.SchedulingRules[0].Hours.Add(new TimeSpan(0, 0, 0, 0));
                myServiceConfiguration.BaseConfiguration = baseConfiguration;
                Profile profile = new Profile()
                {
                    ID       = accountID,
                    Name     = accountID.ToString(),
                    Settings = new Dictionary <string, object>()
                };
                profile.Settings.Add("AccountID", accountID);
                myServiceConfiguration.SchedulingProfile = profile;
                _scheduler.AddNewServiceToSchedule(myServiceConfiguration);
            }
            catch (Exception ex)
            {
                respond = false;
                Easynet.Edge.Core.Utilities.Log.Write("AddManualServiceListner", ex.Message, ex, Easynet.Edge.Core.Utilities.LogMessageType.Error);
            }



            return(respond);
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string[]        serviceAndAccount;
                bool            resultAll       = true;
                ServicePriority servicePriority = ServicePriority.Low;
                for (int i = 0; i < servicesCheckedListBox.Items.Count; i++)
                {
                    bool chk = servicesCheckedListBox.GetItemChecked(i);
                    if (chk)
                    {
                        serviceAndAccount = servicesCheckedListBox.Items[i].ToString().Split(':');


                        string account     = serviceAndAccount[0].Trim();
                        string serviceName = serviceAndAccount[1].Trim();


                        if (priorityCmb.SelectedItem != null)
                        {
                            switch (priorityCmb.SelectedItem.ToString())
                            {
                            case "Low":
                            {
                                servicePriority = ServicePriority.Low;
                                break;
                            }

                            case "Normal":
                            {
                                servicePriority = ServicePriority.Normal;
                                break;
                            }

                            case "High":
                            {
                                servicePriority = ServicePriority.High;
                                break;
                            }

                            case "Immediate":
                            {
                                servicePriority = ServicePriority.Immediate;
                                break;
                            }
                            }
                        }
                        Easynet.Edge.Core.SettingsCollection options = new Easynet.Edge.Core.SettingsCollection();
                        DateTime targetDateTime = new DateTime(dateToRunPicker.Value.Year, dateToRunPicker.Value.Month, dateToRunPicker.Value.Day, timeToRunPicker.Value.Hour, timeToRunPicker.Value.Minute, 0);
                        bool     result         = false;
                        if (useOptionsCheckBox.Checked)
                        {
                            foreach (ListViewItem item in optionsListView.Items)
                            {
                                options.Add(item.SubItems[0].Text.Trim(), item.SubItems[1].Text.Trim());
                            }

                            DateTime from = FromPicker.Value;
                            DateTime to   = toPicker.Value;
                            if (to.Date < from.Date || to.Date > DateTime.Now.Date)
                            {
                                throw new Exception("to date must be equal or greater then from date, and both should be less then today's date");
                            }


                            while (from.Date <= to.Date)
                            {
                                options["Date"] = from.ToString("yyyyMMdd");
                                result          = _listner.FormAddToSchedule(serviceName, int.Parse(account), targetDateTime, options, servicePriority);
                                from            = from.AddDays(1);
                            }
                        }
                        else
                        {
                            result = _listner.FormAddToSchedule(serviceName, int.Parse(account), targetDateTime, options, servicePriority);
                        }

                        if (result != true)
                        {
                            resultAll = result;
                        }
                    }
                }
                if (resultAll)
                {
                    MessageBox.Show("Service has been added to schedule and will be runinng shortly");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("one or more unplaned serivces did not schedule");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }