diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-06 15:19:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-06 15:19:28 +0800 |
commit | dcdb4873c8d77a444526fad5b1c8e705fdfe149d (patch) | |
tree | 599efda4334775ef40db9dd845355dfde7d73b33 /models | |
parent | f49d160447899270fbca6370cb7ab2742dce85dc (diff) | |
download | gitea-dcdb4873c8d77a444526fad5b1c8e705fdfe149d.tar.gz gitea-dcdb4873c8d77a444526fad5b1c8e705fdfe149d.zip |
Move repo archiver to models/repo (#17913)
* Move repo archiver to models/repo
* Move archiver service into services/repository/
* Fix imports
* Fix test
* Fix test
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 51 | ||||
-rw-r--r-- | models/repo/archiver.go | 109 | ||||
-rw-r--r-- | models/repo/main_test.go | 1 | ||||
-rw-r--r-- | models/repo_archiver.go | 73 |
4 files changed, 115 insertions, 119 deletions
diff --git a/models/repo.go b/models/repo.go index 67879fefda..4f6b1c3464 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1605,19 +1605,18 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error { } // Remove archives - var archives []*RepoArchiver + var archives []*repo_model.RepoArchiver if err = sess.Where("repo_id=?", repoID).Find(&archives); err != nil { return err } var archivePaths = make([]string, 0, len(archives)) for _, v := range archives { - v.Repo = repo p, _ := v.RelativePath() archivePaths = append(archivePaths, p) } - if _, err := sess.Delete(&RepoArchiver{RepoID: repoID}); err != nil { + if _, err := sess.Delete(&repo_model.RepoArchiver{RepoID: repoID}); err != nil { return err } @@ -1824,52 +1823,6 @@ func GetPrivateRepositoryCount(u *user_model.User) (int64, error) { return getPrivateRepositoryCount(db.GetEngine(db.DefaultContext), u) } -// DeleteOldRepositoryArchives deletes old repository archives. -func DeleteOldRepositoryArchives(ctx context.Context, olderThan time.Duration) error { - log.Trace("Doing: ArchiveCleanup") - - for { - var archivers []RepoArchiver - err := db.GetEngine(db.DefaultContext).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()). - Asc("created_unix"). - Limit(100). - Find(&archivers) - if err != nil { - log.Trace("Error: ArchiveClean: %v", err) - return err - } - - for _, archiver := range archivers { - if err := deleteOldRepoArchiver(ctx, &archiver); err != nil { - return err - } - } - if len(archivers) < 100 { - break - } - } - - log.Trace("Finished: ArchiveCleanup") - return nil -} - -var delRepoArchiver = new(RepoArchiver) - -func deleteOldRepoArchiver(ctx context.Context, archiver *RepoArchiver) error { - p, err := archiver.RelativePath() - if err != nil { - return err - } - _, err = db.GetEngine(db.DefaultContext).ID(archiver.ID).Delete(delRepoArchiver) - if err != nil { - return err - } - if err := storage.RepoArchives.Delete(p); err != nil { - log.Error("delete repo archive file failed: %v", err) - } - return nil -} - type repoChecker struct { querySQL, correctSQL string desc string diff --git a/models/repo/archiver.go b/models/repo/archiver.go new file mode 100644 index 0000000000..cee6013ca3 --- /dev/null +++ b/models/repo/archiver.go @@ -0,0 +1,109 @@ +// Copyright 2021 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 repo + +import ( + "context" + "fmt" + "time" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/builder" +) + +// ArchiverStatus represents repo archive status +type ArchiverStatus int + +// enumerate all repo archive statuses +const ( + ArchiverGenerating = iota // the archiver is generating + ArchiverReady // it's ready +) + +// RepoArchiver represents all archivers +type RepoArchiver struct { //revive:disable-line:exported + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"index unique(s)"` + Type git.ArchiveType `xorm:"unique(s)"` + Status ArchiverStatus + CommitID string `xorm:"VARCHAR(40) unique(s)"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"` +} + +func init() { + db.RegisterModel(new(RepoArchiver)) +} + +// RelativePath returns relative path +func (archiver *RepoArchiver) RelativePath() (string, error) { + return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil +} + +var delRepoArchiver = new(RepoArchiver) + +// DeleteRepoArchiver delete archiver +func DeleteRepoArchiver(ctx context.Context, archiver *RepoArchiver) error { + _, err := db.GetEngine(db.DefaultContext).ID(archiver.ID).Delete(delRepoArchiver) + return err +} + +// GetRepoArchiver get an archiver +func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) { + var archiver RepoArchiver + has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver) + if err != nil { + return nil, err + } + if has { + return &archiver, nil + } + return nil, nil +} + +// AddRepoArchiver adds an archiver +func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error { + _, err := db.GetEngine(ctx).Insert(archiver) + return err +} + +// UpdateRepoArchiverStatus updates archiver's status +func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error { + _, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver) + return err +} + +// DeleteAllRepoArchives deletes all repo archives records +func DeleteAllRepoArchives() error { + _, err := db.GetEngine(db.DefaultContext).Where("1=1").Delete(new(RepoArchiver)) + return err +} + +// FindRepoArchiversOption represents an archiver options +type FindRepoArchiversOption struct { + db.ListOptions + OlderThan time.Duration +} + +func (opts FindRepoArchiversOption) toConds() builder.Cond { + var cond = builder.NewCond() + if opts.OlderThan > 0 { + cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-opts.OlderThan).Unix()}) + } + return cond +} + +// FindRepoArchives find repo archivers +func FindRepoArchives(opts FindRepoArchiversOption) ([]*RepoArchiver, error) { + var archivers = make([]*RepoArchiver, 0, opts.PageSize) + start, limit := opts.GetSkipTake() + err := db.GetEngine(db.DefaultContext).Where(opts.toConds()). + Asc("created_unix"). + Limit(limit, start). + Find(&archivers) + return archivers, err +} diff --git a/models/repo/main_test.go b/models/repo/main_test.go index ac62df9d9c..aa960bf132 100644 --- a/models/repo/main_test.go +++ b/models/repo/main_test.go @@ -14,5 +14,6 @@ import ( func TestMain(m *testing.M) { unittest.MainTest(m, filepath.Join("..", ".."), "attachment.yml", + "repo_archiver.yml", ) } diff --git a/models/repo_archiver.go b/models/repo_archiver.go index 647a3b47be..2369a16108 100644 --- a/models/repo_archiver.go +++ b/models/repo_archiver.go @@ -5,44 +5,13 @@ package models import ( - "context" - "fmt" - "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/timeutil" -) - -// RepoArchiverStatus represents repo archive status -type RepoArchiverStatus int -// enumerate all repo archive statuses -const ( - RepoArchiverGenerating = iota // the archiver is generating - RepoArchiverReady // it's ready + repo_model "code.gitea.io/gitea/models/repo" ) -// RepoArchiver represents all archivers -type RepoArchiver struct { - ID int64 `xorm:"pk autoincr"` - RepoID int64 `xorm:"index unique(s)"` - Repo *Repository `xorm:"-"` - Type git.ArchiveType `xorm:"unique(s)"` - Status RepoArchiverStatus - CommitID string `xorm:"VARCHAR(40) unique(s)"` - CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"` -} - -func init() { - db.RegisterModel(new(RepoArchiver)) -} - -// LoadRepo loads repository -func (archiver *RepoArchiver) LoadRepo() (*Repository, error) { - if archiver.Repo != nil { - return archiver.Repo, nil - } - +// LoadArchiverRepo loads repository +func LoadArchiverRepo(archiver *repo_model.RepoArchiver) (*Repository, error) { var repo Repository has, err := db.GetEngine(db.DefaultContext).ID(archiver.RepoID).Get(&repo) if err != nil { @@ -55,39 +24,3 @@ func (archiver *RepoArchiver) LoadRepo() (*Repository, error) { } return &repo, nil } - -// RelativePath returns relative path -func (archiver *RepoArchiver) RelativePath() (string, error) { - return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil -} - -// GetRepoArchiver get an archiver -func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) { - var archiver RepoArchiver - has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver) - if err != nil { - return nil, err - } - if has { - return &archiver, nil - } - return nil, nil -} - -// AddRepoArchiver adds an archiver -func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error { - _, err := db.GetEngine(ctx).Insert(archiver) - return err -} - -// UpdateRepoArchiverStatus updates archiver's status -func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error { - _, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver) - return err -} - -// DeleteAllRepoArchives deletes all repo archives records -func DeleteAllRepoArchives() error { - _, err := db.GetEngine(db.DefaultContext).Where("1=1").Delete(new(RepoArchiver)) - return err -} |