/// <summary> /// OnStop(): Put your stop code here /// - Stop threads, set final data, etc. /// </summary> protected override void OnStop() { try { _log.Info("Stoping service\r\n"); if (_cancelTokenSource != null) { _cancelTokenSource.Cancel(); } if (_queueManager != null) { _queueManager.CancelHandler.WaitOne(); } StopTimer(); if (_workTimer != null) { _workTimer.Dispose(); _workTimer = null; } if (_queueManager != null) { _queueManager.Dispose(); } if (_signalrWorker != null) { _signalrWorker.Dispose(); } } catch (Exception ex) { _log.ErrorFormat("Stop service Error: {0}\r\n", ex.ToString()); } finally { _log.Info("Stop service\r\n"); base.OnStop(); if (_resetEvent != null) { _resetEvent.Set(); } if (_healthCheckServiceHost != null) { _healthCheckServiceHost.Close(); _healthCheckServiceHost = null; } } }
public string StoreMailEml(int tenant, string user, string streamId, MimeMessage message, ILog log) { if (message == null) { return(string.Empty); } // Using id_user as domain in S3 Storage - allows not to add quota to tenant. var savePath = MailStoragePathCombiner.GetEmlKey(user, streamId); var storage = MailDataStore.GetDataStore(tenant); try { using (var stream = new MemoryStream()) { message.WriteTo(stream); var res = storage.Save(savePath, stream, MailStoragePathCombiner.EML_FILE_NAME).ToString(); log.InfoFormat("StoreMailEml() tenant='{0}', user_id='{1}', save_eml_path='{2}' Result: {3}", tenant, user, savePath, res); return(res); } } catch (Exception ex) { log.ErrorFormat("StoreMailEml Exception: {0}", ex.ToString()); } return(string.Empty); }
private void NotifySignalrIfNeed(MailBoxData mailbox, ILog log) { if (!_tasksConfig.EnableSignalr) { log.Debug("Skip NotifySignalrIfNeed: EnableSignalr == false"); return; } var now = DateTime.UtcNow; try { if (mailbox.LastSignalrNotify.HasValue && !((now - mailbox.LastSignalrNotify.Value).TotalSeconds > SIGNALR_WAIT_SECONDS)) { mailbox.LastSignalrNotifySkipped = true; log.InfoFormat( "Skip NotifySignalrIfNeed: last notification has occurend less then {0} seconds ago", SIGNALR_WAIT_SECONDS); return; } if (_signalrWorker == null) { throw new NullReferenceException("_signalrWorker"); } _signalrWorker.AddMailbox(mailbox); log.InfoFormat("NotifySignalrIfNeed(UserId = {0} TenantId = {1}) has been succeeded", mailbox.UserId, mailbox.TenantId); } catch (Exception ex) { log.ErrorFormat("NotifySignalrIfNeed(UserId = {0} TenantId = {1}) Exception: {2}", mailbox.UserId, mailbox.TenantId, ex.ToString()); } mailbox.LastSignalrNotify = now; mailbox.LastSignalrNotifySkipped = false; }
private static void SetMailboxAuthError(MailBoxData mailbox, ILog log) { try { if (mailbox.AuthErrorDate.HasValue) { return; } mailbox.AuthErrorDate = DateTime.UtcNow; var engine = new EngineFactory(mailbox.TenantId); engine.MailboxEngine.SetMaiboxAuthError(mailbox.MailBoxId, mailbox.AuthErrorDate.Value); } catch (Exception ex) { log.ErrorFormat( "CreateTasks->SetMailboxAuthError(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message); } }
private void CloseMailClient(MailClient client, MailBoxData mailbox, ILog log) { if (client == null) { return; } try { client.Authenticated -= ClientOnAuthenticated; client.GetMessage -= ClientOnGetMessage; client.Cancel(); client.Dispose(); } catch (Exception ex) { log.ErrorFormat( "CloseMailClient(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message); } }
private void DoOptionalOperations(MailMessageData message, MimeMessage mimeMessage, MailBoxData mailbox, MailFolder folder, ILog log) { var factory = new EngineFactory(mailbox.TenantId, mailbox.UserId, log); var tagIds = new List <int>(); if (folder.Tags.Any()) { log.Debug("DoOptionalOperations->GetOrCreateTags()"); tagIds = factory.TagEngine.GetOrCreateTags(mailbox.TenantId, mailbox.UserId, folder.Tags); } log.Debug("DoOptionalOperations->IsCrmAvailable()"); if (IsCrmAvailable(mailbox, log)) { log.Debug("DoOptionalOperations->GetCrmTags()"); var crmTagIds = factory.TagEngine.GetCrmTags(message.FromEmail); if (crmTagIds.Any()) { if (tagIds == null) { tagIds = new List <int>(); } tagIds.AddRange(crmTagIds.Select(t => t.Id)); } } if (tagIds.Any()) { if (message.TagIds == null || !message.TagIds.Any()) { message.TagIds = tagIds; } else { message.TagIds.AddRange(tagIds); } message.TagIds = message.TagIds.Distinct().ToList(); } log.Debug("DoOptionalOperations->AddMessageToIndex()"); var wrapper = message.ToMailWrapper(mailbox.TenantId, new Guid(mailbox.UserId)); factory.IndexEngine.Add(wrapper); foreach (var tagId in tagIds) { try { log.DebugFormat("DoOptionalOperations->SetMessagesTag(tagId: {0})", tagId); factory.TagEngine.SetMessagesTag(new List <int> { message.Id }, tagId); } catch (Exception e) { log.ErrorFormat( "SetMessagesTag(tenant={0}, userId='{1}', messageId={2}, tagid = {3}) Exception:\r\n{4}\r\n", mailbox.TenantId, mailbox.UserId, message.Id, e.ToString(), tagIds != null ? string.Join(",", tagIds) : "null"); } } log.Debug("DoOptionalOperations->AddRelationshipEventForLinkedAccounts()"); factory.CrmLinkEngine.AddRelationshipEventForLinkedAccounts(mailbox, message, _tasksConfig.DefaultApiSchema); log.Debug("DoOptionalOperations->SaveEmailInData()"); factory.EmailInEngine.SaveEmailInData(mailbox, message, _tasksConfig.DefaultApiSchema); log.Debug("DoOptionalOperations->SendAutoreply()"); factory.AutoreplyEngine.SendAutoreply(mailbox, message, _tasksConfig.DefaultApiSchema, log); log.Debug("DoOptionalOperations->UploadIcsToCalendar()"); factory .CalendarEngine .UploadIcsToCalendar(mailbox, message.CalendarId, message.CalendarUid, message.CalendarEventIcs, message.CalendarEventCharset, message.CalendarEventMimeType, mailbox.EMail.Address, _tasksConfig.DefaultApiSchema); if (_tasksConfig.SaveOriginalMessage) { log.Debug("DoOptionalOperations->StoreMailEml()"); StoreMailEml(mailbox.TenantId, mailbox.UserId, message.StreamId, mimeMessage, log); } log.Debug("DoOptionalOperations->ApplyFilters()"); var filters = GetFilters(factory, log); factory.FilterEngine.ApplyFilters(message, mailbox, folder, filters); log.Debug("DoOptionalOperations->NotifySignalrIfNeed()"); NotifySignalrIfNeed(mailbox, log); }
private MailClient CreateMailClient(MailBoxData mailbox, ILog log, CancellationToken cancelToken) { MailClient client = null; var connectError = false; var stopClient = false; Stopwatch watch = null; if (_tasksConfig.CollectStatistics) { watch = new Stopwatch(); watch.Start(); } try { client = new MailClient(mailbox, cancelToken, _tasksConfig.TcpTimeout, mailbox.IsTeamlab || _tasksConfig.SslCertificateErrorsPermit, _tasksConfig.ProtocolLogPath, log, true); log.DebugFormat("MailClient.LoginImapPop(Tenant = {0}, MailboxId = {1} Address = '{2}')", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail); if (!mailbox.Imap) { client.FuncGetPop3NewMessagesIDs = uidls => MessageEngine.GetPop3NewMessagesIDs(mailbox, uidls, _tasksConfig.ChunkOfPop3Uidl); } client.Authenticated += ClientOnAuthenticated; client.LoginImapPop(); } catch (System.TimeoutException exTimeout) { log.WarnFormat( "[TIMEOUT] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, exTimeout.ToString()); connectError = true; stopClient = true; } catch (OperationCanceledException) { log.InfoFormat( "[CANCEL] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail); stopClient = true; } catch (AuthenticationException authEx) { log.ErrorFormat( "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, authEx.ToString()); connectError = true; stopClient = true; } catch (WebException webEx) { log.ErrorFormat( "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, webEx.ToString()); connectError = true; stopClient = true; } catch (Exception ex) { log.ErrorFormat( "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n", mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex is ImapProtocolException || ex is Pop3ProtocolException ? ex.Message : ex.ToString()); stopClient = true; } finally { if (connectError) { SetMailboxAuthError(mailbox, log); } if (stopClient) { CloseMailClient(client, mailbox, log); } if (_tasksConfig.CollectStatistics && watch != null) { watch.Stop(); LogStat(CONNECT_MAILBOX, mailbox, watch.Elapsed, connectError); } } return(client); }