Skip to content

Add configuration to support free concurrent tasks and optimize git library update performance issues #2424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions db_lib/GitRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"github.com/semaphoreui/semaphore/util"
"os"
"path"
"time"

"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/task_logger"
)

type GitRepositoryDirType int

var GitUpdateTimeCache = map[string]int64{}

const (
GitRepositoryTmpPath GitRepositoryDirType = iota
GitRepositoryFullPath
Expand All @@ -34,6 +37,28 @@ type GitRepository struct {
Client GitClient
}

func (r GitRepository) isInCache() bool {
if r.TmpDirName != "" {
return false
}
cacheTime, ok := GitUpdateTimeCache[r.GetFullPath()]
if !ok {
return false
}
if util.Config.GitCacheTime == 0 {
return false
}
if (time.Now().Unix() - cacheTime) < int64(util.Config.GitCacheTime) {
return true
}
return false
}
func (r GitRepository) setCache() {
if r.TmpDirName != "" {
return
}
GitUpdateTimeCache[r.GetFullPath()] = time.Now().Unix()
}
func (r GitRepository) GetFullPath() string {
if r.TmpDirName != "" {
return path.Join(util.Config.TmpPath, r.TmpDirName)
Expand All @@ -47,10 +72,15 @@ func (r GitRepository) ValidateRepo() error {
}

func (r GitRepository) Clone() error {
r.setCache()
return r.Client.Clone(r)
}

func (r GitRepository) Pull() error {
if r.isInCache() {
return nil
}
r.setCache()
return r.Client.Pull(r)
}

Expand All @@ -59,6 +89,9 @@ func (r GitRepository) Checkout(target string) error {
}

func (r GitRepository) CanBePulled() bool {
if r.isInCache() {
return true
}
return r.Client.CanBePulled(r)
}

Expand Down
20 changes: 13 additions & 7 deletions services/tasks/TaskPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func (p *TaskPool) GetTask(id int) (task *TaskRunner) {

// nolint: gocyclo
func (p *TaskPool) Run() {
ticker := time.NewTicker(5 * time.Second)
tickerDuration := time.Duration(int64(util.Config.TaskPoolTickerSec)) * time.Second
if tickerDuration.Seconds() == 0 {
tickerDuration = 5 * time.Second
}
ticker := time.NewTicker(tickerDuration)

defer func() {
close(p.resourceLocker)
Expand Down Expand Up @@ -193,12 +197,14 @@ func (p *TaskPool) blocks(t *TaskRunner) bool {
return false
}

for _, r := range p.activeProj[t.Task.ProjectID] {
if r.Task.Status.IsFinished() {
continue
}
if r.Template.ID == t.Task.TemplateID {
return true
if !util.Config.TaskFreeConcurrencyMode {
for _, r := range p.activeProj[t.Task.ProjectID] {
if r.Task.Status.IsFinished() {
continue
}
if r.Template.ID == t.Task.TemplateID {
return true
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type ConfigType struct {

GitClientId string `json:"git_client,omitempty" rule:"^go_git|cmd_git$" env:"SEMAPHORE_GIT_CLIENT" default:"cmd_git"`

GitCacheTime int `json:"git_cache_time" default:"0" env:"SEMAPHORE_GIT_CACHE_TIME"`

// web host
WebHost string `json:"web_host,omitempty" env:"SEMAPHORE_WEB_ROOT"`

Expand Down Expand Up @@ -191,6 +193,9 @@ type ConfigType struct {
// task concurrency
MaxParallelTasks int `json:"max_parallel_tasks,omitempty" default:"10" rule:"^[0-9]{1,10}$" env:"SEMAPHORE_MAX_PARALLEL_TASKS"`

TaskFreeConcurrencyMode bool `json:"task_free_concurrency_mode" env:"SEMAPHORE_TASK_FREE_CONCURRENCY_MODE"`
TaskPoolTickerSec int `json:"task_pool_ticker_sec" default:"5" env:"SEMAPHORE_TASK_POOL_TICKER_SEC"`

RunnerRegistrationToken string `json:"runner_registration_token,omitempty" env:"SEMAPHORE_RUNNER_REGISTRATION_TOKEN"`

// feature switches
Expand Down
Loading