Ejemplo n.º 1
0
        protected int PostConsumeCleanup(ClientActiveTasks cleanup)
        {
            lock (_inProgress)
            {
                foreach (object id in cleanup)

                {
                    bool removed = _inProgress[_queueName].Remove(id);
                    if (!removed)
                    {
                        _logger.LogError($"_inProgress Q {_queueName} failed to remove '{id}'");
                    }
                }
            }
            cleanup.Clear();
            return(0); //Func<> must declare a return type; cannot be void
        }
Ejemplo n.º 2
0
        protected override async Task OnConsume(string playlistId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, playlistId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                var playlist = await _context.Playlists.FindAsync(playlistId);

                List <Media> medias = new List <Media>();
                switch (playlist.SourceType)
                {
                case SourceType.Echo360: medias = await GetEchoPlaylist(playlist, _context); break;

                case SourceType.Youtube: medias = await GetYoutubePlaylist(playlist, _context); break;

                case SourceType.Local: medias = await GetLocalPlaylist(playlist, _context); break;

                case SourceType.Kaltura: medias = await GetKalturaPlaylist(playlist, _context); break;

                case SourceType.Box: medias = await GetBoxPlaylist(playlist, _context); break;
                }
                // TASK DEPENDENCY (REFACTOR)
                medias.ForEach(m => _downloadMediaTask.Publish(m.Id));
            }
        }
Ejemplo n.º 3
0
        protected async override Task OnConsume(string example, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, "ExampleTask"); // may throw AlreadyInProgress exception
            _logger.LogInformation("Example Task Starting");
            int captionCount       = 0;
            int transcriptionCount = 0;

            using (var _context = CTDbContext.CreateDbContext())
            {
                CaptionQueries captionQueries = new CaptionQueries(_context);

                var transcriptions = await _context.Transcriptions.Take(30).ToListAsync();

                foreach (var transcription in transcriptions)
                {
                    var transcriptionId = transcription.Id;
                    var videoID         = transcription.VideoId;
                    var captions        = await captionQueries.GetCaptionsAsync(transcriptionId);

                    _logger.LogInformation($"{transcription.Id}: Caption count= {captions.Count}");
                    transcriptionCount++;
                }
            }

            _logger.LogInformation($"Example Task Done.  transcriptionCount={transcriptionCount} captionCount={captionCount}");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// [2020.7.7] For the purpose of resuming failed transcriptions, all the captions of the failed transcription would now be stored in the database.
        /// Before resuming, all the old captions would be queried out from the database and stored in a dictionary, which will be passed to the
        /// transcription function along with the resume time point.
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="taskParameters"></param>
        /// <returns></returns>
        protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId); // may throw AlreadyInProgress exception
            if (Globals.appSettings.MOCK_RECOGNITION == "MOCK")
            {
                buildMockCaptions(videoId);
            }


            using (var _context = CTDbContext.CreateDbContext())
            {
                // TODO: taskParameters.Force should wipe all captions and reset the Transcription Status

                Video video = await _context.Videos.Include(v => v.Video1).Where(v => v.Id == videoId).FirstAsync();

                // ! Note the 'Include' ; we don't build the whole tree of related Entities

                if (video.TranscriptionStatus == Video.TranscriptionStatusMessages.NOERROR)
                {
                    GetLogger().LogInformation($"{videoId}:Skipping Transcribing of- already complete");
                    return;
                }

                // GetKey can throw if the video.Id is currently being transcribed
                // However registerTask should have already detected that
                Key key = TaskEngineGlobals.KeyProvider.GetKey(video.Id);

                video.TranscribingAttempts += 10;
                await _context.SaveChangesAsync();

                try
                {
                    // create Dictionary and pass it to the recognition function
                    var captionsMap = new Dictionary <string, List <Caption> >();


                    // Set Source Language and Target (translation) Languages
                    var sourceLanguage = String.IsNullOrWhiteSpace(Globals.appSettings.SPEECH_RECOGNITION_DIALECT) ?
                                         Languages.ENGLISH_AMERICAN : Globals.appSettings.SPEECH_RECOGNITION_DIALECT.Trim();

                    var translations = new List <string> {
                        Languages.ENGLISH_AMERICAN, Languages.SIMPLIFIED_CHINESE, Languages.KOREAN, Languages.SPANISH, Languages.FRENCH
                    };
                    if (!String.IsNullOrWhiteSpace(Globals.appSettings.LANGUAGE_TRANSLATIONS))
                    {
                        translations = Globals.appSettings.LANGUAGE_TRANSLATIONS.Split(',').ToList();
                    }
                    GetLogger().LogInformation($"{videoId}: ({sourceLanguage}). Translation(s) = ({String.Join(',', translations)})");


                    // Different languages may not be as complete
                    // So find the minimum timespan of the max observed ending for each language
                    TimeSpan shortestTime  = TimeSpan.MaxValue; // Cant use TimeSpan.Zero to mean unset
                    var      startAfterMap = new Dictionary <string, TimeSpan>();

                    var allLanguages = new List <string>(translations);
                    allLanguages.Add(sourceLanguage);

                    foreach (string language in allLanguages)
                    {
                        var existing = await _captionQueries.GetCaptionsAsync(video.Id, language);

                        captionsMap[language] = existing;

                        startAfterMap[language] = TimeSpan.Zero;
                        if (existing.Any())
                        {
                            TimeSpan lastCaptionTime = existing.Select(c => c.End).Max();
                            startAfterMap[language] = lastCaptionTime;

                            GetLogger().LogInformation($"{ videoId}:{language}. Last Caption at {lastCaptionTime}");
                        }
                    }


                    //var lastSuccessTime = shortestTime < TimeSpan.MaxValue ? shortestTime : TimeSpan.Zero;
                    //if (video.JsonMetadata != null && video.JsonMetadata["LastSuccessfulTime"] != null)
                    //{
                    //    lastSuccessTime = TimeSpan.Parse(video.JsonMetadata["LastSuccessfulTime"].ToString());
                    //}


                    var result = await _msTranscriptionService.RecognitionWithVideoStreamAsync(videoId, video.Video1, key, captionsMap, sourceLanguage, startAfterMap);

                    if (video.JsonMetadata == null)
                    {
                        video.JsonMetadata = new JObject();
                    }

                    TaskEngineGlobals.KeyProvider.ReleaseKey(key, video.Id);

                    foreach (var captionsInLanguage in result.Captions)
                    {
                        var theLanguage = captionsInLanguage.Key;
                        var theCaptions = captionsInLanguage.Value;

                        if (theCaptions.Any())
                        {
                            var t = _context.Transcriptions.SingleOrDefault(t => t.VideoId == video.Id && t.Language == theLanguage);
                            GetLogger().LogInformation($"Find Existing Transcriptions null={t == null}");
                            // Did we get the default or an existing Transcription entity?
                            if (t == null)
                            {
                                t = new Transcription()
                                {
                                    Captions = theCaptions,
                                    Language = theLanguage,
                                    VideoId  = video.Id
                                };
                                _context.Add(t);
                            }
                            else
                            {
                                // Transcriptions already existed, we are just completing them, so add only the new ones
                                // TODO/TOREVIEW: Does this filter actually help, if existing caption entries were edited by hand?
                                var newCaptions = theCaptions.Where(c => c.Id == null);

                                t.Captions.AddRange(newCaptions);
                            }
                        }
                    }


                    video.TranscriptionStatus = result.ErrorCode;
                    video.JsonMetadata["LastSuccessfulTime"] = result.LastSuccessTime.ToString();


                    await _context.SaveChangesAsync();

                    _sceneDetectionTask.Publish(video.Id);
                    video.Transcriptions.ForEach(t => _generateVTTFileTask.Publish(t.Id));
                }
                catch (Exception ex)
                {
                    GetLogger().LogError(ex, $"{videoId}: Transcription Exception:${ex.StackTrace}");
                    video.TranscribingAttempts += 1000;
                    await _context.SaveChangesAsync();

                    throw;
                }
            }
        }
Ejemplo n.º 5
0
        protected async override Task OnConsume(string example, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, "BuildElasticIndexTask"); // may throw AlreadyInProgress exception
            GetLogger().LogInformation("BuildElasticIndexTask Starting");

            using (var _context = CTDbContext.CreateDbContext())
            {
                CaptionQueries captionQueries = new CaptionQueries(_context);

                var all_transcriptions = await _context.Transcriptions.Where(t => t.Language == Languages.ENGLISH_AMERICAN).ToListAsync();

                foreach (var transcription in all_transcriptions)
                {
                    var all_captions = transcription.Captions;

                    // each index has the unique name "index_string_unique", the current in use one has the alias "index_string_alias"
                    var index_string_base   = transcription.Id + "_" + Languages.ENGLISH_AMERICAN.ToLower(System.Globalization.CultureInfo.CurrentCulture);
                    var index_string_unique = index_string_base + "_" + $"{DateTime.Now:yyyyMMddHHmmss}";
                    var index_string_alias  = index_string_base + "_" + "primary";

                    var asyncBulkIndexResponse = await _client.BulkAsync(b => b
                                                                         .Index(index_string_unique)
                                                                         .IndexMany(all_captions)
                                                                         );

                    var alias_exist = await _client.Indices.ExistsAsync(index_string_alias);

                    if (alias_exist.Exists)
                    {
                        var oldIndices = await _client.GetIndicesPointingToAliasAsync(index_string_alias);

                        var oldIndexName = oldIndices.First().ToString();

                        var indexResponse = await _client.Indices.BulkAliasAsync(new BulkAliasRequest
                        {
                            Actions = new List <IAliasAction>
                            {
                                new AliasRemoveAction {
                                    Remove = new AliasRemoveOperation {
                                        Index = oldIndexName, Alias = index_string_alias
                                    }
                                },
                                new AliasAddAction {
                                    Add = new AliasAddOperation {
                                        Index = index_string_unique, Alias = index_string_alias
                                    }
                                }
                            }
                        });
                    }
                    else
                    {
                        var putAliasResponse = await _client.Indices.PutAliasAsync(new PutAliasRequest(index_string_unique, index_string_alias));
                    }
                }
            }

            GetLogger().LogInformation("BuildElasticIndexTask Done");
        }
Ejemplo n.º 6
0
#pragma warning disable 1998
        protected async override Task OnConsume(string emptyString, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            // Maybe in the future if we use this task again: registerTask(cleanup, "RefreshAccessTokenAsync"); // may throw AlreadyInProgress exception
            // no. xx nope await _box.RefreshAccessTokenAsync();
            // refreshing the Box access token caused the token to go stale
            // We've had a better experience not refreshing it
        }
Ejemplo n.º 7
0
 protected async override Task OnConsume(string authCode, TaskParameters taskParameters, ClientActiveTasks cleanup)
 {
     registerTask(cleanup, "CreateAccessTokenAsync");
     await _box.CreateAccessTokenAsync(authCode);
 }
Ejemplo n.º 8
0
        /// <summary>Finds incomplete tasks and adds them all a TaskItem table.
        /// This appears to be defunct and not yet used code - grep FindPendingJobs, found no callers of this function
        /// </summary>
        //       private async Task FindPendingJobs()
        // {
        //     using (var context = CTDbContext.CreateDbContext())
        //     {
        //         // Medias for which no videos have downloaded
        //         var toDownloadMediaIds = await context.Medias.Where(m => m.Video == null).Select(m =>
        //             new TaskItem
        //             {
        //                 UniqueId = m.Id,
        //                 ResultData = new JObject(),
        //                 TaskParameters = new JObject(),
        //                 TaskType = TaskType.DownloadMedia,
        //                 Attempts = 0
        //             }).ToListAsync();

        //         // Videos which haven't been converted to wav
        //         var toConvertVideoIds = await context.Videos.Where(v => v.Medias.Any() && v.Audio == null).Select(v =>
        //             new TaskItem
        //             {
        //                 UniqueId = v.Id,
        //                 ResultData = new JObject(),
        //                 TaskParameters = new JObject(),
        //                 TaskType = TaskType.ConvertMedia,
        //                 Attempts = 0
        //             }).ToListAsync();

        //         // Transcribe pending videos.
        //         var toTranscribeVideoIds = await context.Videos.Where(v => v.TranscribingAttempts < 3 &&
        //                                                                    v.TranscriptionStatus != "NoError" &&
        //                                                                    v.Medias.Any() && v.Audio != null).Select(v =>
        //                                                                    new TaskItem
        //                                                                    {
        //                                                                        UniqueId = v.Id,
        //                                                                        ResultData = new JObject(),
        //                                                                        TaskParameters = new JObject(),
        //                                                                        TaskType = TaskType.Transcribe,
        //                                                                        Attempts = 0
        //                                                                    }).ToListAsync();

        //         // Completed Transcriptions which haven't generated vtt files
        //         var toGenerateVTTsTranscriptionIds = await context.Transcriptions.Where(t => t.Captions.Count > 0 && t.File == null)
        //                                                                         .Select(t =>
        //                                                                         new TaskItem
        //                                                                         {
        //                                                                             UniqueId = t.Id,
        //                                                                             ResultData = new JObject(),
        //                                                                             TaskParameters = new JObject(),
        //                                                                             TaskType = TaskType.GenerateVTTFile,
        //                                                                             Attempts = 0
        //                                                                         }).ToListAsync();

        //         var allTaskItems = new List<TaskItem>();
        //         allTaskItems.AddRange(toDownloadMediaIds);
        //         allTaskItems.AddRange(toConvertVideoIds);
        //         allTaskItems.AddRange(toTranscribeVideoIds);
        //         allTaskItems.AddRange(toGenerateVTTsTranscriptionIds);

        //         foreach(var taskItem in allTaskItems)
        //         {
        //             if(!await context.TaskItems.AnyAsync(t => t.TaskType == taskItem.TaskType && t.UniqueId == taskItem.UniqueId))
        //             {
        //                 await context.TaskItems.AddAsync(taskItem);
        //             }
        //         }
        //         await context.SaveChangesAsync();
        //     }
        // }
        /// <summary> Used by the PeriodicCheck to identify and enqueue missing tasks.
        /// This Task is started after all playlists are updated.
        /// </summary>
        private async Task PendingJobs()
        {
            // Update Box Token every few hours
            _updateBoxTokenTask.Publish("");

            //We will use these outside of the DB scope
            List <String> todoVTTs;
            List <String> todoProcessVideos;
            List <String> todoTranscriptions;
            List <String> todoDownloads;

            using (var context = CTDbContext.CreateDbContext())
            {
                // Most tasks are created directly from within a task when it normally completed.
                // This code exists to detect missing items and to publish tasks to complete them
                // A redesigned taskengine should not have the direct coupling inside each task

                // Since downloading a video could also create a Video, it is better to do these with little time delay in-between and then publish all the tasks
                // I believe there is still a race condition: Prior to this, we've just polled all active playlists and at least one of these may have already completed
                // So let's only consider items that are older than 10 minutes
                // Okay this is bandaid on the current design until we redesign the taskengine
                // Ideas For the future:
                // * Consider setting TTL on these messages to be 5 minutes short of thethe Periodic Refresh?
                // * If/when we drop the direct appoach consider: Random ordering. Most recent first (or randomly choosing either)

                // If an object was created during the middle of a periodic cycle, give it a full cycle to queue, and another cycle to complete its tasks


                int minutesCutOff = Math.Max(1, Convert.ToInt32(Globals.appSettings.PERIODIC_CHECK_OLDER_THAN_MINUTES));


                var tooRecentCutoff = DateTime.Now.AddMinutes(-minutesCutOff);
                // This is the first use of 'AsNoTracking' in this project; let's check it works in Production as expected

                // TODO/TOREVIEW: Does EF create the complete entity and then project out the ID column in dot Net, or does it request only the ID from the database?
                // TODO/TOREVIEW: Since this code  just pulls the IDs from the database, I expect this will be harmless no-op, however all DB reads should use AsNoTracking as a best practice
                // See https://code-maze.com/queries-in-entity-framework-core/
                // See https://docs.microsoft.com/en-us/ef/core/querying/tracking


                // Completed Transcriptions which haven't generated vtt files
                // TODO: Should also check dates too
                GetLogger().LogInformation($"Finding incomplete VTTs, Transcriptions and Downloads from before {tooRecentCutoff}, minutesCutOff=({minutesCutOff})");


                // Todo Could also check for secondary video too
                todoProcessVideos = await context.Videos.AsNoTracking().Where(
                    v => (v.Duration == null && !String.IsNullOrEmpty(v.Video1Id))
                    ).OrderByDescending(t => t.CreatedAt).Select(e => e.Id).ToListAsync();

                todoVTTs = await context.Transcriptions.AsNoTracking().Where(
                    t => t.Captions.Count > 0 && t.File == null && t.CreatedAt < tooRecentCutoff
                    ).OrderByDescending(t => t.CreatedAt).Select(e => e.Id).ToListAsync();

                todoTranscriptions = await context.Videos.AsNoTracking().Where(
                    v => v.TranscribingAttempts < 1 && v.TranscriptionStatus != "NoError" && v.Medias.Any() && v.CreatedAt < tooRecentCutoff
                    ).OrderByDescending(t => t.CreatedAt).Select(e => e.Id).ToListAsync();

                // Medias for which no videos have downloaded
                todoDownloads = await context.Medias.AsNoTracking().Where(
                    m => m.Video == null && m.CreatedAt < tooRecentCutoff
                    ).OrderByDescending(t => t.CreatedAt).Select(e => e.Id).ToListAsync();
            }
            // We have a list of outstanding tasks
            // However some of these may already be in progress
            // So don't queue theses

            GetLogger().LogInformation($"Found {todoProcessVideos.Count},{todoVTTs.Count},{todoTranscriptions.Count},{todoDownloads.Count} counts before filtering");
            ClientActiveTasks currentProcessVideos = _processVideoTask.GetCurrentTasks();

            todoProcessVideos.RemoveAll(e => currentProcessVideos.Contains(e));


            ClientActiveTasks currentVTTs = _generateVTTFileTask.GetCurrentTasks();

            todoVTTs.RemoveAll(e => currentVTTs.Contains(e));

            ClientActiveTasks currentTranscription = _transcriptionTask.GetCurrentTasks();

            todoTranscriptions.RemoveAll(e => currentTranscription.Contains(e));

            ClientActiveTasks currentDownloads = _transcriptionTask.GetCurrentTasks();

            todoDownloads.RemoveAll(e => currentDownloads.Contains(e));

            GetLogger().LogInformation($"Current In progress  {currentProcessVideos.Count},{currentVTTs.Count},{currentTranscription.Count},{currentDownloads.Count} counts after filtering");
            GetLogger().LogInformation($"Found {todoProcessVideos.Count},{todoVTTs.Count},{todoTranscriptions.Count},{todoDownloads.Count} counts after filtering");


            // Now we have a list of new things we want to do
            GetLogger().LogInformation($"Publishing processingVideos ({String.Join(",", todoProcessVideos)})");

            todoProcessVideos.ForEach(t => _processVideoTask.Publish(t));

            GetLogger().LogInformation($"Publishing todoVTTs ({String.Join(",", todoVTTs)})");

            todoVTTs.ForEach(t => _generateVTTFileTask.Publish(t));

            GetLogger().LogInformation($"Publishing todoTranscriptions ({String.Join(",", todoTranscriptions)})");

            todoTranscriptions.ForEach(v => _transcriptionTask.Publish(v));

            GetLogger().LogInformation($"Publishing todoDownloads ({String.Join(",", todoDownloads)})");

            todoDownloads.ForEach(m => _downloadMediaTask.Publish(m));

            //// Not used Videos which haven't been converted to wav
            /// Code Not deleted because one day we will just reuse the one wav file and use an offset into that file
            //(await context.Videos.Where(v => v.Medias.Any() && v.Audio == null).ToListAsync()).ForEach(v => _convertVideoToWavTask.Publish(v.Id));
            // Videos which have failed in transcribing
            GetLogger().LogInformation("Pending Jobs - completed");
        }
Ejemplo n.º 9
0
        protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId); // may throw AlreadyInProgress exception
            Video video;
            bool  videoUpdated = false;

            using (var _context = CTDbContext.CreateDbContext())
            {
                video = await _context.Videos.Include(v => v.Video1)
                        .Include(v => v.Video2)
                        .Include(v => v.ProcessedVideo1)
                        .Include(v => v.ProcessedVideo2)
                        .Where(v => v.Id == videoId).FirstAsync();
            }
            GetLogger().LogInformation("Consuming" + video);
            if (video.Duration == null && video.Video1 != null)
            {
                var mediaInfoResult = await _rpcClient.PythonServerClient.GetMediaInfoRPCAsync(new CTGrpc.File
                {
                    FilePath = video.Video1.VMPath
                });

                var mediaJson = JObject.Parse(mediaInfoResult.Json);
                video.FileMediaInfo = mediaJson;
                video.UpdateMediaProperties();
                videoUpdated = true;
            }
            bool runbrokencode = false;

            if (runbrokencode)
            {
                if (video.Video1 != null)
                {
                    if (video.ProcessedVideo1 == null || taskParameters.Force)
                    {
                        var file = await _rpcClient.PythonServerClient.ProcessVideoRPCAsync(new CTGrpc.File
                        {
                            FilePath = video.Video1.VMPath
                        });

                        //This does not work
                        video.ProcessedVideo1 = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                        videoUpdated = true;
                    }
                }
                if (video.Video2 != null)
                {
                    if (video.ProcessedVideo2 == null || taskParameters.Force)
                    {
                        var file = await _rpcClient.PythonServerClient.ProcessVideoRPCAsync(new CTGrpc.File
                        {
                            FilePath = video.Video2.VMPath
                        });

                        //This does not work
                        video.ProcessedVideo2 = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                        videoUpdated = true;
                    }
                }
            }
            if (videoUpdated)
            {
                using (var _context = CTDbContext.CreateDbContext())
                {
                    _context.Entry(video).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 10
0
        protected async override Task OnConsume(string example, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, "CleanUpElasticIndexTask"); // may throw AlreadyInProgress exception
            GetLogger().LogInformation("CleanUpElasticIndexTask Starting");

            var result = await _client.Indices.GetAsync(new GetIndexRequest(Indices.All));

            var indices = result.Indices;

            foreach (var index in indices)
            {
                try
                {
                    string index_name = index.Key.ToString();

                    // elastic search internal management index starts with '.'
                    if (index_name[0] == '.')
                    {
                        continue;
                    }

                    GetLogger().LogInformation("Running Index: " + index_name);
                    string[] parts = index_name.Split("_");

                    if (parts.Length != 3)
                    {
                        GetLogger().LogError("Parsing Error: invalid Index Name");
                        continue;
                    }

                    var index_string_id   = parts[0];
                    var index_string_lang = parts[1];
                    var index_string_time = parts[2];

                    // Indices that were created in the last 2 days would not be removed.
                    DateTime createdAt = DateTime.ParseExact(index_string_time, "yyyyMMddHHmmss", null);
                    GetLogger().LogInformation("Created at: " + createdAt.ToString());
                    DateTime range = DateTime.Now.AddMinutes(-_time_to_live);
                    if (DateTime.Compare(createdAt, range) >= 0)
                    {
                        GetLogger().LogInformation("Skipped: Index does not exceed time_to_live");
                        continue;
                    }

                    // If alias exist, this index is currently in use and should not be removed.
                    var index_string_alias = index_string_id + "_" + index_string_lang + "_" + "primary";
                    var cur_index          = await _client.GetIndicesPointingToAliasAsync(index_string_alias);

                    var cur_index_list = cur_index.ToList();
                    if (cur_index_list.Any() && cur_index_list[0].ToString() == index_name)
                    {
                        GetLogger().LogInformation("Skipped: Index is in use");
                        continue;
                    }

                    // remove index
                    GetLogger().LogInformation("Removing Index: " + index_name);
                    var resp = await _client.Indices.DeleteAsync(index_name);

                    GetLogger().LogInformation(resp.ToString());
                }
                catch (Exception e)
                {
                    GetLogger().LogError("Error: " + e.ToString());
                    continue;
                }
            }
            GetLogger().LogInformation("CleanUpElasticIndexTask Done");
        }
Ejemplo n.º 11
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                FileRecord existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                FileRecord existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);


                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                if (string.IsNullOrEmpty(transcription.FileId))
                {
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                if (string.IsNullOrEmpty(transcription.SrtFileId))
                {
                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();
            }
        }
Ejemplo n.º 12
0
        protected override async Task OnConsume(string mediaId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, mediaId); // may throw AlreadyInProgress exception

            Media media;

            using (var _context = CTDbContext.CreateDbContext())
            {
                media = await _context.Medias.Where(m => m.Id == mediaId)
                        .Include(m => m.Playlist).FirstAsync();
            }
            GetLogger().LogInformation("Consuming" + media);
            Video video = new Video();

            switch (media.SourceType)
            {
            case SourceType.Echo360: video = await DownloadEchoVideo(media); break;

            case SourceType.Youtube: video = await DownloadYoutubeVideo(media); break;

            case SourceType.Local: video = await DownloadLocalPlaylist(media); break;

            case SourceType.Kaltura: video = await DownloadKalturaVideo(media); break;

            case SourceType.Box: video = await DownloadBoxVideo(media); break;
            }
            // If no valid video1, or if a video2 object exists but not a valid file - fail the task.
            if (video == null || video.Video1 == null || !video.Video1.IsValidFile() ||
                (video.Video2 != null && !video.Video2.IsValidFile()))
            {
                throw new Exception("DownloadMediaTask failed for mediaId " + media.Id);
            }

            using (var _context = CTDbContext.CreateDbContext())
            {
                var latestMedia = await _context.Medias.FindAsync(media.Id);

                // Don't add video if there are already videos for the given media.
                if (latestMedia.Video == null)
                {
                    // Check if Video already exists, if yes link it with this media item.
                    var file = _context.FileRecords.Where(f => f.Hash == video.Video1.Hash).ToList();
                    if (!file.Any())
                    {
                        // Create new video Record
                        await _context.Videos.AddAsync(video);

                        await _context.SaveChangesAsync();

                        latestMedia.VideoId = video.Id;
                        await _context.SaveChangesAsync();

                        GetLogger().LogInformation("Downloaded:" + video);
                        _transcriptionTask.Publish(video.Id);
                        _processVideoTask.Publish(video.Id);
                    }
                    else
                    {
                        var existingVideos = await _context.Videos.Where(v => v.Video1Id == file.First().Id).ToListAsync();

                        // If file exists but video doesn't.
                        if (!existingVideos.Any())
                        {
                            // Delete existing file Record
                            await file.First().DeleteFileRecordAsync(_context);

                            // Create new video Record
                            await _context.Videos.AddAsync(video);

                            await _context.SaveChangesAsync();

                            latestMedia.VideoId = video.Id;
                            await _context.SaveChangesAsync();

                            GetLogger().LogInformation("Downloaded:" + video);
                            _transcriptionTask.Publish(video.Id);
                            _processVideoTask.Publish(video.Id);
                        }
                        // If video and file both exist.
                        else
                        {
                            var existingVideo = await _context.Videos.Where(v => v.Video1Id == file.First().Id).FirstAsync();

                            latestMedia.VideoId = existingVideo.Id;
                            await _context.SaveChangesAsync();

                            GetLogger().LogInformation("Existing Video:" + existingVideo);

                            // Deleting downloaded video as it's duplicate.
                            await video.DeleteVideoAsync(_context);
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        protected async override Task OnConsume(JObject jObject, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            using (var _context = CTDbContext.CreateDbContext())
            {
                var type = jObject["Type"].ToString();

                if (type == TaskType.PeriodicCheck.ToString())
                {
                    await _slackLogger.PostMessageAsync("Periodic Check.");

                    registerTask(cleanup, "PeriodicCheck");
                    _updateBoxTokenTask.Publish("");
                    _buildElasticIndexTask.Publish("");
                    _cleanUpElasticIndexTask.Publish("");
                    _exampleTask.Publish("");

                    await DownloadAllPlaylists();
                    await PendingJobs();
                }
                else if (type == TaskType.DownloadAllPlaylists.ToString())
                {
                    await DownloadAllPlaylists();
                }
                else if (type == TaskType.DownloadPlaylistInfo.ToString())
                {
                    var playlistId = jObject["PlaylistId"].ToString();
                    var playlist   = await _context.Playlists.FindAsync(playlistId);

                    _downloadPlaylistInfoTask.Publish(playlist.Id);
                }
                else if (type == TaskType.GenerateVTTFile.ToString())
                {
                    var transcriptionId = jObject["TranscriptionId"].ToString();
                    var transcription   = await _context.Transcriptions.FindAsync(transcriptionId);

                    _generateVTTFileTask.Publish(transcription.Id);
                }
                else if (type == TaskType.SceneDetection.ToString())
                {
                    var mediaId = jObject["mediaId"].ToString();
                    var media   = _context.Medias.Find(mediaId);
                    _scenedDetectionTask.Publish(media.Video.Id);
                }
                else if (type == TaskType.CreateBoxToken.ToString())
                {
                    var authCode = jObject["authCode"].ToString();
                    _createBoxTokenTask.Publish(authCode);
                }
                else if (type == TaskType.DownloadMedia.ToString())
                {
                    var mediaId = jObject["mediaId"].ToString();
                    var media   = await _context.Medias.FindAsync(mediaId);

                    _downloadMediaTask.Publish(media.Id);
                }
                //else if (type == TaskType.ConvertMedia.ToString())
                //{
                //    var videoId = jObject["videoId"].ToString();
                //    var video = await _context.Videos.FindAsync(videoId);
                //    _convertVideoToWavTask.Publish(video.Id);
                //}
                else if (type == TaskType.TranscribeVideo.ToString())
                {
                    var id = jObject["videoOrMediaId"].ToString();
                    GetLogger().LogInformation($"{type}:{id}");
                    var video = await _context.Videos.FindAsync(id);

                    if (video == null)
                    {
                        var media = await _context.Medias.FindAsync(id);

                        if (media != null)
                        {
                            GetLogger().LogInformation($"{id}: media Found. videoID=({media.VideoId})");
                            video = media.Video;
                        }
                    }
                    if (video == null)
                    {
                        GetLogger().LogInformation($"No video found for video/mediaId ({id})");
                        return;
                    }
                    //TODO: These properties should not be literal strings
                    bool deleteExisting = false;
                    try
                    {
                        deleteExisting = jObject["DeleteExisting"].Value <bool>();
                    }
                    catch (Exception) { }
                    if (deleteExisting)
                    {
                        GetLogger().LogInformation($"{id}:Removing Transcriptions for video ({video.Id})");

                        var transcriptions = video.Transcriptions;
                        _context.Transcriptions.RemoveRange(transcriptions);
                        video.TranscriptionStatus = "";
                        // Could also remove LastSuccessTime and reset attempts

                        await _context.SaveChangesAsync();
                    }
                    _transcriptionTask.Publish(video.Id);
                }
                else if (type == TaskType.UpdateOffering.ToString())
                {
                    var offeringId = jObject["offeringId"].ToString();
                    (await _context.Playlists.Where(o => o.OfferingId == offeringId).ToListAsync())
                    .ForEach(p => _downloadPlaylistInfoTask.Publish(p.Id));
                }
                else if (type == TaskType.ReTranscribePlaylist.ToString())
                {
                    var playlistId = jObject["PlaylistId"].ToString();

                    // Get all videos
                    var videos = await _context.Playlists.Where(p => p.Id == playlistId)
                                 .SelectMany(p => p.Medias)
                                 .Where(e => e != null)
                                 .Select(m => m.Video)
                                 .ToListAsync();

                    // Delete all captions. This caused a null pointer exception because some elements were null
                    // the above line and this line now have null filters
                    var captions = videos.SelectMany(v => v.Transcriptions)
                                   .Where(e => e != null)
                                   .SelectMany(t => t.Captions).ToList();

                    _context.Captions.RemoveRange(captions);
                    // TODO/TOREVIEW: No need to create in captions. Their IDs should be sufficient

                    // Delete all Transcriptions
                    var transcriptions = videos.SelectMany(v => v.Transcriptions).Where(e => e != null).ToList();
                    _context.Transcriptions.RemoveRange(transcriptions);

                    videos.ForEach(v =>
                    {
                        v.TranscribingAttempts = 0;
                        v.TranscriptionStatus  = null;
                    });

                    await _context.SaveChangesAsync();

                    videos.ForEach(v =>
                    {
                        _transcriptionTask.Publish(v.Id);
                    });
                }
            }
        }
        #pragma warning disable 1998
        //Tasks/ConvertVideoToWavTask.cs(32,39): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [/src/TaskEngine/TaskEngine.csproj]

        protected override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId);

            throw new Exception("ConvertVideoToWavTask No longer used. Videoid= " + videoId);
        }
Ejemplo n.º 15
0
 protected abstract Task OnConsume(T data, TaskParameters taskParameters, ClientActiveTasks cleanup);
Ejemplo n.º 16
0
        /// <summary>Extracts scene information for a video.
        /// Beware: It is possible to start another scene task while the first one is still running</summary>
        protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                Video video = await _context.Videos.FindAsync(videoId);

                if (video.SceneData == null || taskParameters.Force)
                {
                    var jsonString = await _rpcClient.PythonServerClient.GetScenesRPCAsync(new CTGrpc.File
                    {
                        FilePath = video.Video1.VMPath
                    });

                    JArray scenes = JArray.Parse(jsonString.Json);

                    video.SceneData = new JObject
                    {
                        { "Scenes", scenes }
                    };

                    await _context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 17
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception

            GetLogger().LogInformation($"Creating VTT & SRT files for ({transcriptionId})");

            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                FileRecord?existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                if (existingVtt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new vtt file {vttfile.FileName}");
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing vtt file contents {existingVtt.FileName}");
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                FileRecord?existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);

                if (existingSrt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new srt file {srtfile.FileName}");

                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing srt file contents {existingSrt.FileName}");
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();

                GetLogger().LogInformation($"{transcriptionId}: Database updated");
            }
        }