fix: remove usage of core

This commit is contained in:
Jason Song 2022-10-20 18:43:20 +08:00
parent 6cdbf3e986
commit dca2ca2f47
5 changed files with 6 additions and 210 deletions

View File

@ -7,7 +7,6 @@ package bots
import (
"context"
"code.gitea.io/gitea/core"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/util"
@ -54,13 +53,12 @@ func (opts FindRunOptions) toConds() builder.Cond {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
}
if opts.IsClosed.IsFalse() {
cond = cond.And(builder.Eq{"status": core.StatusPending}.Or(
builder.Eq{"status": core.StatusWaiting}.Or(
builder.Eq{"status": core.StatusRunning})))
cond = cond.And(builder.Eq{"status": StatusWaiting}.Or(
builder.Eq{"status": StatusRunning}))
} else if opts.IsClosed.IsTrue() {
cond = cond.And(builder.Neq{"status": core.StatusPending}.And(
builder.Neq{"status": core.StatusWaiting}.And(
builder.Neq{"status": core.StatusRunning})))
cond = cond.And(
builder.Neq{"status": StatusWaiting}.And(
builder.Neq{"status": StatusRunning}))
}
if opts.WorkflowFileName != "" {
cond = cond.And(builder.Eq{"workflow_id": opts.WorkflowFileName})

View File

@ -8,14 +8,11 @@ import (
"net/http"
"code.gitea.io/gitea/routers/api/bots/runner"
"code.gitea.io/gitea/routers/api/bots/scheduler/queue"
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
)
func RunnerRoute() (string, http.Handler) {
runnerService := &runner.Service{
Scheduler: queue.New(),
}
runnerService := &runner.Service{}
return runnerv1connect.NewRunnerServiceHandler(
runnerService,

View File

@ -10,7 +10,6 @@ import (
"fmt"
"time"
"code.gitea.io/gitea/core"
bots_model "code.gitea.io/gitea/models/bots"
"code.gitea.io/gitea/modules/bots"
"code.gitea.io/gitea/modules/json"
@ -28,8 +27,6 @@ import (
var _ runnerv1connect.RunnerServiceClient = (*Service)(nil)
type Service struct {
Scheduler core.Scheduler
runnerv1connect.UnimplementedRunnerServiceHandler
}

View File

@ -1,159 +0,0 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package queue
import (
"context"
"sync"
"time"
"code.gitea.io/gitea/core"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
)
type worker struct {
kind string
typ string
os string
arch string
channel chan *runnerv1.Task
}
type queue struct {
sync.Mutex
ready chan struct{}
paused bool
interval time.Duration
workers map[*worker]struct{}
ctx context.Context
}
func (q *queue) Schedule(ctx context.Context, stage *runnerv1.Task) error {
select {
case q.ready <- struct{}{}:
default:
}
return nil
}
func (q *queue) Request(ctx context.Context, params core.Filter) (*runnerv1.Task, error) {
w := &worker{
kind: params.Kind,
typ: params.Type,
os: params.OS,
arch: params.Arch,
channel: make(chan *runnerv1.Task),
}
q.Lock()
q.workers[w] = struct{}{}
q.Unlock()
select {
case q.ready <- struct{}{}:
default:
}
select {
case <-ctx.Done():
q.Lock()
delete(q.workers, w)
q.Unlock()
return nil, ctx.Err()
case b := <-w.channel:
return b, nil
}
}
func (q *queue) start() error {
for {
select {
case <-q.ctx.Done():
return q.ctx.Err()
case <-q.ready:
_ = q.signal(q.ctx)
case <-time.After(q.interval):
_ = q.signal(q.ctx)
}
}
}
func (q *queue) signal(ctx context.Context) error {
q.Lock()
count := len(q.workers)
pause := q.paused
q.Unlock()
if pause {
return nil
}
if count == 0 {
return nil
}
/*items, err := bots.FindStages(ctx, bots.FindStageOptions{})
if err != nil {
return err
}
q.Lock()
defer q.Unlock()
for _, item := range items {
if item.Status == core.StatusRunning {
continue
}
if item.Machine != "" {
continue
}
loop:
for w := range q.workers {
// the worker must match the resource kind and type
if !matchResource(w.kind, w.typ, item.Kind, item.Type) {
continue
}
if w.os != "" || w.arch != "" {
if w.os != item.OS {
continue
}
if w.arch != item.Arch {
continue
}
}
stage := &runnerv1.Task{
Id: item.ID,
// BuildId: item.BuildID,
// Name: item.Name,
// Kind: item.Name,
// Type: item.Type,
// Status: string(item.Status),
// Started: int64(item.Started),
// Stopped: int64(item.Stopped),
}
w.channel <- stage
delete(q.workers, w)
break loop
}
}*/
return nil
}
// matchResource is a helper function that returns
func matchResource(kinda, typea, kindb, typeb string) bool {
if kinda == "" {
kinda = "pipeline"
}
if kindb == "" {
kindb = "pipeline"
}
if typea == "" {
typea = "docker"
}
if typeb == "" {
typeb = "docker"
}
return kinda == kindb && typea == typeb
}

View File

@ -1,37 +0,0 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package queue
import (
"context"
"time"
"code.gitea.io/gitea/core"
)
type scheduler struct {
*queue
}
// New creates a new scheduler.
func New() core.Scheduler {
return scheduler{
queue: newQueue(),
}
}
// newQueue returns a new Queue backed by the build datastore.
func newQueue() *queue {
q := &queue{
ready: make(chan struct{}, 1),
workers: map[*worker]struct{}{},
interval: time.Minute,
ctx: context.Background(),
}
go func() {
_ = q.start()
}()
return q
}