public void SendReport(PipelineContext ctx, ImportReport report)
        {
            if ((ctx.ImportEngine.ImportFlags & _ImportFlags.NoMailReport) != 0)
            {
                return;
            }

            if ((Mode & _Mode.Always) != 0)
            {
                goto SEND_REPORT;
            }
            if ((report.ErrorState & _ErrorState.Error) != 0 && (Mode & _Mode.Errors) != 0)
            {
                goto SEND_REPORT;
            }
            if ((report.ErrorState & _ErrorState.Limited) != 0 && (Mode & _Mode.Limited) != 0)
            {
                goto SEND_REPORT;
            }

            return;

SEND_REPORT:
            StringBuilder sb = new StringBuilder();
            String fn = ctx.ImportEngine.Xml.FileName;

            sb.AppendFormat("Import report for {0}.\r\n", fn);
            if (report.ErrorMessage != null)
            {
                sb.AppendFormat("-- LastError={0}\r\n", indentLines(report.ErrorMessage, 6, false));
            }
            sb.AppendFormat("-- Active datasources ({0}):\r\n", report.DatasourceReports.Count);
            for (int i = 0; i < report.DatasourceReports.Count; i++)
            {
                var dsrep = report.DatasourceReports[i];
                sb.AppendFormat("-- -- [{0}]: {1} \r\n", dsrep.DatasourceName, indentLines(dsrep.Stats, 9, false));
            }

            String shortName = Path.GetFileName(Path.GetDirectoryName(fn));

            ctx.ImportLog.Log("Sending report to {0} (first address)...", MailTo[0]);
            using (SmtpClientEx smtp = new SmtpClientEx(MailServer, Port))
            {
                using (MailMessage m = new MailMessage())
                {
                    m.From = MailFrom;
                    foreach (var x in MailTo)
                    {
                        m.To.Add(x);
                    }


                    m.Subject         = String.Format(MailSubject, fn, shortName, report.ErrorState.ToString());
                    m.SubjectEncoding = Encoding.UTF8;
                    m.BodyEncoding    = Encoding.UTF8;
                    m.Body            = sb.ToString();
                    smtp.Send(m);
                }
            }
            ctx.ImportLog.Log("Report is sent successfull.");
        }
Exemple #2
0
        public ImportReport Import(String[] enabledDSses = null)
        {
            var ret = new ImportReport();

            RunAdministrations = RunAdminSettings.Load();
            StartTimeUtc       = DateTime.UtcNow;

            ImportLog.Log();
            ImportLog.Log(new String('_', 80));
            ImportLog.Log(_LogType.ltProgress, "Starting import. VirtMem={3:F1}GB, Flags={0}, MaxAdds={1}, ActiveDS's='{2}'.", ImportFlags, MaxAdds, enabledDSses == null ? null : String.Join(", ", enabledDSses), OS.GetTotalVirtualMemory() / (1024 * 1024 * 1024.0));

            PipelineContext mainCtx = new PipelineContext(this);

            try
            {
                _ErrorState stateFilter = _ErrorState.All;
                if ((ImportFlags & _ImportFlags.IgnoreLimited) != 0)
                {
                    stateFilter &= ~_ErrorState.Limited;
                }
                if ((ImportFlags & _ImportFlags.IgnoreErrors) != 0)
                {
                    stateFilter &= ~_ErrorState.Error;
                }

                Endpoints.Open(mainCtx);

                for (int i = 0; i < Datasources.Count; i++)
                {
                    DatasourceAdmin admin = Datasources[i];
                    if (!String.IsNullOrEmpty(OverrideEndpoint))
                    {
                        ImportLog.Log(_LogType.ltWarning, "Datsource {0} will run with the override endpoint={1}", admin.Name, OverrideEndpoint);
                        admin.EndpointName = OverrideEndpoint;
                    }
                    if (!String.IsNullOrEmpty(OverrideEndpoint))
                    {
                        ImportLog.Log(_LogType.ltWarning, "Datsource {0} will run with the override pipeline={1}", admin.Name, OverridePipeline);
                        //admin.p = OverrideEndpoint;
                    }

                    if (!isActive(enabledDSses, admin))
                    {
                        ImportLog.Log(_LogType.ltProgress, "[{0}]: not active", admin.Name);
                        continue;
                    }

                    var report = new DatasourceReport(admin);
                    ret.Add(report);
                    PipelineContext ctx      = new PipelineContext(mainCtx, admin, report);
                    var             pipeline = admin.Pipeline;

                    try
                    {
                        admin.Import(ctx);
                        mainCtx.ErrorState |= (ctx.ErrorState & stateFilter);
                        if (ctx.LastError != null)
                        {
                            mainCtx.LastError = ctx.LastError;
                        }
                        report.MarkEnded(ctx);
                    }
                    catch (Exception err)
                    {
                        mainCtx.LastError   = err;
                        mainCtx.ErrorState |= (ctx.ErrorState & stateFilter) | _ErrorState.Error;
                        report.MarkEnded(ctx);
                        throw;
                    }
                    Endpoints.OptClosePerDatasource(ctx);

                    foreach (var c in Converters)
                    {
                        c.DumpMissed(ctx);
                    }
                }
                ImportLog.Log(_LogType.ltProgress, "Import ended");
                ProcessHostCollection.StopAll();
                Endpoints.Close(mainCtx);
                RunAdminSettings.Save(RunAdministrations);
            }
            catch (Exception err2)
            {
                mainCtx.LastError = err2;
                throw;
            }
            finally
            {
                try
                {
                    Endpoints.CloseFinally(mainCtx);
                }
                catch (Exception e2)
                {
                    ErrorLog.Log(e2);
                    ImportLog.Log(e2);
                }
                try
                {
                    ProcessHostCollection.StopAll();
                }
                catch (Exception e2)
                {
                    ErrorLog.Log(e2);
                    ImportLog.Log(e2);
                }
                try
                {
                    ret.SetGlobalStatus(mainCtx);
                    if (Reporter != null)
                    {
                        Reporter.SendReport(mainCtx, ret);
                    }
                }
                catch (Exception e2)
                {
                    ErrorLog.Log(e2);
                    ImportLog.Log(e2);
                }
            }
            //ret.SetGlobalStatus(mainCtx);
            ImportLog.Log("Unknown switches: [{0}]", ret.UnknownSwitches);
            ImportLog.Log("Mentioned switches: [{0}]", ret.MentionedSwitches);
            return(ret);
        }