Exemplo n.º 1
0
        static CrawlerApi()
        {
            Db = DbApi.Create();
            lock (Db)
            {
                try
                {
                    CrawlerId = Log.EntryAssemblyName;

                    Record r = Db.Get("SELECT State FROM Crawlers WHERE Id=@Id").GetFirstRecord("@Id", CrawlerId);
                    if (r == null)
                    {
                        LogMessage.Exit("Crawler id '" + CrawlerId + "' does not exist in [Crawlers] table.");
                    }

                    if ((Crawler.State)r["State"] == Crawler.State.DISABLED)
                    {
                        LogMessage.Exit("Crawler id '" + CrawlerId + "' is disabled.");
                    }

                    ProductsTable = Db.CreateProductsTableForCrawler(CrawlerId);

                    Session.Closing += session_Closing;

                    string archive = "session_start_time:"
                                     + (r["_SessionStartTime"] != null ? ((DateTime)r["_SessionStartTime"]).ToString("yyyy-MM-dd HH:mm:ss") : "")
                                     + " start_time:" + (r["_LastStartTime"] != null ? ((DateTime)r["_LastStartTime"]).ToString("yyyy-MM-dd HH:mm:ss") : "")
                                     + " end_time:" + (r["_LastEndTime"] != null ? ((DateTime)r["_LastEndTime"]).ToString("yyyy-MM-dd HH:mm:ss") : "")
                                     + " state:" + (r["_LastSessionState"] != null ? ((Crawler.SessionState)r["_LastSessionState"]).ToString() : "")
                                     + " log:" + r["_LastLog"] + "\n" + r["_Archive"];
                    const int MAX_ARCHIVE_LENGTH = 10000;
                    archive = archive.Substring(0, archive.Length < MAX_ARCHIVE_LENGTH ? archive.Length : MAX_ARCHIVE_LENGTH);
                    if (Db.Get("UPDATE Crawlers SET _SessionStartTime=@SessionStartTime, _LastProcessId=@ProcessId, _LastStartTime=GETDATE(), _LastEndTime=NULL, _LastSessionState=" + (int)Crawler.SessionState.STARTED + ", _LastLog=@Log, _Archive=@Archive WHERE Id=@Id").Execute(
                            "@SessionStartTime", Session.This.StartTime, "@ProcessId", Process.GetCurrentProcess().Id, "@Log", Log.SessionDir, "@Archive", archive, "@Id", CrawlerId) < 1
                        )
                    {
                        throw new Exception("Could not update Crawlers table.");
                    }

                    Log.Main.Write(Log.MessageType.INFORM, CrawlerApi.MessageMark.STARTED + "\r\nCommand line: " + string.Join("|", Environment.GetCommandLineArgs()) + "\n Running as: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                }
                catch (Exception e)
                {
                    LogMessage.Exit(e);
                }
            }
        }
Exemplo n.º 2
0
        static public void LoadFromDatabase(Assembly assembly, System.Configuration.ApplicationSettingsBase settings)
        {
            Assembly entry_assembly = Assembly.GetEntryAssembly();
            string   scope          = entry_assembly.GetName().Name;
            string   key            = KEY_PREFIX + assembly.GetName().Name;
            DbApi    di             = DbApi.Create();
            Dictionary <string, object> setting_names2value = Cliver.Bot.DbSettings.Get <Dictionary <string, object> >(di, scope, key);

            if (setting_names2value == null)
            {
                setting_names2value = new Dictionary <string, object>();
            }

            FieldInfo fi = settings.GetType().GetField("defaultInstance", BindingFlags.NonPublic | BindingFlags.Static);

            PropertyInfo[] pis          = fi.FieldType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            bool           new_property = false;

            foreach (PropertyInfo pi in pis)
            {
                string name = pi.DeclaringType.FullName + "." + pi.Name;
                object value;
                if (setting_names2value.TryGetValue(name, out value))
                {
                    if (!pi.CanWrite)
                    {
                        Log.Warning("Settings '" + name + "' is not writable.");
                        continue;
                    }
                    if (pi.PropertyType == typeof(int) && value is string)
                    {
                        value = int.Parse((string)value);
                    }
                    pi.SetValue(settings, value);
                }
                else
                {
                    new_property = true;
                    setting_names2value[name] = pi.GetValue(settings);
                }
            }
            if (new_property)
            {
                Cliver.Bot.DbSettings.Save(di, scope, key, setting_names2value);
            }
        }
Exemplo n.º 3
0
        protected Service()
        {
            Db = DbApi.Create();
            lock (Db)
            {
                try
                {
                    ServiceId = Log.EntryAssemblyName;

                    ThreadLog.Exitig += ThreadLog_Exitig;

                    Record r = Db.Get("SELECT State FROM Services WHERE Id=@Id").GetFirstRecord("@Id", ServiceId);
                    if (r == null)
                    {
                        LogMessage.Exit("Service id '" + ServiceId + "' does not exist in [Services] table.");
                    }

                    if ((Service.State)r["State"] == Service.State.DISABLED)
                    {
                        LogMessage.Exit("Service id '" + ServiceId + "' is disabled.");
                    }

                    string archive = " start_time:" + (r["_LastStartTime"] != null ? ((DateTime)r["_LastStartTime"]).ToString("yyyy-MM-dd HH:mm:ss") : "")
                                     + " end_time:" + (r["_LastEndTime"] != null ? ((DateTime)r["_LastEndTime"]).ToString("yyyy-MM-dd HH:mm:ss") : "")
                                     + " state:" + (r["_LastSessionState"] != null ? ((Service.SessionState)r["_LastSessionState"]).ToString() : "")
                                     + " log:" + r["_LastLog"] + "\n" + r["_Archive"];
                    const int MAX_ARCHIVE_LENGTH = 10000;
                    archive = archive.Substring(0, archive.Length < MAX_ARCHIVE_LENGTH ? archive.Length : MAX_ARCHIVE_LENGTH);
                    if (1 > Db.Get("UPDATE Services SET _LastProcessId=@ProcessId, _LastStartTime=GETDATE(), _LastEndTime=NULL, _LastSessionState=" + (int)Service.SessionState.STARTED + ", _LastLog=@Log, _Archive=@Archive WHERE Id=@Id").Execute(
                            "@ProcessId", Process.GetCurrentProcess().Id, "@Log", Log.WorkDir, "@Archive", archive, "@Id", ServiceId)
                        )
                    {
                        throw new Exception("Could not update Services table.");
                    }
                }
                catch (Exception e)
                {
                    LogMessage.Error(e);
                }
            }
        }
Exemplo n.º 4
0
 static public void Save(string scope, string key, Dictionary <string, object> setting_names2value)
 {
     Cliver.Bot.DbSettings.Save(DbApi.Create(), scope, key, setting_names2value);
 }
Exemplo n.º 5
0
        static public void Send(DbApi db, string message, ReportSourceType source_type, string source_id = null, bool error = true)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Properties.Settings.Default.SmtpHost))
                {
                    Log.Main.Warning("Message not emailed as SmtpHost is not specified:\n" + (error ? "ERROR:" : "" + source_id) + ":\n" + message);
                    return;
                }

                if (error)
                {
                    Log.Main.Error(message);
                }
                else
                {
                    Log.Main.Inform(message);
                }

                string AdminEmails = null;
                if (source_id != null)
                {
                    switch (source_type)
                    {
                    case ReportSourceType.CRAWLER:
                        AdminEmails = (string)db["SELECT AdminEmails FROM Crawlers WHERE Id=@Id"].GetSingleValue("@Id", source_id);
                        break;

                    case ReportSourceType.SERVICE:
                        AdminEmails = (string)db["SELECT AdminEmails FROM Services WHERE Id=@Id"].GetSingleValue("@Id", source_id);
                        break;

                    case ReportSourceType.MANAGER:
                        break;

                    default:
                        throw new Exception("Option is not defined.");
                    }
                }
                if (AdminEmails == null)
                {
                    AdminEmails = Properties.Settings.Default.DefaultAdminEmails;
                }
                if (AdminEmails != null)
                {
                    AdminEmails = Regex.Replace(AdminEmails.Trim(), @"[\s+\,]+", ",", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
                }
                else
                {
                    Log.Main.Error("No email is defined to send messages.");
                }

                MailMessage m = new MailMessage();
                m.From = new MailAddress(Properties.Settings.Default.EmailSender);
                m.To.Add(AdminEmails);
                string subject = "Crawler Host Service Manager:";
                if (source_id != null)
                {
                    subject += " " + source_id;
                }
                if (error)
                {
                    subject += " error";
                }
                subject  += " notification";
                m.Subject = subject;
                m.Body    = message;

                System.Net.Mail.SmtpClient c = new SmtpClient(Properties.Settings.Default.SmtpHost, Properties.Settings.Default.SmtpPort);
                c.EnableSsl             = true;
                c.DeliveryMethod        = SmtpDeliveryMethod.Network;
                c.UseDefaultCredentials = false;
                c.Credentials           = new System.Net.NetworkCredential(Properties.Settings.Default.SmtpLogin, Properties.Settings.Default.SmtpPassword);

                try
                {
                    c.Send(m);
                }
                catch (Exception e)
                {
                    Log.Main.Error(e);
                }
            }
            catch (Exception e)
            {
                Log.Main.Error(e);
            }
        }
Exemplo n.º 6
0
        static public void Send(DbApi db, string message, ReportSourceType source_type, string source_id = null, bool error = true)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Properties.Settings.Default.SmtpHost))
                {
                    Log.Main.Warning("Message not emailed as SmtpHost is not specified:\n" + (error ? "ERROR:" : "" + source_id) + ":\n" + message);
                    return;
                }

                if (error)
                    Log.Main.Error(message);
                else
                    Log.Main.Inform(message);

                string AdminEmails = null;
                if (source_id != null)
                    switch (source_type)
                    {
                        case ReportSourceType.CRAWLER:
                            AdminEmails = (string)db["SELECT AdminEmails FROM Crawlers WHERE Id=@Id"].GetSingleValue("@Id", source_id);
                            break;
                        case ReportSourceType.SERVICE:
                            AdminEmails = (string)db["SELECT AdminEmails FROM Services WHERE Id=@Id"].GetSingleValue("@Id", source_id);
                            break;
                        case ReportSourceType.MANAGER:
                            break;
                        default:
                            throw new Exception("Option is not defined.");
                    }
                if (AdminEmails == null)
                    AdminEmails = Properties.Settings.Default.DefaultAdminEmails;
                if (AdminEmails != null)
                    AdminEmails = Regex.Replace(AdminEmails.Trim(), @"[\s+\,]+", ",", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
                else
                    Log.Main.Error("No email is defined to send messages.");

                MailMessage m = new MailMessage();
                m.From = new MailAddress(Properties.Settings.Default.EmailSender);
                m.To.Add(AdminEmails);
                string subject = "Crawler Host Service Manager:";
                if (source_id != null)
                    subject += " " + source_id;
                if (error) subject += " error";
                subject += " notification";
                m.Subject = subject;
                m.Body = message;

                System.Net.Mail.SmtpClient c = new SmtpClient(Properties.Settings.Default.SmtpHost, Properties.Settings.Default.SmtpPort);
                c.EnableSsl = true;
                c.DeliveryMethod = SmtpDeliveryMethod.Network;
                c.UseDefaultCredentials = false;
                c.Credentials = new System.Net.NetworkCredential(Properties.Settings.Default.SmtpLogin, Properties.Settings.Default.SmtpPassword);

                try
                {
                    c.Send(m);
                }
                catch (Exception e)
                {
                    Log.Main.Error(e);
                }
            }
            catch (Exception e)
            {
                Log.Main.Error(e);
            }
        }