Ejemplo n.º 1
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);
            }
        }