Ejemplo n.º 1
0
        public async Task Handle(AddSampleRatings busCommand)
        {
            // Get some user ids and video ids to rate with those users
            List <Guid> userIds = await _sampleDataRetriever.GetRandomSampleUserIds(busCommand.NumberOfRatings).ConfigureAwait(false);

            if (userIds.Count == 0)
            {
                Logger.Warn("No sample users available.  Cannot add sample ratings.");
                return;
            }

            List <Guid> videoIds = await _sampleDataRetriever.GetRandomVideoIds(busCommand.NumberOfRatings).ConfigureAwait(false);

            if (videoIds.Count == 0)
            {
                Logger.Warn("No sample videos available.  Cannot add sample ratings.");
                return;
            }

            // Rate some videos in parallel
            var ratingTasks = new List <Task>();
            var random      = new Random();

            for (int i = 0; i < busCommand.NumberOfRatings; i++)
            {
                ratingTasks.Add(_ratingsService.RateVideo(new RateVideo
                {
                    UserId  = userIds[i],
                    VideoId = videoIds[i],
                    Rating  = random.Next(1, 6)
                }));
            }

            await Task.WhenAll(ratingTasks).ConfigureAwait(false);
        }
        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);
            }
        }
Ejemplo n.º 3
0
        public async Task Handle(AddSampleComments busCommand)
        {
            // Get some sample users to use as comment authors
            List <Guid> userIds = await _sampleDataRetriever.GetRandomSampleUserIds(busCommand.NumberOfComments).ConfigureAwait(false);

            if (userIds.Count == 0)
            {
                Logger.Warn("No sample users available.  Cannot add sample comments.");
                return;
            }

            // Get some videos to comment on
            List <Guid> videoIds = await _sampleDataRetriever.GetRandomVideoIds(busCommand.NumberOfComments).ConfigureAwait(false);

            if (videoIds.Count == 0)
            {
                Logger.Warn("No sample videos available.  Cannot add sample comments.");
                return;
            }

            // Choose a NLipsum generator for generating the comment text
            int lipsumIdx = new Random().Next(LipsumSources.Length);
            var lipsum    = new LipsumGenerator(LipsumSources[lipsumIdx](), false);
            var comments  = lipsum.GenerateParagraphs(busCommand.NumberOfComments, Paragraph.Short);

            // Add some sample comments in parallel
            var commentTasks = new List <Task>();

            for (int i = 0; i < busCommand.NumberOfComments; i++)
            {
                commentTasks.Add(_commentService.CommentOnVideo(new CommentOnVideo
                {
                    CommentId = TimeUuid.NewId(),
                    VideoId   = videoIds[i],
                    UserId    = userIds[i],
                    Comment   = comments[i]
                }));
            }

            await Task.WhenAll(commentTasks).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);
        }