/// <summary> /// Set up regular schedule for running jobs. /// </summary> private void SetUpSchedule() { var parameters = new Dictionary <string, object>(); parameters.Add("Method", "SetUpSchedule"); // Run async, so we are not holding up the ability to start the hosted service. Task.Run(async() => { // Continue to run the task until cancellation is requested. while (!_hostApplicationLifetime.ApplicationStopping.IsCancellationRequested) { // Get the recent tweets schedule from Api > Tweets > SyncRecentSchedule in appsettings.json. var importRecentTweetsSchedule = _apiConfiguration.Value.Tweets.SyncRecentSchedule.HasValue ? _apiConfiguration.Value.Tweets.SyncRecentSchedule.Value : new TimeSpan(0, 5, 0); var nextSchedule = Task.Delay(importRecentTweetsSchedule, _hostApplicationLifetime.ApplicationStopping); // Delay the schedule until the sync recent schedule has been reached. _logger.LogWithParameters(LogLevel.Information, string.Format("Set up schedule for importing recent tweets. The importing of recent tweets will happen every {0}", importRecentTweetsSchedule.ToString("h\\:mm\\:ss")), parameters); await nextSchedule; // Import recent tweets from the Twitter API. ImportRecentTweetsFromTwitterApiTask = new ImportRecentTweetsFromTwitterApiTask(_serviceProvider, _hostApplicationLifetime.ApplicationStopping); await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => ImportRecentTweetsFromTwitterApiTask.RunAsync(id))); } ; }); }
/// <summary> /// Starts the hosted service, running the tasks needed before the API can properly initalise. /// </summary> /// <param name="cancellationToken">Propagates notification that operations should be cancelled.</param> /// <returns>An instance of <see cref="Task"/>.</returns> public async Task StartAsync(CancellationToken cancellationToken) { var parameters = new Dictionary <string, object>(); parameters.Add("Method", "StartAsync"); _logger.LogWithParameters(LogLevel.Information, "Starting Twitter Integration Background Service...", parameters); // Create Tasks var importRulesFromTwitterTask = new ImportRulesFromTwitterApiTask(_serviceProvider, _hostApplicationLifetime.ApplicationStopping); ImportRecentTweetsFromTwitterApiTask = new ImportRecentTweetsFromTwitterApiTask(_serviceProvider, _hostApplicationLifetime.ApplicationStopping); ListenForRealtimeTweetsFromTwitterApiTask = new ListenForRealtimeTweetsFromTwitterApiTask(_serviceProvider, _hostApplicationLifetime.ApplicationStopping); // Process Tweet when Recieved ListenForRealtimeTweetsFromTwitterApiTask.OnTweetReceived += async(tweetResponse) => { // When a tweet is received run the 'OnRealtimeTweetReceivedTask' task. await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => new OnRealtimeTweetReceivedTask(_serviceProvider, _hostApplicationLifetime.ApplicationStopping, tweetResponse).RunAsync(id))); }; // Run Tasks for importing rules, listening for realtime tweets, and importing recent tweets. await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => importRulesFromTwitterTask.RunAsync(id))); await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => ListenForRealtimeTweetsFromTwitterApiTask.RunAsync(id))); await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => ImportRecentTweetsFromTwitterApiTask.RunAsync(id))); // Schedule a task to import for the recent tweets at regular intervals. SetUpSchedule(); _logger.LogWithParameters(LogLevel.Information, "Started Twitter Integration Background Service", parameters); }
/// <summary> /// Runs the recent tweets task for a particular dashboard. /// </summary> /// <param name="dashboard"></param> /// <returns></returns> protected async Task ImportTweetsForDashboard(Dashboard dashboard) { var parameters = new Dictionary <string, object>(); parameters.Add("Method", "UpdateTweetsForDashboard"); parameters.Add("DB Dashboard ID", dashboard.Id); try { _logger.LogWithParameters(LogLevel.Information, "Start importing tweets for nearly created dashboard.", parameters); var importRecentTweetsFromTwitterApiTask = new ImportRecentTweetsFromTwitterApiTask(_serviceProvider, new System.Threading.CancellationTokenSource().Token, dashboard); // Creates the task. await _twitterIntegrationJobService.RunJobAsync(new TwitterIntegrationJob((Guid id) => importRecentTweetsFromTwitterApiTask.RunAsync(id))); // Runs the job. _logger.LogWithParameters(LogLevel.Information, "Finish importing tweets for nearly created dashboard.", parameters); } catch (Exception exception) { // Log any exception. _logger.LogWithParameters(LogLevel.Error, exception, exception.Message, parameters); throw; } }