protected void FinishConnectorSchedule(DateTime start, ConnectorSchedule connectorSchedule)
        {
            TimeSpan ts   = DateTime.Now.Subtract(start);
            string   time = ts.ToString().Substring(0, 8);

            connectorSchedule.Duration                = time;
            connectorSchedule.ScheduledNextRun        = context.NextFireTimeUtc;
            connectorSchedule.ConnectorScheduleStatus = ConnectorScheduleStatus.WaitForNextRun;
            ctx.SubmitChanges();
        }
        public void Execute(JobExecutionContext context)
        {
            try
            {
                DataMap = context.JobDetail.JobDataMap;
                Running = true;

                DateTime start = DateTime.Now;
                log.InfoFormat("Starting plugin: {0}", Name);

                Concentrator.Objects.Web.Client.Login(Concentrator.Objects.Web.ConcentratorPrincipal.SystemPrincipal);

                using (var unit = GetUnitOfWork())
                {
                    var _repoConnectors = unit.Scope.Repository <Connector>();

                    //TODO : Preload options

                    //  var options = new DataLoadOptions();
                    //  options.LoadWith<Connector>(x => x.ConnectorSystem);
                    //  options.LoadWith<Connector>(x => x.Settings);
                    //  options.LoadWith<Connector>(x => x.ContentProducts);
                    //  options.LoadWith<Connector>(x => x.PreferredConnectorVendors);
                    //  options.LoadWith<Connector>(x => x.ConnectorLanguages);
                    //  options.LoadWith<ConnectorLanguage>(x => x.Language);
                    //  options.LoadWith<Vendor>(v => v.VendorSettings);
                    //  ctx.LoadOptions = options;

                    // filter connectors based on the optional attribute on Plugin level
                    var att = GetType().GetCustomAttributes(typeof(ConnectorSystemAttribute), true);

                    if (att.Length > 0)
                    {
                        var attributevalue = ((ConnectorSystemAttribute)att[0]).ConnectorSystemID;
                        _connectors = _repoConnectors.GetAll(c => c.ConnectorSystemID.HasValue && c.ConnectorSystemID.Value == attributevalue && c.IsActive).ToList();
                    }
                    else
                    {
                        _connectors = _repoConnectors.GetAll(x => x.IsActive).ToList();
                    }

                    _connectorSchedules = ctx.ConnectorSchedules.Where(x => x.Plugin == Name).ToList();

                    foreach (var c in _connectors)
                    {
                        var s = _connectorSchedules.FirstOrDefault(x => x.ConnectorID == c.ConnectorID);
                        if (s == null)
                        {
                            s = new ConnectorSchedule()
                            {
                                ConnectorID             = c.ConnectorID,
                                Plugin                  = Name,
                                ConnectorScheduleStatus = ConnectorScheduleStatus.Disabled
                            };
                            ctx.ConnectorSchedules.InsertOnSubmit(s);
                            _connectorSchedules.Add(s);
                        }
                    }

                    _vendors = ctx.Vendors.ToList();


                    Process();

                    _connectorSchedules.Where(x => x.ConnectorScheduleStatus != ConnectorScheduleStatus.WaitForNextRun).ToList().ForEach(x => x.ConnectorScheduleStatus = ConnectorScheduleStatus.Disabled);
                    ctx.SubmitChanges();
                    TimeSpan ts   = DateTime.Now.Subtract(start);
                    string   time = ts.ToString().Substring(0, 8);

                    log.InfoFormat("Finished plugin: {0}, duration : {1}, next run : {2}", Name, time, context.NextFireTimeUtc);
                }
            }
            catch (Exception ex)
            {
                log.FatalFormat("Error executing {0} plugin : {1}\n{2}", this.Name, ex.Message, ex.StackTrace);
            }
            catch
            {
                log.Fatal("Unknown error plugin");
            }
        }
Exemple #3
0
        public void Execute(JobExecutionContext context)
        {
            string pluginName = context.JobDetail.FullName;

            try
            {
                using (var unit = GetUnitOfWork())
                {
                    DataMap = context.JobDetail.JobDataMap;
                    DateTime start = DateTime.Now;

                    Plugin plugin = null;
                    if (DataMap.Contains("Plugin"))
                    {
                        int pluginID = ((Plugin)DataMap.Get("Plugin")).PluginID;
                        plugin = unit.Scope.Repository <Plugin>().GetSingle(x => x.PluginID == pluginID);

                        plugin.NextRun  = null;
                        plugin.Duration = null;
                    }

                    ConnectorSchedule schedule = null;
                    if (DataMap.Contains("ConnectorSchedule"))
                    {
                        int connectorScheduleID = ((ConnectorSchedule)DataMap.Get("ConnectorSchedule")).ConnectorScheduleID;
                        schedule                         = unit.Scope.Repository <ConnectorSchedule>().GetSingle(x => x.ConnectorScheduleID == connectorScheduleID);
                        schedule.LastRun                 = start;
                        schedule.Duration                = null;
                        schedule.ScheduledNextRun        = null;
                        schedule.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.Running;
                    }

                    unit.Save();

                    Running = true;

                    log.InfoFormat("Starting plugin: {0}", pluginName);

                    Concentrator.Objects.Web.Client.Login(Concentrator.Objects.Web.ConcentratorPrincipal.SystemPrincipal);

                    var _repoConnectors = unit.Scope.Repository <Connector>();

                    // filter connectors based on the optional attribute on Plugin level
                    var att = GetType().GetCustomAttributes(typeof(ConnectorSystemAttribute), true);

                    if (att.Length > 0)
                    {
                        var attributevalue = ((ConnectorSystemAttribute)att[0]).ConnectorSystem;
                        _connectors = _repoConnectors.GetAll(c => c.ConnectorSystemID.HasValue && c.ConnectorSystem.Name == attributevalue).ToList();

#if !DEBUG
                        _connectors = _connectors.Where(c => c.IsActive).ToList();
#endif
                    }
                    else
                    {
                        try
                        {
                            _connectors = _repoConnectors.GetAll().ToList();
#if !DEBUG
                            _connectors = _connectors.Where(c => c.IsActive).ToList();
#endif
                        }
                        catch (Exception e)
                        {
                            log.Debug(e.InnerException);
                        }
                    }

                    if (schedule == null)
                    {
                        var allConnectorSchedules = unit.Scope.Repository <ConnectorSchedule>().GetAll(x => x.PluginID == plugin.PluginID).ToList();
                        _connectors = _connectors.Except(allConnectorSchedules.Select(x => x.Connector));
                    }
                    else
                    {
                        _connectors = _connectors.Where(x => x.ConnectorID == schedule.ConnectorID);
                    }

                    _vendors = unit.Scope.Repository <Vendor>().GetAll().ToList();

                    Stopwatch watch = Stopwatch.StartNew();
                    log.AuditInfo(string.Format("Starting {0} at {1}", Name, DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss")), Name);
                    Process();
                    watch.Stop();
                    log.AuditComplete(string.Format("Finished {0} at {1}. The plugin took {2} to finish", Name, DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), watch.Elapsed.TotalMinutes.ToString()), Name);

                    //_connectorSchedules.Where(x => x.ConnectorScheduleStatus != (int)ConnectorScheduleStatus.WaitForNextRun).ToList().ForEach(x => x.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.Disabled);
                    TimeSpan ts   = DateTime.Now.Subtract(start);
                    string   time = ts.ToString().Substring(0, 8);

                    if (plugin != null && schedule == null)
                    {
                        plugin.LastRun  = start;
                        plugin.Duration = time;
                        if (context.NextFireTimeUtc.HasValue)
                        {
                            plugin.NextRun = context.NextFireTimeUtc.Value.ToLocalTime();
                        }
                    }

                    if (schedule != null)
                    {
                        if (context.NextFireTimeUtc.HasValue)
                        {
                            schedule.ScheduledNextRun = context.NextFireTimeUtc.Value.ToLocalTime();
                        }

                        schedule.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.WaitForNextRun;
                        schedule.Duration = time;
                    }

                    log.InfoFormat("Finished plugin: {0}, duration : {1}, next run : {2}", pluginName, time, context.NextFireTimeUtc.HasValue ? context.NextFireTimeUtc.Value.ToLocalTime() : DateTime.MinValue);
                    unit.Save();
                }
            }
            catch (Exception ex)
            {
                //log.FatalFormat("Error executing {0} plugin : {1}\n{2}", pluginName, ex.InnerException != null ? ex.InnerException.Message : ex.Message, ex.StackTrace);
                log.AuditFatal(string.Format("Error executing {0} plugin : {1}\n{2}", pluginName, ex.InnerException != null ? ex.InnerException.Message : ex.Message, ex.StackTrace), ex, pluginName);
            }
        }