mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 改进内核任务调度机制提升稳定性 https://github.com/siyuan-note/siyuan/issues/7113
This commit is contained in:
parent
98f682e507
commit
492389470f
6 changed files with 181 additions and 38 deletions
162
kernel/task/queue.go
Normal file
162
kernel/task/queue.go
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// Copyright (c) 2020-present, b3log.org
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/siyuan-note/logging"
|
||||
)
|
||||
|
||||
var (
|
||||
taskQueue []*Task
|
||||
taskQueueStatus int
|
||||
queueLock = sync.Mutex{}
|
||||
taskLock = sync.Mutex{}
|
||||
)
|
||||
|
||||
const (
|
||||
QueueStatusRunning = iota
|
||||
QueueStatusClosing
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Action string
|
||||
Handler reflect.Value
|
||||
Args []interface{}
|
||||
Created time.Time
|
||||
}
|
||||
|
||||
func PrependTask(action string, handler interface{}, args ...interface{}) {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
if QueueStatusRunning != taskQueueStatus {
|
||||
logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
|
||||
return
|
||||
}
|
||||
|
||||
cancelTask(action)
|
||||
taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
|
||||
}
|
||||
|
||||
func AppendTask(action string, handler interface{}, args ...interface{}) {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
if QueueStatusRunning != taskQueueStatus {
|
||||
logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
|
||||
return
|
||||
}
|
||||
|
||||
cancelTask(action)
|
||||
taskQueue = append(taskQueue, newTask(action, handler, args...))
|
||||
}
|
||||
|
||||
func CancelTask(actions ...string) {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
cancelTask(actions...)
|
||||
}
|
||||
|
||||
func cancelTask(actions ...string) {
|
||||
for i := len(taskQueue) - 1; i >= 0; i-- {
|
||||
task := taskQueue[i]
|
||||
for _, action := range actions {
|
||||
if action == task.Action {
|
||||
taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTask(action string, handler interface{}, args ...interface{}) *Task {
|
||||
return &Task{
|
||||
Action: action,
|
||||
Handler: reflect.ValueOf(handler),
|
||||
Args: args,
|
||||
Created: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
CloudSync = "task.cloud.sync" // 数据同步
|
||||
RepoCheckout = "task.repo.checkout" // 从快照中检出
|
||||
DatabaseIndexFull = "task.database.index.full" // 重建索引
|
||||
DatabaseIndex = "task.database.index" // 数据库所以队列
|
||||
DatabaseIndexFix = "task.database.index.fix" // 数据库索引订正
|
||||
OCRImage = "task.ocr.image" // 图片 OCR 提取文本
|
||||
DatabaseIndexEmbedBlock = "task.database.index.embedblock" // 数据库索引嵌入块
|
||||
)
|
||||
|
||||
func Loop() {
|
||||
for {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
task := popTask()
|
||||
if nil == task {
|
||||
continue
|
||||
}
|
||||
|
||||
execTask(task)
|
||||
}
|
||||
}
|
||||
|
||||
func CloseWait() {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
taskQueueStatus = QueueStatusClosing
|
||||
for {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
if 1 > len(taskQueue) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func popTask() (ret *Task) {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
if 0 == len(taskQueue) {
|
||||
return
|
||||
}
|
||||
|
||||
ret = taskQueue[0]
|
||||
taskQueue = taskQueue[1:]
|
||||
return
|
||||
}
|
||||
|
||||
func execTask(task *Task) {
|
||||
taskLock.Lock()
|
||||
defer taskLock.Unlock()
|
||||
defer logging.Recover()
|
||||
|
||||
args := make([]reflect.Value, len(task.Args))
|
||||
for i, v := range task.Args {
|
||||
if nil == v {
|
||||
args[i] = reflect.New(task.Handler.Type().In(i)).Elem()
|
||||
} else {
|
||||
args[i] = reflect.ValueOf(v)
|
||||
}
|
||||
}
|
||||
task.Handler.Call(args)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue