Ejemplo n.º 1
0
        public async Task<JarvisUpdateInfo> CheckForUpdates()
        {
            try
            {
                _log.Verbose("Getting releases from GitHub.");
                var uri = new Uri("https://api.github.com/repos/spectresystems/jarvis/releases");
                var response = await _client.GetAsync(uri);
                if (!response.IsSuccessStatusCode)
                {
                    _log.Warning($"GitHub returned status code {response.StatusCode} ({(int)response.StatusCode}) when checking for updates.");
                    return null;
                }

                var json = await response.Content.ReadAsStringAsync();

                // Get the last release.
                var result = JsonConvert.DeserializeObject<List<GitHubRelease>>(json)
                    .Where(x => !x.Draft);

                // Should we not include previews?
                if (!_settings.Get<bool>(Constants.Settings.General.IncludePreviews))
                {
                    result = result.Where(x => !x.Prerelease);
                }

                // Return the latest version.
                return result.OrderByDescending(x => x.PublishedAt)
                    .FirstOrDefault()?.ToJarvisUpdateInfo(_currentVersion);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "An exception occured while checking for updates at GitHub.");
                return null;
            }
        }
Ejemplo n.º 2
0
        public Task <bool> Run(CancellationToken token)
        {
            return(Task.Run(() =>
            {
                while (true)
                {
                    var indexingWatch = new Stopwatch();
                    indexingWatch.Start();

                    var result = LoadResults(token);

                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    _log.Debug("Updating index...");
                    var indexUpdateWatch = new Stopwatch();
                    indexUpdateWatch.Start();

                    var trie = new Trie <IndexedEntry>();
                    foreach (var file in result)
                    {
                        // Index individual words as well as combinations.
                        Index(trie, file.Title, file);
                        Index(trie, file.Description, file);

                        // Also index the whole words without tokenization.
                        trie.Insert(file.Title, file);

                        // Is this a file path?
                        if (file is IHasPath entryWithPath)
                        {
                            if (entryWithPath.Path is FilePath filePath)
                            {
                                // Index individual words as well as combinations.
                                Index(trie, filePath.GetFilenameWithoutExtension().FullPath, file);
                                Index(trie, filePath.GetFilename().RemoveExtension().FullPath, file);

                                // Also index the whole words without tokenization.
                                trie.Insert(filePath.GetFilenameWithoutExtension().FullPath, file);
                                trie.Insert(filePath.GetFilename().FullPath, file);
                            }
                        }
                    }

                    indexUpdateWatch.Stop();
                    _log.Debug($"Building trie took {indexUpdateWatch.ElapsedMilliseconds}ms");

                    _log.Debug("Writing index...");
                    Interlocked.Exchange(ref _trie, trie);

                    _log.Verbose($"Nodes: {_trie.NodeCount}");
                    _log.Verbose($"Items: {_trie.ItemCount}");

                    indexingWatch.Stop();
                    _log.Debug($"Indexing done. Took {indexingWatch.ElapsedMilliseconds}ms");

                    // Wait for a while.
                    var index = WaitHandle.WaitAny(new[] { token.WaitHandle, _trigger }, (int)TimeSpan.FromMinutes(5).TotalMilliseconds);
                    if (index == 0)
                    {
                        _log.Information("We were instructed to stop (2).");
                        break;
                    }

                    // Triggered update?
                    if (index == 1)
                    {
                        _log.Information("A re-index was triggered.");
                        _trigger.Reset();
                    }
                }

                return true;
            }, token));
        }
Ejemplo n.º 3
0
        public async Task <bool> Run(CancellationToken token)
        {
            while (true)
            {
                // Ask all sources for files.
                var result = new HashSet <IndexedEntry>();
                foreach (var source in _sources)
                {
                    if (token.WaitHandle.WaitOne((int)TimeSpan.FromMinutes(0).TotalMilliseconds))
                    {
                        _log.Information("We were instructed to stop (1). Aborting indexing...");
                        break;
                    }

                    _log.Debug($"Running '{source.Name}' indexer...");
                    foreach (var file in source.Index())
                    {
                        result.Add(file);
                    }
                }

                _log.Debug("Updating index...");
                var trie = new Trie <IndexedEntry>();
                foreach (var file in result)
                {
                    // Index individual words as well as combinations.
                    Index(trie, file.Title, file);
                    Index(trie, file.Description, file);

                    // Also index the whole words without tokenization.
                    trie.Insert(file.Title, file);

                    // Is this a file path?
                    if (file is IHasPath entryWithPath)
                    {
                        if (entryWithPath.Path is FilePath filePath)
                        {
                            // Index individual words as well as combinations.
                            Index(trie, filePath.GetFilenameWithoutExtension().FullPath, file);
                            Index(trie, filePath.GetFilename().RemoveExtension().FullPath, file);

                            // Also index the whole words without tokenization.
                            trie.Insert(filePath.GetFilenameWithoutExtension().FullPath, file);
                            trie.Insert(filePath.GetFilename().FullPath, file);
                        }
                    }
                }

                _log.Debug("Writing index...");
                using (await _lock.WriterLockAsync(token))
                {
                    _trie = trie;
                }

                _log.Verbose($"Nodes: {_trie.NodeCount}");
                _log.Verbose($"Items: {_trie.ItemCount}");

                // Wait for a minute.
                _log.Debug("Indexing done.");
                if (token.WaitHandle.WaitOne((int)TimeSpan.FromMinutes(5).TotalMilliseconds))
                {
                    _log.Information("We were instructed to stop (2).");
                    break;
                }
            }

            return(true);
        }