Пример #1
0
        private void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (padlock)
            {
                if (IsAlreadyWork)
                {
                    return;
                }
                IsAlreadyWork = true;
            }
            try
            {
                logger.Debug(string.Format("New elapsation started-{0}", DateTime.Now));

                //log maintainance
                this.DeleteOldLogs();

                //go through items in Manual Mapping Import Queue
                this.ProcessImportQueue();

                //check current time against defined in config
                if (!CanonConfigManager.ForceStart)
                {
                    DateTime start = CanonConfigManager.TimeToStart;
                    DateTime now   = DateTime.Now;

                    logger.Debug(string.Format("now-{0}, start-{1}", now, start));

                    if (start > now)
                    {
                        return;
                    }
                    else if ((now - start).TotalMinutes > 30)
                    {
                        return;
                    }
                }

                //Clear manual import queue and subsribers
                CanonDataContext db = Cdb.Instance;
                if (!CanonConfigManager.ForceStart)
                {
                    //delete all complete every night
                    var allManuals = db.ManualImportQueues.Where(p => p.Status == (int)ManualImportStatusEnum.ImportComplete ||
                                                                 p.Status == (int)ManualImportStatusEnum.InProgress);
                    db.ManualImportQueues.DeleteAllOnSubmit(allManuals);
                    db.SubmitChanges();
                }
                else
                {
                    //delete all old
                    var allManualsA = db.ManualImportQueues.Where(p => p.PostDate.Date < DateTime.Now.Date);
                    db.ManualImportQueues.DeleteAllOnSubmit(allManualsA);
                    db.SubmitChanges();
                }

                //application maintainance
                db.MaintainDatabase();

                //go through all channels
                foreach (Channel channel in db.Channels)
                {
                    try
                    {
                        if (!channel.IsActive)
                        {
                            logger.Info(string.Format("Channel is disabled: {0}, {1}, {2}",
                                                      channel.ChannelId, channel.ChannelName, channel.Url));
                            continue;
                        }

                        logger.Info(string.Format("Starting new export: {0}, {1}, {2}",
                                                  channel.ChannelId, channel.ChannelName, channel.Url));

                        if (!ShouldRun(channel))
                        {
                            logger.Info(string.Format("Data already imported today or there was a error during import."));
                            continue;
                        }

                        this.ProcessChannel(channel);
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(string.Format("Import for channel {0} failed", channel.ChannelName), ex);
                    }
                }
                //send daily emails
                if (CanonImportService.LastEmailSent.Date < DateTime.Now.Date)
                {
                    List <User> users = db.Users.Where(u => u.IsDailyEmail && u.Email.Length > 0).ToList();
                    string      text  = this.CreateEmailText(null).Trim();
                    logger.Info("Email text:" + text);
                    if (!string.IsNullOrEmpty(text))
                    {
                        foreach (User user in users)
                        {
                            try
                            {
                                EmailGateway email = new EmailGateway(user.Email, "Daily Log", text);
                                email.Send();
                                CanonImportService.LastEmailSent = DateTime.Now;
                            }
                            catch (Exception ex)
                            {
                                logger.Fatal(ex);
                            }
                        }
                    }
                    //send daily email for users in channels
                    List <Channel> repChannels = db.Channels.ToList();
                    foreach (Channel repChannel in repChannels)
                    {
                        if (!repChannel.IsActive)
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(repChannel.ReportingTo))
                        {
                            string[] emails    = repChannel.ReportingTo.Split(';');
                            string   emailText = this.CreateEmailText(repChannel).Trim();
                            foreach (string email in emails)
                            {
                                try
                                {
                                    EmailGateway emailG = new EmailGateway(email, "Průzkum trhu", emailText);
                                    logger.Info(string.Format("Sending: {0}, text={1}", email, emailText));
                                    emailG.Send();
                                    CanonImportService.LastEmailSent = DateTime.Now;
                                }
                                catch (Exception ex)
                                {
                                    logger.Fatal(ex);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
            }
            finally
            {
                IsAlreadyWork = false;
            }
        }