public async Task Handle(AddSampleYouTubeVideos busCommand) { // Get some sample users to be the authors for the videos we're going to add List <Guid> userIds = await _sampleDataRetriever.GetRandomSampleUserIds(busCommand.NumberOfVideos).ConfigureAwait(false); if (userIds.Count == 0) { Logger.Warn("No sample users available. Cannot add sample YouTube videos."); return; } // Get some unused sample videos List <YouTubeVideo> sampleVideos = await _youTubeManager.GetUnusedVideos(busCommand.NumberOfVideos).ConfigureAwait(false); // Add them to the site using sample users for (int idx = 0; idx < sampleVideos.Count; idx++) { YouTubeVideo sampleVideo = sampleVideos[idx]; Guid userId = userIds[idx]; // Submit the video await _videoCatalog.SubmitYouTubeVideo(new SubmitYouTubeVideo { VideoId = Guid.NewGuid(), UserId = userId, YouTubeVideoId = sampleVideo.YouTubeVideoId, Name = sampleVideo.Name, Description = sampleVideo.Description, Tags = sampleVideo.SuggestedTags }).ConfigureAwait(false); // Mark them as used so we make a best effort not to reuse sample videos and post duplicates await _youTubeManager.MarkVideoAsUsed(sampleVideo).ConfigureAwait(false); } }
protected override async Task RunImpl() { // When running for the first time, try to make sure we've refreshed videos from YouTube first and added some users before running int numberOfVideos = 1; if (IsFirstTimeRunning) { // Will throw if no videos are there yet, causing the job to be delayed and retried await _youTubeManager.GetUnusedVideos(10); // Check for users List <Guid> userIds = await _sampleDataRetriever.GetRandomSampleUserIds(10); if (userIds.Count == 0) { throw new InvalidOperationException("No sample users available yet. Waiting to run AddSampleYouTubeVideosJob."); } // Otherwise, we're good, add 10 sample videos the first time we run numberOfVideos = 10; } await _sampleDataService.AddSampleYouTubeVideos(new AddSampleYouTubeVideos { NumberOfVideos = numberOfVideos }).ConfigureAwait(false); }