code.gitea.io/gitea@v1.21.7/routers/web/admin/queue_tester.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package admin
     5  
     6  import (
     7  	"runtime/pprof"
     8  	"sync"
     9  	"time"
    10  
    11  	"code.gitea.io/gitea/modules/graceful"
    12  	"code.gitea.io/gitea/modules/log"
    13  	"code.gitea.io/gitea/modules/process"
    14  	"code.gitea.io/gitea/modules/queue"
    15  	"code.gitea.io/gitea/modules/setting"
    16  )
    17  
    18  var testQueueOnce sync.Once
    19  
    20  // initTestQueueOnce initializes the test queue for dev mode
    21  // the test queue will also be shown in the queue list
    22  // developers could see the queue length / worker number / items number on the admin page and try to remove the items
    23  func initTestQueueOnce() {
    24  	testQueueOnce.Do(func() {
    25  		ctx, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "TestQueue", process.SystemProcessType, false)
    26  		qs := setting.QueueSettings{
    27  			Name:        "test-queue",
    28  			Type:        "channel",
    29  			Length:      20,
    30  			BatchLength: 2,
    31  			MaxWorkers:  3,
    32  		}
    33  		testQueue, err := queue.NewWorkerPoolQueueWithContext(ctx, "test-queue", qs, func(t ...int64) (unhandled []int64) {
    34  			for range t {
    35  				select {
    36  				case <-graceful.GetManager().ShutdownContext().Done():
    37  				case <-time.After(5 * time.Second):
    38  				}
    39  			}
    40  			return nil
    41  		}, true)
    42  		if err != nil {
    43  			log.Error("unable to create test queue: %v", err)
    44  			return
    45  		}
    46  
    47  		queue.GetManager().AddManagedQueue(testQueue)
    48  		testQueue.SetWorkerMaxNumber(5)
    49  		go graceful.GetManager().RunWithCancel(testQueue)
    50  		go func() {
    51  			pprof.SetGoroutineLabels(ctx)
    52  			defer finished()
    53  
    54  			cnt := int64(0)
    55  			adding := true
    56  			for {
    57  				select {
    58  				case <-ctx.Done():
    59  				case <-time.After(500 * time.Millisecond):
    60  					if adding {
    61  						if testQueue.GetQueueItemNumber() == qs.Length {
    62  							adding = false
    63  						}
    64  					} else {
    65  						if testQueue.GetQueueItemNumber() == 0 {
    66  							adding = true
    67  						}
    68  					}
    69  					if adding {
    70  						_ = testQueue.Push(cnt)
    71  						cnt++
    72  					}
    73  				}
    74  			}
    75  		}()
    76  	})
    77  }