aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-05-26 15:31:55 +0800
committerGitHub <noreply@github.com>2023-05-26 07:31:55 +0000
commit18f26cfbf7f9b36b838c0e8762bfba98c89b9797 (patch)
treef3bcf0ef2b76601980ab6144e9ce9acd5316a0da /services
parente4922d484b9ee94ba22e5ff08b3c25c8be09d9c8 (diff)
downloadgitea-18f26cfbf7f9b36b838c0e8762bfba98c89b9797.tar.gz
gitea-18f26cfbf7f9b36b838c0e8762bfba98c89b9797.zip
Improve queue and logger context (#24924)
Before there was a "graceful function": RunWithShutdownFns, it's mainly for some modules which doesn't support context. The old queue system doesn't work well with context, so the old queues need it. After the queue refactoring, the new queue works with context well, so, use Golang context as much as possible, the `RunWithShutdownFns` could be removed (replaced by RunWithCancel for context cancel mechanism), the related code could be simplified. This PR also fixes some legacy queue-init problems, eg: * typo : archiver: "unable to create codes indexer queue" => "unable to create repo-archive queue" * no nil check for failed queues, which causes unfriendly panic After this PR, many goroutines could have better display name: ![image](https://github.com/go-gitea/gitea/assets/2114189/701b2a9b-8065-4137-aeaa-0bda2b34604a) ![image](https://github.com/go-gitea/gitea/assets/2114189/f1d5f50f-0534-40f0-b0be-f2c9daa5fe92)
Diffstat (limited to 'services')
-rw-r--r--services/actions/init.go8
-rw-r--r--services/automerge/automerge.go6
-rw-r--r--services/mailer/mailer.go12
-rw-r--r--services/pull/check.go6
-rw-r--r--services/pull/check_test.go18
-rw-r--r--services/repository/archiver/archiver.go9
-rw-r--r--services/repository/push.go7
-rw-r--r--services/task/task.go9
-rw-r--r--services/webhook/deliver.go6
9 files changed, 36 insertions, 45 deletions
diff --git a/services/actions/init.go b/services/actions/init.go
index 8a9a30084a..26573c1681 100644
--- a/services/actions/init.go
+++ b/services/actions/init.go
@@ -5,6 +5,7 @@ package actions
import (
"code.gitea.io/gitea/modules/graceful"
+ "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
@@ -15,8 +16,11 @@ func Init() {
return
}
- jobEmitterQueue = queue.CreateUniqueQueue("actions_ready_job", jobEmitterQueueHandler)
- go graceful.GetManager().RunWithShutdownFns(jobEmitterQueue.Run)
+ jobEmitterQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "actions_ready_job", jobEmitterQueueHandler)
+ if jobEmitterQueue == nil {
+ log.Fatal("Unable to create actions_ready_job queue")
+ }
+ go graceful.GetManager().RunWithCancel(jobEmitterQueue)
notification.RegisterNotifier(NewNotifier())
}
diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go
index f001a6ccc5..bf713c4431 100644
--- a/services/automerge/automerge.go
+++ b/services/automerge/automerge.go
@@ -29,11 +29,11 @@ var prAutoMergeQueue *queue.WorkerPoolQueue[string]
// Init runs the task queue to that handles auto merges
func Init() error {
- prAutoMergeQueue = queue.CreateUniqueQueue("pr_auto_merge", handler)
+ prAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler)
if prAutoMergeQueue == nil {
- return fmt.Errorf("Unable to create pr_auto_merge Queue")
+ return fmt.Errorf("unable to create pr_auto_merge queue")
}
- go graceful.GetManager().RunWithShutdownFns(prAutoMergeQueue.Run)
+ go graceful.GetManager().RunWithCancel(prAutoMergeQueue)
return nil
}
diff --git a/services/mailer/mailer.go b/services/mailer/mailer.go
index 5aeda9ed79..ee4721d438 100644
--- a/services/mailer/mailer.go
+++ b/services/mailer/mailer.go
@@ -401,7 +401,9 @@ func NewContext(ctx context.Context) {
Sender = &smtpSender{}
}
- mailQueue = queue.CreateSimpleQueue("mail", func(items ...*Message) []*Message {
+ subjectTemplates, bodyTemplates = templates.Mailer(ctx)
+
+ mailQueue = queue.CreateSimpleQueue(graceful.GetManager().ShutdownContext(), "mail", func(items ...*Message) []*Message {
for _, msg := range items {
gomailMsg := msg.ToMessage()
log.Trace("New e-mail sending request %s: %s", gomailMsg.GetHeader("To"), msg.Info)
@@ -413,10 +415,10 @@ func NewContext(ctx context.Context) {
}
return nil
})
-
- go graceful.GetManager().RunWithShutdownFns(mailQueue.Run)
-
- subjectTemplates, bodyTemplates = templates.Mailer(ctx)
+ if mailQueue == nil {
+ log.Fatal("Unable to create mail queue")
+ }
+ go graceful.GetManager().RunWithCancel(mailQueue)
}
// SendAsync send mail asynchronously
diff --git a/services/pull/check.go b/services/pull/check.go
index 8bc2bdff1d..b5150ad9a8 100644
--- a/services/pull/check.go
+++ b/services/pull/check.go
@@ -384,13 +384,13 @@ func CheckPRsForBaseBranch(baseRepo *repo_model.Repository, baseBranchName strin
// Init runs the task queue to test all the checking status pull requests
func Init() error {
- prPatchCheckerQueue = queue.CreateUniqueQueue("pr_patch_checker", handler)
+ prPatchCheckerQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_patch_checker", handler)
if prPatchCheckerQueue == nil {
- return fmt.Errorf("Unable to create pr_patch_checker Queue")
+ return fmt.Errorf("unable to create pr_patch_checker queue")
}
- go graceful.GetManager().RunWithShutdownFns(prPatchCheckerQueue.Run)
+ go graceful.GetManager().RunWithCancel(prPatchCheckerQueue)
go graceful.GetManager().RunWithShutdownContext(InitializePullRequests)
return nil
}
diff --git a/services/pull/check_test.go b/services/pull/check_test.go
index 52209b4d35..57ccb20033 100644
--- a/services/pull/check_test.go
+++ b/services/pull/check_test.go
@@ -5,6 +5,7 @@
package pull
import (
+ "context"
"strconv"
"testing"
"time"
@@ -31,7 +32,7 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) {
cfg, err := setting.GetQueueSettings(setting.CfgProvider, "pr_patch_checker")
assert.NoError(t, err)
- prPatchCheckerQueue, err = queue.NewWorkerPoolQueueBySetting("pr_patch_checker", cfg, testHandler, true)
+ prPatchCheckerQueue, err = queue.NewWorkerPoolQueueWithContext(context.Background(), "pr_patch_checker", cfg, testHandler, true)
assert.NoError(t, err)
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
@@ -46,12 +47,7 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) {
assert.True(t, has)
assert.NoError(t, err)
- var queueShutdown, queueTerminate []func()
- go prPatchCheckerQueue.Run(func(shutdown func()) {
- queueShutdown = append(queueShutdown, shutdown)
- }, func(terminate func()) {
- queueTerminate = append(queueTerminate, terminate)
- })
+ go prPatchCheckerQueue.Run()
select {
case id := <-idChan:
@@ -67,12 +63,6 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) {
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
- for _, callback := range queueShutdown {
- callback()
- }
- for _, callback := range queueTerminate {
- callback()
- }
-
+ prPatchCheckerQueue.ShutdownWait(5 * time.Second)
prPatchCheckerQueue = nil
}
diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go
index 1c514a4112..2e3defee8d 100644
--- a/services/repository/archiver/archiver.go
+++ b/services/repository/archiver/archiver.go
@@ -297,7 +297,7 @@ func ArchiveRepository(request *ArchiveRequest) (*repo_model.RepoArchiver, error
var archiverQueue *queue.WorkerPoolQueue[*ArchiveRequest]
-// Init initlize archive
+// Init initializes archiver
func Init() error {
handler := func(items ...*ArchiveRequest) []*ArchiveRequest {
for _, archiveReq := range items {
@@ -309,12 +309,11 @@ func Init() error {
return nil
}
- archiverQueue = queue.CreateUniqueQueue("repo-archive", handler)
+ archiverQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "repo-archive", handler)
if archiverQueue == nil {
- return errors.New("unable to create codes indexer queue")
+ return errors.New("unable to create repo-archive queue")
}
-
- go graceful.GetManager().RunWithShutdownFns(archiverQueue.Run)
+ go graceful.GetManager().RunWithCancel(archiverQueue)
return nil
}
diff --git a/services/repository/push.go b/services/repository/push.go
index f213948916..571eedccb3 100644
--- a/services/repository/push.go
+++ b/services/repository/push.go
@@ -42,12 +42,11 @@ func handler(items ...[]*repo_module.PushUpdateOptions) [][]*repo_module.PushUpd
}
func initPushQueue() error {
- pushQueue = queue.CreateSimpleQueue("push_update", handler)
+ pushQueue = queue.CreateSimpleQueue(graceful.GetManager().ShutdownContext(), "push_update", handler)
if pushQueue == nil {
- return errors.New("unable to create push_update Queue")
+ return errors.New("unable to create push_update queue")
}
-
- go graceful.GetManager().RunWithShutdownFns(pushQueue.Run)
+ go graceful.GetManager().RunWithCancel(pushQueue)
return nil
}
diff --git a/services/task/task.go b/services/task/task.go
index 493586a645..11a47a68bb 100644
--- a/services/task/task.go
+++ b/services/task/task.go
@@ -37,14 +37,11 @@ func Run(t *admin_model.Task) error {
// Init will start the service to get all unfinished tasks and run them
func Init() error {
- taskQueue = queue.CreateSimpleQueue("task", handler)
-
+ taskQueue = queue.CreateSimpleQueue(graceful.GetManager().ShutdownContext(), "task", handler)
if taskQueue == nil {
- return fmt.Errorf("Unable to create Task Queue")
+ return fmt.Errorf("unable to create task queue")
}
-
- go graceful.GetManager().RunWithShutdownFns(taskQueue.Run)
-
+ go graceful.GetManager().RunWithCancel(taskQueue)
return nil
}
diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go
index e817783e55..fd7a3d7fba 100644
--- a/services/webhook/deliver.go
+++ b/services/webhook/deliver.go
@@ -283,11 +283,11 @@ func Init() error {
},
}
- hookQueue = queue.CreateUniqueQueue("webhook_sender", handler)
+ hookQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "webhook_sender", handler)
if hookQueue == nil {
- return fmt.Errorf("Unable to create webhook_sender Queue")
+ return fmt.Errorf("unable to create webhook_sender queue")
}
- go graceful.GetManager().RunWithShutdownFns(hookQueue.Run)
+ go graceful.GetManager().RunWithCancel(hookQueue)
go graceful.GetManager().RunWithShutdownContext(populateWebhookSendingQueue)