Example #1
0
        private async Task InitAsync(IDirectory rootDir)
        {
            while (true)
            {
                var awaitTask = rootDir.AwaitChange("*.md", Duration.FromHours(1));
                var files     = await rootDir.ListFilesAsync().ConfigureAwait(_th);

                var postList = new List <Post>();
                foreach (var file in files.Where(x => x.Id.EndsWith(".md")))
                {
                    try
                    {
                        using (var content = await rootDir.ReadAsync(file.Id).ConfigureAwait(_th))
                        {
                            var post = await Post.CreateAsync(_th, _time, content).ConfigureAwait(_th);

                            postList.Add(post);
                        }
                    }
                    catch (Exception e)
                    {
                        // TODO: Use logging.
                        Console.WriteLine();
                        Console.WriteLine($"Error load blog post: '{file.Id}'");
                        Console.WriteLine(e);
                        Console.WriteLine();
                    }
                }
                var posts = postList.Where(x => x != null).ToDictionary(x => x.Id);
                Interlocked.Exchange(ref _posts, posts);
                _initTcs.TrySetResult(0);
                await awaitTask.ConfigureAwait(_th);
            }
        }
Example #2
0
        public static async Task <bool> AwaitChange(this IDirectory dir, string filter, Duration timeout, CancellationToken ct = default)
        {
            var th = dir.TaskHelper;

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(ct))
            {
                var timeoutTask = dir.TaskHelper.Delay(timeout, cts.Token);
                var changeTask  = dir.AwaitChange(filter, cts.Token);
                var task        = await th.WhenAny(new [] { timeoutTask, changeTask }).ConfigureAwait(th);

                cts.Cancel();
                ct.ThrowIfCancellationRequested();
                return(task == timeoutTask);
            }
        }