/// <summary> /// The register services. /// </summary> /// <param name="container"> /// The container. /// </param> private void RegisterServices(IUnityContainer container) { EmailsBasePath = this.GetMandatorySetting(EmailsBasePathSetting); string storageAccount = this.GetMandatorySetting(StorageSetting); string emailJobsQueueName = this.GetMandatorySetting(EmailJobsQueueNameSetting); string userDalConnectionString = CloudConfigurationManager.GetSetting(UsersDalConnectionStringSetting); bool isDebugSecurityProviderEnabled = bool.Parse(this.GetMandatorySetting(DebugSecurityProviderEnabled)); Uri bingOffersBaseUri = new Uri(this.GetMandatorySetting(BingOffersBasePathUriSetting)); // Read Email Confirmation Settings EntityConfirmationSettings entityConfirmationSettings = new EntityConfirmationSettings { BingOffersBaseUri = bingOffersBaseUri }; var emailJobsQueue = new JobsQueue <EmailCargo>(storageAccount, emailJobsQueueName); var priorityEmailJobsQueue = new PriorityEmailJobsQueue <PriorityEmailCargo>(storageAccount); UsersDal usersDal = new UsersDal(userDalConnectionString, queue: priorityEmailJobsQueue); container.RegisterInstance(entityConfirmationSettings); container.RegisterInstance <IJobsQueue <EmailCargo> >(emailJobsQueue); container.RegisterInstance <IPriorityEmailJobsQueue <PriorityEmailCargo> >(priorityEmailJobsQueue); container.RegisterInstance <IUsersDal>(usersDal); container.RegisterInstance <IEmailClient>(new EmailSendGridClient()); // Authentication Modules // Add modules that perform authorization if (isDebugSecurityProviderEnabled) { Security.Add("user_debug", new UserDebugSecurityProvider(usersDal)); } Security.Add("lomo", new LomoSecurityProvider(usersDal)); }
/// <summary> /// Starts the main execution loop. /// </summary> public void Start() { while (!ct.IsCancellationRequested) { jobsLock.EnterMoveLock(); while (!JobsQueue.TryDequeue(out currentJob)) { jobsLock.ExitMoveLock(); queueNonEmpty.WaitOne(); if (ct.IsCancellationRequested) { return; } jobsLock.EnterMoveLock(); } jobsLock.ExitMoveLock(); OnJobChange(currentJob.GetView(), JobChangeEvent.BeforeRun); currentJob.Run(); if (currentJob.LastStatus != JobStatus.Canceled && currentJob.LastStatus != JobStatus.Error) { OnJobChange(currentJob.GetView(), JobChangeEvent.AfterCompleted); } currentJob.Dispose(); } }
private void Update() { if (JobsQueue.Count == 0 && TempJobsQueue.Count == 0) { return; } //Taking the jobs one by one as long as we have any jobs if (JobsQueue.Count == 0 && TempJobsQueue.Count > 0) { jobsQueue = new List <TweenJob>(TempJobsQueue); TempJobsQueue.Clear(); } while (JobsQueue.Count > 0) { currentJob = JobsQueue[0]; JobsQueue.RemoveAt(0); if (currentJob == null) { continue; } TempJobsQueue.Add(currentJob); currentJob.Work(Time.deltaTime, this); currentJob = null; } }
public void AddJob(TweenJob job) { if (job == null) { return; } JobsQueue.Add(job); }
public TweenJob GetJob(int id) { TweenJob job = JobsQueue.Find(x => x.JobID == id); // look for the job in the active queue if (job != null) { return(job); } job = TempJobsQueue.Find(x => x.JobID == id); // look in the temp as well return(job); }
private static void DispatchTrendingDeals(UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, bool includeList, List <Guid> userIds, string emailRenderingUrl, string campaignName, IEnumerable <Guid> dealIds) { string subject = ConfigurationManager.AppSettings["LoMo.TrendingDeals.Subject"]; if (includeList) { foreach (Guid userId in userIds) { Record(string.Format("Start dispatching. User Id={0}", userId)); var subscriptions = usersDal.GetEmailSubscriptionsByUserId(userId, true, SubscriptionType.WeeklyDeals.ToString()); foreach (EmailSubscription emailSubscription in subscriptions) { if (emailSubscription.LocationId.Contains("us:postal:")) { if (CloHelper.IsCloRegion(emailSubscription.LocationId).Item1) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, true, null, dealIds: dealIds, subject: subject); } } } } } else { object continuationContext = null; bool hasMore = true; while (hasMore) { EmailsSubscriptionsBatchResponse response = usersDal.GetNextEmailSubscriptionsBatch(10000, true, continuationContext, SubscriptionType.WeeklyDeals); if (response.EmailSubscriptions != null) { foreach (EmailSubscription emailSubscription in response.EmailSubscriptions) { if (userIds != null && userIds.Contains(emailSubscription.UserId)) { Record(string.Format("User With Id {0} is excluded from this run.", emailSubscription.UserId)); } else { if (emailSubscription.LocationId.Contains("us:postal:") && CloHelper.IsCloRegion(emailSubscription.LocationId).Item1) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, true, null, dealIds: dealIds, subject: subject); } } } } hasMore = response.HasMore; continuationContext = response.ContinuationContext; } } }
public void RemoveJob(int id) { TweenJob job = JobsQueue.Find(x => x.JobID == id); // look for the job in the active queue if (job != null) { JobsQueue.Remove(job); } job = TempJobsQueue.Find(x => x.JobID == id); // look in the temp as well if (job != null) { TempJobsQueue.Remove(job); } }
private static void DispatchRemainderEmail(JobsQueue <EmailCargo> emailJobsQueue, User user, string campaign) { PromotionalEmailCargo remainderEmailCargo = new PromotionalEmailCargo { Id = Guid.NewGuid(), EmailAddress = user.Email, Campaign = campaign, UserId = user.Id, EmailRenderingServiceAddress = ConfigurationManager.AppSettings["LoMo.RemainderEmailRenderingServiceUrl"], PromotionalEmailType = PromotionalEmailType.CompleteSignup.ToString(), Subject = ConfigurationManager.AppSettings["LoMo.RemainderEmail.Subject"] }; emailJobsQueue.Enqueue(remainderEmailCargo); Record(string.Format("Email Job Enqueued. JobInfo = {0}", remainderEmailCargo)); }
private static void DispatchNotificationMail( UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, string campaignName, string emailSubject, List <Guid> userIds) { int totalDispatched = 0; foreach (Guid userId in userIds) { User user = usersDal.GetUserByUserId(userId); EmailSubscription subscription = new EmailSubscription { IsActive = true, //Dummy location LocationId = "us:postal:00000", SubscriptionType = SubscriptionType.Promotional, UserId = user.Id, }; if (user.IsSuppressed == true) { Record( string.Format("User With Id={0} is suppressed", user.Id)); } else { totalDispatched++; DispatchEmailJob( subscription, usersDal, emailJobsQueue, campaignName, CampaignRenderingServiceURL, false, null, false, false, emailSubject); Record(string.Format("Total dispatched {0}", totalDispatched)); } } }
private static void DispatchPromotionalMails(UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, List <Guid> userIds, string campaignName) { int count = 0; foreach (Guid userId in userIds) { Record(string.Format("Start dispatching. User Id={0}", userId)); var subscriptions = usersDal.GetEmailSubscriptionsByUserId(userId, true, SubscriptionType.WeeklyDeals.ToString()); foreach (EmailSubscription emailSubscription in subscriptions) { if (emailSubscription.LocationId.Contains("us:postal:")) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, CampaignRenderingServiceURL, false, null, false); count++; } } } Record(string.Format("Total mails sent : {0}", count)); }
private static void DispatchRemainderEmails(UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, string inputFile, int emailCount, string campaign) { Uri analyticsBaseUri = new Uri(ConfigurationManager.AppSettings["LoMo.AnalyticsUri"]); string analyticsUserInfoUri = "api/user?userId={0}"; string accessToken = GetAnalyticsAccessToken(); if (!string.IsNullOrEmpty(accessToken)) { var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); string[] lines = File.ReadAllLines(inputFile); Console.WriteLine("Total Count of users in the input file : {0}", lines.Count()); for (int i = 0; i < lines.Count() && emailCount > 0; i++) { User user = usersDal.GetUserByUserId(new Guid(lines[i])); if (user != null && !string.IsNullOrEmpty(user.Email)) { var analyticsUri = new Uri(analyticsBaseUri, string.Format(analyticsUserInfoUri, user.Id)); HttpResponseMessage response = client.GetAsync(analyticsUri).Result; try { if (response.IsSuccessStatusCode) { var userInfo = JsonConvert.DeserializeObject <UserContract>(response.Content.ReadAsStringAsync().Result); if (userInfo.CloUserAddedDateTime != null && userInfo.CloFirstCardAddedDateTime == null) { DispatchRemainderEmail(emailJobsQueue, user, campaign); emailCount--; } } } catch (Exception exception) { Console.WriteLine(exception.Message); } } } } }
private static void DispatchEmailJob(EmailSubscription emailSubscription, UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, string campaign, string emailRenderingUrl, bool isCloDeal, string[] categories, bool includeDeals = true, bool useTestAccount = false, string subject = null, IEnumerable <Guid> dealIds = null) { try { User user = usersDal.GetUserByUserId(emailSubscription.UserId); string unsubscribeUrl = usersDal.GetUnsubscribeUrlInfo(user.Id).UnsubscribeUrl; if (string.IsNullOrEmpty(user.Email)) { Record(string.Format("can't dispatch email job for user without emil address. User Id={0}", user.Id)); return; } if (string.IsNullOrEmpty(unsubscribeUrl)) { Record(string.Format("can't dispatch email job for user without unsubscribe url. User Id={0}", user.Id)); return; } DealsEmailCargo dealsEmailCargo = new DealsEmailCargo { Id = Guid.NewGuid(), EmailAddress = user.Email, Campaign = campaign, Hints = new EmailJobHints { IsTest = useTestAccount, IncludeDeals = includeDeals }, LocationId = emailSubscription.LocationId, UnsubscribeUrl = unsubscribeUrl, UserId = user.Id, DealIds = dealIds, IsCloDeal = isCloDeal, EmailRenderingServiceAddress = emailRenderingUrl }; if (categories != null && categories.Any()) { List <Guid> lstGuid = new List <Guid>(); foreach (string category in categories) { lstGuid.Add(GetCategoryGuid(category)); } dealsEmailCargo.Categories = lstGuid; } else { dealsEmailCargo.Categories = (user.Info != null && user.Info.Preferences != null) ? user.Info.Preferences.Categories : null; } if (dealsEmailCargo.Hints.IncludeDeals && !string.IsNullOrEmpty(user.MsId) && !user.MsId.StartsWith("fb")) { dealsEmailCargo.Anid = AnidIdentityProvider.Instance.DeriveIdentity(user.MsId.ToUpper()); } if (!string.IsNullOrEmpty(subject)) { dealsEmailCargo.Subject = subject; } emailJobsQueue.Enqueue(dealsEmailCargo); Record(string.Format("Email Job Enqueued. Id={0}; LocationId={1};UnsubscribeUrl={2};UserId={3};CategoriesCount={4}", dealsEmailCargo.Id, dealsEmailCargo.LocationId, dealsEmailCargo.UnsubscribeUrl, dealsEmailCargo.UserId, dealsEmailCargo.Categories == null ? 0 : dealsEmailCargo.Categories.Count())); } catch (Exception e) { Log.Error("Error while dispathing email job. User Id={0}; Location Id={1}; Error={2}", emailSubscription.UserId, emailSubscription.LocationId, e); } }
private static void DispatchWeeklyDeals(UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, bool includeList, List <Guid> userIds, string emailRenderingUrl, string campaignName, bool isClo) { int cloUser = 0; int nonCloUser = 0; if (includeList) { foreach (Guid userId in userIds) { Record(string.Format("Start dispatching. User Id={0}", userId)); var subscriptions = usersDal.GetEmailSubscriptionsByUserId(userId, true, SubscriptionType.WeeklyDeals.ToString()); foreach (EmailSubscription emailSubscription in subscriptions) { if (emailSubscription.LocationId.Contains("us:postal:")) { if (CloHelper.IsCloRegion(emailSubscription.LocationId).Item1) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, true, null); cloUser++; } else { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, false, null); nonCloUser++; } } } } Record(string.Format("Total {0} mails sent : {1}", isClo ? "CLO" : "Non CLO", isClo ? cloUser : nonCloUser)); } else { // List of excluded users object continuationContext = null; bool hasMore = true; while (hasMore) { EmailsSubscriptionsBatchResponse response = usersDal.GetNextEmailSubscriptionsBatch(10000, true, continuationContext, SubscriptionType.WeeklyDeals); if (response.EmailSubscriptions != null) { foreach (EmailSubscription emailSubscription in response.EmailSubscriptions) { if (userIds != null && userIds.Contains(emailSubscription.UserId)) { Record(string.Format("User With Id {0} is excluded from this run.", emailSubscription.UserId)); } else { if (emailSubscription.LocationId.Contains("us:postal:")) { if (isClo) { if (CloHelper.IsCloRegion(emailSubscription.LocationId).Item1) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, true, null); cloUser++; } } else { if (!CloHelper.IsCloRegion(emailSubscription.LocationId).Item1) { DispatchEmailJob(emailSubscription, usersDal, emailJobsQueue, campaignName, emailRenderingUrl, false, null); nonCloUser++; } } } } } } hasMore = response.HasMore; continuationContext = response.ContinuationContext; } Record(string.Format("Total {0} mails sent : {1}", isClo ? "CLO" : "Non CLO", isClo ? cloUser : nonCloUser)); } }
static void Main(string[] args) { bool exit = false; Record("Email Jobs Dispatcher Started"); string accountName = CloudConfigurationManager.GetSetting(StorageSetting); string queueName = CloudConfigurationManager.GetSetting(EmailJobsQueueName); emailJobsQueue = new JobsQueue <EmailCargo>(accountName, queueName); if (args[0] == "create_users" && args.Length >= 3) { string emailSource = args.Length == 4 ? args[3] : null; CreateUsers(args[1], args[2], emailSource, usersDal); } else if (args[0] == "WeeklyDeals" && args.Length == 5) { bool includeUsers = false; if (args[1] == "include_users") { includeUsers = true; } else if (args[1] != "exclude_users") { Record("bad arguments - dispatch"); exit = true; } string dealType = args[3].Trim().ToUpper(); if (!exit && (dealType == "CLO" || dealType == "PP")) { bool isClo = dealType == "CLO"; string EmailRenderingServiceURL = ConfigurationManager.AppSettings["LoMo.WeeklyEmailRenderingServiceUrl"]; EmailRenderingServiceURL = string.Format(EmailRenderingServiceURL, args[4]); List <Guid> userIds = File.ReadAllLines(args[2]).Select(elem => new Guid(elem)).ToList(); DispatchWeeklyDeals(usersDal, emailJobsQueue, includeUsers, userIds, EmailRenderingServiceURL, args[4], isClo); } } else if (args[0] == "Promotional" && args.Length == 3) { string campaignName = args[1]; CampaignRenderingServiceURL = ConfigurationManager.AppSettings["LoMo.CampaignEmailRenderingServiceUrl"]; CampaignRenderingServiceURL = string.Format(CampaignRenderingServiceURL, campaignName); List <Guid> userIds = File.ReadAllLines(args[2]).Select(elem => new Guid(elem)).ToList(); DispatchPromotionalMails(usersDal, emailJobsQueue, userIds, campaignName); } else if (args[0] == "Notification" && args.Length == 5) { string campaignName = args[2]; List <Guid> userIds = File.ReadAllLines(args[1]).Select(elem => new Guid(elem)).ToList(); CampaignRenderingServiceURL = args[3]; string subject = args[4]; DispatchNotificationMail(usersDal, emailJobsQueue, campaignName, subject, userIds); } else if (args[0] == "Campaign") { DispatchCampaignMails(); } else if (args[0] == "suppress" && args.Length == 2) { SuppressUsers(args[1], usersDal); } else if (args[0] == "CleanUp") { DeactivateSpamMailAddress(usersDal); } else if (args[0] == "Lookup" && args.Length == 3) { GetUsersByEmail(usersDal, args[1], args[2]); } else if (args[0] == "SignupRemainder" && args.Length == 4) { int emailCount; if (!int.TryParse(args[2], out emailCount)) { emailCount = 100; } DispatchRemainderEmails(usersDal, emailJobsQueue, args[1], emailCount, args[3]); } else if (args[0] == "TrendingDeals" && args.Length == 5) { bool includeUsers = false; if (args[1] == "include_users") { includeUsers = true; } else if (args[1] != "exclude_users") { Record("bad arguments - dispatch"); exit = true; } if (!exit) { string emailRenderingServiceUrl = ConfigurationManager.AppSettings["LoMo.TrendingDealEmailRenderingServiceUrl"]; emailRenderingServiceUrl = string.Format(emailRenderingServiceUrl, args[3]); List <Guid> userIds = File.ReadAllLines(args[2]).Select(elem => new Guid(elem)).ToList(); List <Guid> dealIds = File.ReadAllLines(args[4]).Select(elem => new Guid(elem)).ToList(); DispatchTrendingDeals(usersDal, emailJobsQueue, includeUsers, userIds, emailRenderingServiceUrl, args[3], dealIds); } } else if (args[0] == "Statistics" && args.Length == 2) { CalculateEmailStatistics(usersDal, args[1]); } else { StringBuilder sb = new StringBuilder(); sb.AppendLine("Usage:"); sb.AppendLine("EmailJobsDispatcher create_users <users_info_file_path>"); sb.AppendLine("Or"); sb.AppendLine("EmailJobsDispatcher dispatch"); throw new Exception(sb.ToString()); } Log.Info("Email Jobs Dispatcher Completed"); Console.WriteLine("Email Jobs Dispatcher Completed"); Console.ReadKey(); }