Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,51 @@ <h2 class="project-tagline">{{ page.description | default: site.description | de
</header>

<main id="content" class="main-content" role="main">
{% assign sibling_count = 0 %}
{% assign section_home = nil %}
{% assign prev_page = nil %}
{% assign next_page = nil %}
{% if page.path contains 'docs/' and page.path != 'docs/README.md' %}
{% assign sibling_pages = site.pages | where_exp: "item", "item.path contains 'docs/' and item.path contains '.md' and item.dir == page.dir" | sort: "url" %}
{% assign section_home = sibling_pages | where_exp: "item", "item.name == 'README.md'" | first %}
{% assign prev_page = nil %}
{% assign next_page = nil %}
{% assign section_pages = site.pages | where: "dir", page.dir | sort: "url" %}
{% assign seen_current = false %}
<!-- ponytail: path-sorted sibling nav keeps docs alive; add curated order data only if alphabetical flow stops being useful. -->
{% for item in sibling_pages %}
{% if item.url == page.url %}
{% assign seen_current = true %}
{% elsif seen_current and next_page == nil %}
{% assign next_page = item %}
{% elsif seen_current == false %}
{% assign prev_page = item %}
{% for item in section_pages %}
{% if item.path contains 'docs/' and item.path contains '.md' %}
{% assign sibling_count = sibling_count | plus: 1 %}
{% if item.name == 'README.md' %}
{% assign section_home = item %}
{% endif %}
{% if item.url == page.url %}
{% assign seen_current = true %}
{% elsif seen_current == false %}
{% assign prev_page = item %}
{% elsif next_page == nil %}
{% assign next_page = item %}
{% endif %}
{% endif %}
{% endfor %}
<nav class="docs-nav" aria-label="文档导航">
<div class="docs-nav__links">
<span class="docs-nav__label">文档导航</span>
<a href="{{ '/docs/' | relative_url }}">文档中心</a>
{% if section_home and section_home.url != page.url and section_home.url != '/docs/' %}
<a href="{{ section_home.url | relative_url }}">本主题目录</a>
{% if section_home %}
{% if section_home.url != page.url and section_home.url != '/docs/' %}
<a href="{{ section_home.url | relative_url }}">本主题目录</a>
{% endif %}
{% endif %}
<a href="{{ '/' | relative_url }}">项目首页</a>
</div>
{% if sibling_pages.size > 1 %}
{% if sibling_count > 1 %}
<div class="docs-nav__row">
<span class="docs-nav__label">继续阅读</span>
{% for item in sibling_pages %}
{% assign item_title = item.title | default: item.name | replace: '.md', '' %}
{% if item.url == page.url %}
<span class="docs-nav__current">{{ item_title }}</span>
{% else %}
<a href="{{ item.url | relative_url }}">{{ item_title }}</a>
{% for item in section_pages %}
{% if item.path contains 'docs/' and item.path contains '.md' %}
{% assign item_title = item.title | default: item.name | replace: '.md', '' %}
{% if item.url == page.url %}
<span class="docs-nav__current">{{ item_title }}</span>
{% else %}
<a href="{{ item.url | relative_url }}">{{ item_title }}</a>
{% endif %}
{% endif %}
{% endfor %}
</div>
Expand All @@ -128,21 +139,23 @@ <h2 class="project-tagline">{{ page.description | default: site.description | de

{{ content }}

{% if page.path contains 'docs/' and page.path != 'docs/README.md' and sibling_pages.size > 1 %}
<nav class="docs-nav docs-nav__pager" aria-label="上一篇和下一篇">
<div class="docs-nav__pager-item">
{% if prev_page %}
<span class="docs-nav__label">上一篇</span><br>
<a href="{{ prev_page.url | relative_url }}">{{ prev_page.title | default: prev_page.name | replace: '.md', '' }}</a>
{% endif %}
</div>
<div class="docs-nav__pager-item docs-nav__pager-item--next">
{% if next_page %}
<span class="docs-nav__label">下一篇</span><br>
<a href="{{ next_page.url | relative_url }}">{{ next_page.title | default: next_page.name | replace: '.md', '' }}</a>
{% endif %}
</div>
</nav>
{% if page.path contains 'docs/' and page.path != 'docs/README.md' %}
{% if sibling_count > 1 %}
<nav class="docs-nav docs-nav__pager" aria-label="上一篇和下一篇">
<div class="docs-nav__pager-item">
{% if prev_page %}
<span class="docs-nav__label">上一篇</span><br>
<a href="{{ prev_page.url | relative_url }}">{{ prev_page.title | default: prev_page.name | replace: '.md', '' }}</a>
{% endif %}
</div>
<div class="docs-nav__pager-item docs-nav__pager-item--next">
{% if next_page %}
<span class="docs-nav__label">下一篇</span><br>
<a href="{{ next_page.url | relative_url }}">{{ next_page.title | default: next_page.name | replace: '.md', '' }}</a>
{% endif %}
</div>
</nav>
{% endif %}
{% endif %}

<footer class="site-footer">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,13 +30,13 @@ public BadPracticeBackgroundService(
public override Task StartAsync(CancellationToken cancellationToken)
{
// TODO:后台作业待配置化
_timer = new Timer(DoWork!, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
_timer = new Timer(DoWork, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
_queue = new Queue<Func<CancellationToken, ValueTask>>(queueSize);

return base.StartAsync(cancellationToken);
}

private void DoWork(object state)
private void DoWork(object? state)
{
ExecuteAsync(CancellationToken.None).ContinueWith(_ => { });
}
Comment on lines +39 to 42
Expand All @@ -52,11 +52,13 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Ru
throw;
}
}, stoppingToken);

private static bool False(Action action)
{
action();
return false;
}

private async ValueTask DoWorkAsync(CancellationToken cancellationToken = default)
{
if (TryAccquired())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading;
using System.Threading;
using System;
using System.Threading.Channels;
using System.Threading.Tasks;
Expand Down Expand Up @@ -36,15 +36,17 @@ public HighPerformanceBackgroundService(
};
_queue = Channel.CreateBounded<Func<CancellationToken, ValueTask>>(options);
#if DEBUG
_timer = new Timer(DoWork!, null, TimeSpan.Zero, TimeSpan.FromSeconds(60 * 5));
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(60 * 5));
#else
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
#endif
}
private void DoWork(object state)

private void DoWork(object? state)
{
DoWorkAsync(_cancellationToken).ContinueWith(_ => { });
}
Comment on lines +45 to 48

private async Task DoWorkAsync(CancellationToken stoppingToken)
{
if (_queue.Reader.TryRead(out var worker))
Expand All @@ -54,6 +56,7 @@ private async Task DoWorkAsync(CancellationToken stoppingToken)
}
await _queue.Writer.WriteAsync(DoWorkFromQueueAsync, stoppingToken);
}

private async ValueTask DoWorkFromQueueAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
Expand All @@ -68,6 +71,7 @@ private async ValueTask DoWorkFromQueueAsync(CancellationToken cancellationToken
_logger.LogError("作业系统调用失败:{Message}", ex.Message + Environment.NewLine + ex.StackTrace);
}
}

// 为何要用Task.Run,是为了防止再程序启动一个长时间阻塞的作业,导致整个应用程序启动阻塞。
// 详见:https://blog.stephencleary.com/2020/05/backgroundservice-gotcha-startup.html
// 官方团队可能会优化这点,具体详见:https://github.com/dotnet/runtime/issues/36063
Expand Down
Loading