Skip to content

Commit 6584138

Browse files
committed
26/01/03
1 parent c330a85 commit 6584138

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

Plugin/基于NodeJs实现任务队列与优雅停机.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,38 @@ async function tryRun(): Promise<void> {
144144
}
145145
```
146146
147-
不过这里其实有个问题,如果此时没有任务可以调度,还是会尝试`maxTasks`次任务执行,这其实是会存在数据库无效读的问题。不过对于数据库来说这里的读操作通常是可以接受的,数据库里状态值是要加索引的,如果实在不可接受就需要排队延迟`run`的执行,例如前个任务`10s`内返回空就不执行。
147+
不过这里其实有个问题,如果此时没有任务可以调度,还是会尝试`maxTasks`次任务执行,这其实是会存在数据库无效读的问题。不过对于数据库来说这里的读操作通常是可以接受的,数据库里状态值是要加条件索引的,如果实在不可接受就需要排队延迟`run`的执行,例如前个任务`10s`内返回空就不执行。
148+
149+
此外,我们可以采用更灵活的模式,只不过这个任务的分发逻辑需要在查询到任务后再进行调度,也就是说只要已经存在了任务,那么就尝试再调度下一个任务。这样可以避免无效读的问题,不过会增加约定式调度的成本,整体来说分发模式如下图所示:
150+
151+
```mermaid
152+
flowchart LR
153+
ROOT --> 并行1 --> |分配锁|1 --> |查询| t1[任务1]
154+
t1 --> 并行3 --> |分配锁|3 --> |查询| t3[任务3]
155+
t1 --> 执行任务1 --> f1[执行完成] --> r1[释放锁1]
156+
r1 --> 并行4 --> n1[...]
157+
t3 --> 并行5 --> n2[...]
158+
t3 --> 执行任务3 --> n3[...]
159+
ROOT --> 并行2 --> |分配锁|2 --> |查询| t2[任务2]
160+
t2 --> 并行6 --> |分配锁|6 --> |查询| t6[无任务]
161+
t2 --> 执行任务2 --> f2[执行完成] --> r2[释放锁]
162+
t6 --> 分支停止 --> 结束
163+
r2 --> 并行7 --> n7[...]
164+
```
165+
166+
我们可以简化上面的流程图,将任务的调度和执行合并为一个节点,这样下来整体调度模式非常类似于二叉树的形式。每个任务节点会分裂出两个子节点,一个子节点是分裂出下一个任务节点,另一个子节点则是执行当前任务节点,这样下来的流程就类似于串并存在的二叉树结构。
167+
168+
```plain
169+
ROOT --- 并行1 --- 任务1 --- 执行1/并行4 --- 任务4 --- 执行4/并行
170+
| | |
171+
| | 并行7 --- 任务7 --- ...
172+
| |
173+
| 并行3 --- 任务3 --- 执行3/并行4 --- 任务4 --- ...
174+
| |
175+
| 并行6 --- 任务6 --- ...
176+
|
177+
ROOT --- 并行2 --- 任务2 --- 执行2/并行5 --- 任务5 --- ...
178+
```
148179
149180
### 原子读写
150181
那么重写`onRunning`方法的实现,主要是从数据库中读取任务并执行。假设我们查找数据是使用`find`语句,写数据是使用`update`语句,那么假设此时的状态字段为`status`,我们可以定义如下的状态,`pending`待处理、`running`处理中、`completed`完成,`failed`失败。

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@
211211
a.onclick = (e) => (vm.route.path = newPath);
212212
}
213213
});
214+
// 处理页面内的图片地址
215+
const imgLinks = document.querySelectorAll("article img[src]");
216+
Array.from(imgLinks).forEach((img) => {
217+
const src = img.getAttribute("src");
218+
const query = BASE_Q + window.$p + "=";
219+
if ($p && src && src.startsWith(query)) {
220+
img.src = src.replace(query, "");
221+
}
222+
});
214223
// 设置页面的标题
215224
let title = $p.split("/").slice(-1).pop();
216225
title = title || window.$docsify.name;

sitemap.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,13 +2859,13 @@
28592859
<url>
28602860
<loc>https://blog.touchczy.top/?p=/Plugin/竞态问题与RxJs</loc>
28612861
<priority>1.0</priority>
2862-
<lastmod>2025-08-17T16:08:26.000Z</lastmod>
2862+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
28632863
</url>
28642864

28652865
<url>
28662866
<loc>https://blog.touchczy.top/I18N/?p=/Plugin/竞态问题与RxJs</loc>
28672867
<priority>1.0</priority>
2868-
<lastmod>2025-08-17T16:08:26.000Z</lastmod>
2868+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
28692869
</url>
28702870

28712871
<url>
@@ -3159,13 +3159,13 @@
31593159
<url>
31603160
<loc>https://blog.touchczy.top/?p=/Plugin/仿照豆包实现Prompt变量模板输入框</loc>
31613161
<priority>1.0</priority>
3162-
<lastmod>2025-09-21T03:17:43.000Z</lastmod>
3162+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
31633163
</url>
31643164

31653165
<url>
31663166
<loc>https://blog.touchczy.top/I18N/?p=/Plugin/仿照豆包实现Prompt变量模板输入框</loc>
31673167
<priority>1.0</priority>
3168-
<lastmod>2025-09-21T03:17:43.000Z</lastmod>
3168+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
31693169
</url>
31703170

31713171
<url>
@@ -3495,11 +3495,13 @@
34953495
<url>
34963496
<loc>https://blog.touchczy.top/?p=/RichText/编辑器文本结构变更的受控处理</loc>
34973497
<priority>1.0</priority>
3498+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
34983499
</url>
34993500

35003501
<url>
35013502
<loc>https://blog.touchczy.top/I18N/?p=/RichText/编辑器文本结构变更的受控处理</loc>
35023503
<priority>1.0</priority>
3504+
<lastmod>2025-12-20T02:23:20.000Z</lastmod>
35033505
</url>
35043506

35053507
<url>

0 commit comments

Comments
 (0)