aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-06 15:19:28 +0800
committerGitHub <noreply@github.com>2021-12-06 15:19:28 +0800
commitdcdb4873c8d77a444526fad5b1c8e705fdfe149d (patch)
tree599efda4334775ef40db9dd845355dfde7d73b33 /services
parentf49d160447899270fbca6370cb7ab2742dce85dc (diff)
downloadgitea-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 'services')
-rw-r--r--services/cron/tasks_basic.go7
-rw-r--r--services/cron/tasks_extended.go3
-rw-r--r--services/repository/archive.go20
-rw-r--r--services/repository/archiver/archiver.go (renamed from services/archiver/archiver.go)89
-rw-r--r--services/repository/archiver/archiver_test.go (renamed from services/archiver/archiver_test.go)2
5 files changed, 79 insertions, 42 deletions
diff --git a/services/cron/tasks_basic.go b/services/cron/tasks_basic.go
index 814f6eae4e..fdf8550c31 100644
--- a/services/cron/tasks_basic.go
+++ b/services/cron/tasks_basic.go
@@ -15,7 +15,8 @@ import (
"code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/migrations"
mirror_service "code.gitea.io/gitea/services/mirror"
- repository_service "code.gitea.io/gitea/services/repository"
+ repo_service "code.gitea.io/gitea/services/repository"
+ archiver_service "code.gitea.io/gitea/services/repository/archiver"
)
func registerUpdateMirrorTask() {
@@ -56,7 +57,7 @@ func registerRepoHealthCheck() {
Args: []string{},
}, func(ctx context.Context, _ *user_model.User, config Config) error {
rhcConfig := config.(*RepoHealthCheckConfig)
- return repository_service.GitFsck(ctx, rhcConfig.Timeout, rhcConfig.Args)
+ return repo_service.GitFsck(ctx, rhcConfig.Timeout, rhcConfig.Args)
})
}
@@ -80,7 +81,7 @@ func registerArchiveCleanup() {
OlderThan: 24 * time.Hour,
}, func(ctx context.Context, _ *user_model.User, config Config) error {
acConfig := config.(*OlderThanConfig)
- return models.DeleteOldRepositoryArchives(ctx, acConfig.OlderThan)
+ return archiver_service.DeleteOldRepositoryArchives(ctx, acConfig.OlderThan)
})
}
diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go
index 26dbe548a7..bc6add090b 100644
--- a/services/cron/tasks_extended.go
+++ b/services/cron/tasks_extended.go
@@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/updatechecker"
repo_service "code.gitea.io/gitea/services/repository"
+ archiver_service "code.gitea.io/gitea/services/repository/archiver"
user_service "code.gitea.io/gitea/services/user"
)
@@ -36,7 +37,7 @@ func registerDeleteRepositoryArchives() {
RunAtStart: false,
Schedule: "@annually",
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
- return repo_service.DeleteRepositoryArchives(ctx)
+ return archiver_service.DeleteRepositoryArchives(ctx)
})
}
diff --git a/services/repository/archive.go b/services/repository/archive.go
deleted file mode 100644
index bea636c579..0000000000
--- a/services/repository/archive.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// 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 repository
-
-import (
- "context"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/storage"
-)
-
-// DeleteRepositoryArchives deletes all repositories' archives.
-func DeleteRepositoryArchives(ctx context.Context) error {
- if err := models.DeleteAllRepoArchives(); err != nil {
- return err
- }
- return storage.Clean(storage.RepoArchives)
-}
diff --git a/services/archiver/archiver.go b/services/repository/archiver/archiver.go
index 06686220f1..7e886d79cb 100644
--- a/services/archiver/archiver.go
+++ b/services/repository/archiver/archiver.go
@@ -1,20 +1,22 @@
-// Copyright 2020 The Gitea Authors.
-// All rights reserved.
+// Copyright 2020 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 archiver
import (
+ "context"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
+ "time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
@@ -113,14 +115,14 @@ func (aReq *ArchiveRequest) GetArchiveName() string {
return strings.ReplaceAll(aReq.refName, "/", "-") + "." + aReq.Type.String()
}
-func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
+func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
ctx, committer, err := db.TxContext()
if err != nil {
return nil, err
}
defer committer.Close()
- archiver, err := models.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID)
+ archiver, err := repo_model.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID)
if err != nil {
return nil, err
}
@@ -128,17 +130,17 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
if archiver != nil {
// FIXME: If another process are generating it, we think it's not ready and just return
// Or we should wait until the archive generated.
- if archiver.Status == models.RepoArchiverGenerating {
+ if archiver.Status == repo_model.ArchiverGenerating {
return nil, nil
}
} else {
- archiver = &models.RepoArchiver{
+ archiver = &repo_model.RepoArchiver{
RepoID: r.RepoID,
Type: r.Type,
CommitID: r.CommitID,
- Status: models.RepoArchiverGenerating,
+ Status: repo_model.ArchiverGenerating,
}
- if err := models.AddRepoArchiver(ctx, archiver); err != nil {
+ if err := repo_model.AddRepoArchiver(ctx, archiver); err != nil {
return nil, err
}
}
@@ -150,9 +152,9 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
_, err = storage.RepoArchives.Stat(rPath)
if err == nil {
- if archiver.Status == models.RepoArchiverGenerating {
- archiver.Status = models.RepoArchiverReady
- if err = models.UpdateRepoArchiverStatus(ctx, archiver); err != nil {
+ if archiver.Status == repo_model.ArchiverGenerating {
+ archiver.Status = repo_model.ArchiverReady
+ if err = repo_model.UpdateRepoArchiverStatus(ctx, archiver); err != nil {
return nil, err
}
}
@@ -169,7 +171,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
rd.Close()
}()
var done = make(chan error)
- repo, err := archiver.LoadRepo()
+ repo, err := models.LoadArchiverRepo(archiver)
if err != nil {
return nil, fmt.Errorf("archiver.LoadRepo failed: %v", err)
}
@@ -180,7 +182,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
}
defer gitRepo.Close()
- go func(done chan error, w *io.PipeWriter, archiver *models.RepoArchiver, gitRepo *git.Repository) {
+ go func(done chan error, w *io.PipeWriter, archiver *repo_model.RepoArchiver, gitRepo *git.Repository) {
defer func() {
if r := recover(); r != nil {
done <- fmt.Errorf("%v", r)
@@ -218,9 +220,9 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
return nil, err
}
- if archiver.Status == models.RepoArchiverGenerating {
- archiver.Status = models.RepoArchiverReady
- if err = models.UpdateRepoArchiverStatus(ctx, archiver); err != nil {
+ if archiver.Status == repo_model.ArchiverGenerating {
+ archiver.Status = repo_model.ArchiverReady
+ if err = repo_model.UpdateRepoArchiverStatus(ctx, archiver); err != nil {
return nil, err
}
}
@@ -234,7 +236,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
// anything. In all cases, the caller should be examining the *ArchiveRequest
// being returned for completion, as it may be different than the one they passed
// in.
-func ArchiveRepository(request *ArchiveRequest) (*models.RepoArchiver, error) {
+func ArchiveRepository(request *ArchiveRequest) (*repo_model.RepoArchiver, error) {
return doArchive(request)
}
@@ -277,3 +279,56 @@ func StartArchive(request *ArchiveRequest) error {
}
return archiverQueue.Push(request)
}
+
+func deleteOldRepoArchiver(ctx context.Context, archiver *repo_model.RepoArchiver) error {
+ p, err := archiver.RelativePath()
+ if err != nil {
+ return err
+ }
+ if err := repo_model.DeleteRepoArchiver(ctx, archiver); err != nil {
+ return err
+ }
+ if err := storage.RepoArchives.Delete(p); err != nil {
+ log.Error("delete repo archive file failed: %v", err)
+ }
+ return nil
+}
+
+// DeleteOldRepositoryArchives deletes old repository archives.
+func DeleteOldRepositoryArchives(ctx context.Context, olderThan time.Duration) error {
+ log.Trace("Doing: ArchiveCleanup")
+
+ for {
+ archivers, err := repo_model.FindRepoArchives(repo_model.FindRepoArchiversOption{
+ ListOptions: db.ListOptions{
+ PageSize: 100,
+ Page: 1,
+ },
+ OlderThan: olderThan,
+ })
+ 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
+}
+
+// DeleteRepositoryArchives deletes all repositories' archives.
+func DeleteRepositoryArchives(ctx context.Context) error {
+ if err := repo_model.DeleteAllRepoArchives(); err != nil {
+ return err
+ }
+ return storage.Clean(storage.RepoArchives)
+}
diff --git a/services/archiver/archiver_test.go b/services/repository/archiver/archiver_test.go
index 5417dfb939..2c0b46d3d4 100644
--- a/services/archiver/archiver_test.go
+++ b/services/repository/archiver/archiver_test.go
@@ -17,7 +17,7 @@ import (
)
func TestMain(m *testing.M) {
- unittest.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", "..", ".."))
}
func TestArchive_Basic(t *testing.T) {