Migrations: Use Process Manager to create own Context (#13793)

This commit is contained in:
6543 2020-12-02 22:11:11 +01:00 committed by GitHub
parent d551152582
commit 8396b792f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 15 deletions

View file

@ -111,7 +111,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
assert.NoError(t, git.CloneWithArgs(u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{})) assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
} }
} }

View file

@ -32,6 +32,7 @@ var (
GitExecutable = "git" GitExecutable = "git"
// DefaultContext is the default context to run git commands in // DefaultContext is the default context to run git commands in
// will be overwritten by Init with HammerContext
DefaultContext = context.Background() DefaultContext = context.Background()
gitVersion *version.Version gitVersion *version.Version

View file

@ -8,6 +8,7 @@ package git
import ( import (
"bytes" "bytes"
"container/list" "container/list"
"context"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -166,19 +167,24 @@ type CloneRepoOptions struct {
// Clone clones original repository to target path. // Clone clones original repository to target path.
func Clone(from, to string, opts CloneRepoOptions) (err error) { func Clone(from, to string, opts CloneRepoOptions) (err error) {
return CloneWithContext(DefaultContext, from, to, opts)
}
// CloneWithContext clones original repository to target path.
func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) (err error) {
cargs := make([]string, len(GlobalCommandArgs)) cargs := make([]string, len(GlobalCommandArgs))
copy(cargs, GlobalCommandArgs) copy(cargs, GlobalCommandArgs)
return CloneWithArgs(from, to, cargs, opts) return CloneWithArgs(ctx, from, to, cargs, opts)
} }
// CloneWithArgs original repository to target path. // CloneWithArgs original repository to target path.
func CloneWithArgs(from, to string, args []string, opts CloneRepoOptions) (err error) { func CloneWithArgs(ctx context.Context, from, to string, args []string, opts CloneRepoOptions) (err error) {
toDir := path.Dir(to) toDir := path.Dir(to)
if err = os.MkdirAll(toDir, os.ModePerm); err != nil { if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
return err return err
} }
cmd := NewCommandNoGlobals(args...).AddArguments("clone") cmd := NewCommandContextNoGlobals(ctx, args...).AddArguments("clone")
if opts.Mirror { if opts.Mirror {
cmd.AddArguments("--mirror") cmd.AddArguments("--mirror")
} }

View file

@ -125,7 +125,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
} }
r.DefaultBranch = repo.DefaultBranch r.DefaultBranch = repo.DefaultBranch
r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, base.MigrateOptions{ r, err = repository.MigrateRepositoryGitData(g.ctx, owner, r, base.MigrateOptions{
RepoName: g.repoName, RepoName: g.repoName,
Description: repo.Description, Description: repo.Description,
OriginalURL: repo.OriginalURL, OriginalURL: repo.OriginalURL,

View file

@ -5,6 +5,7 @@
package repository package repository
import ( import (
"context"
"fmt" "fmt"
"path" "path"
"strings" "strings"
@ -41,7 +42,7 @@ func WikiRemoteURL(remote string) string {
} }
// MigrateRepositoryGitData starts migrating git related data after created migrating repository // MigrateRepositoryGitData starts migrating git related data after created migrating repository
func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) { func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) {
repoPath := models.RepoPath(u.Name, opts.RepoName) repoPath := models.RepoPath(u.Name, opts.RepoName)
if u.IsOrganization() { if u.IsOrganization() {
@ -61,7 +62,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt
return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err) return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err)
} }
if err = git.Clone(opts.CloneAddr, repoPath, git.CloneRepoOptions{ if err = git.CloneWithContext(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
Mirror: true, Mirror: true,
Quiet: true, Quiet: true,
Timeout: migrateTimeout, Timeout: migrateTimeout,
@ -77,7 +78,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt
return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err) return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err)
} }
if err = git.Clone(wikiRemotePath, wikiPath, git.CloneRepoOptions{ if err = git.CloneWithContext(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
Mirror: true, Mirror: true,
Quiet: true, Quiet: true,
Timeout: migrateTimeout, Timeout: migrateTimeout,

View file

@ -5,6 +5,7 @@
package task package task
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -15,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/migrations"
migration "code.gitea.io/gitea/modules/migrations/base" migration "code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -82,11 +84,6 @@ func runMigrateTask(t *models.Task) (err error) {
if err = t.LoadOwner(); err != nil { if err = t.LoadOwner(); err != nil {
return return
} }
t.StartTime = timeutil.TimeStampNow()
t.Status = structs.TaskStatusRunning
if err = t.UpdateCols("start_time", "status"); err != nil {
return
}
var opts *migration.MigrateOptions var opts *migration.MigrateOptions
opts, err = t.MigrateConfig() opts, err = t.MigrateConfig()
@ -96,7 +93,20 @@ func runMigrateTask(t *models.Task) (err error) {
opts.MigrateToRepoID = t.RepoID opts.MigrateToRepoID = t.RepoID
var repo *models.Repository var repo *models.Repository
repo, err = migrations.MigrateRepository(graceful.GetManager().HammerContext(), t.Doer, t.Owner.Name, *opts)
ctx, cancel := context.WithCancel(graceful.GetManager().ShutdownContext())
defer cancel()
pm := process.GetManager()
pid := pm.Add(fmt.Sprintf("MigrateTask: %s/%s", t.Owner.Name, opts.RepoName), cancel)
defer pm.Remove(pid)
t.StartTime = timeutil.TimeStampNow()
t.Status = structs.TaskStatusRunning
if err = t.UpdateCols("start_time", "status"); err != nil {
return
}
repo, err = migrations.MigrateRepository(ctx, t.Doer, t.Owner.Name, *opts)
if err == nil { if err == nil {
log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name) log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
return return

View file

@ -5,6 +5,7 @@
package mirror package mirror
import ( import (
"context"
"path/filepath" "path/filepath"
"testing" "testing"
@ -47,7 +48,7 @@ func TestRelease_MirrorDelete(t *testing.T) {
}) })
assert.NoError(t, err) assert.NoError(t, err)
mirror, err := repository.MigrateRepositoryGitData(user, user, mirrorRepo, opts) mirror, err := repository.MigrateRepositoryGitData(context.Background(), user, mirrorRepo, opts)
assert.NoError(t, err) assert.NoError(t, err)
gitRepo, err := git.OpenRepository(repoPath) gitRepo, err := git.OpenRepository(repoPath)