Skip to content

Commit bab95c3

Browse files
authored
Correctly handle failed migrations (#17575)
* Correctly handle failed migrations There is a bug in handling failed migrations whereby the migration task gets decoupled from the migration repository. This leads to a failure of the task to get deleted with the repository and also leads to the migration failed page resulting in a ISE. This PR removes the zeroing out of the task id from the migration but also makes the migration handler tolerate missing tasks much nicer. Fix #17571 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4744808 commit bab95c3

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

modules/task/migrate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func runMigrateTask(t *models.Task) (err error) {
5858
t.EndTime = timeutil.TimeStampNow()
5959
t.Status = structs.TaskStatusFailed
6060
t.Message = err.Error()
61+
// Ensure that the repo loaded before we zero out the repo ID from the task - thus ensuring that we can delete it
62+
_ = t.LoadRepo()
63+
6164
t.RepoID = 0
6265
if err := t.UpdateCols("status", "errors", "repo_id", "end_time"); err != nil {
6366
log.Error("Task UpdateCols failed: %v", err)

modules/task/task.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.
9090
return nil, err
9191
}
9292

93-
var task = models.Task{
93+
var task = &models.Task{
9494
DoerID: doer.ID,
9595
OwnerID: u.ID,
9696
Type: structs.TaskTypeMigrateRepo,
9797
Status: structs.TaskStatusQueue,
9898
PayloadContent: string(bs),
9999
}
100100

101-
if err := models.CreateTask(&task); err != nil {
101+
if err := models.CreateTask(task); err != nil {
102102
return nil, err
103103
}
104104

@@ -126,5 +126,5 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.
126126
return nil, err
127127
}
128128

129-
return &task, nil
129+
return task, nil
130130
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ migrate.migrate = Migrate From %s
904904
migrate.migrating = Migrating from <b>%s</b> ...
905905
migrate.migrating_failed = Migrating from <b>%s</b> failed.
906906
migrate.migrating_failed.error = Error: %s
907+
migrate.migrating_failed_no_addr = Migration failed.
907908
migrate.github.description = Migrate data from github.com or other Github instances.
908909
migrate.git.description = Migrate a repository only from any Git service.
909910
migrate.gitlab.description = Migrate data from gitlab.com or other GitLab instances.

routers/web/repo/view.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ func checkHomeCodeViewable(ctx *context.Context) {
583583
if ctx.Repo.Repository.IsBeingCreated() {
584584
task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
585585
if err != nil {
586+
if models.IsErrTaskDoesNotExist(err) {
587+
ctx.Data["Repo"] = ctx.Repo
588+
ctx.Data["CloneAddr"] = ""
589+
ctx.Data["Failed"] = true
590+
ctx.HTML(http.StatusOK, tplMigrating)
591+
return
592+
}
586593
ctx.ServerError("models.GetMigratingTask", err)
587594
return
588595
}

routers/web/user/task.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66

77
import (
88
"net/http"
9+
"strconv"
910

1011
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/context"
@@ -16,6 +17,12 @@ import (
1617
func TaskStatus(ctx *context.Context) {
1718
task, opts, err := models.GetMigratingTaskByID(ctx.ParamsInt64("task"), ctx.User.ID)
1819
if err != nil {
20+
if models.IsErrTaskDoesNotExist(err) {
21+
ctx.JSON(http.StatusNotFound, map[string]interface{}{
22+
"error": "task `" + strconv.FormatInt(ctx.ParamsInt64("task"), 10) + "` does not exist",
23+
})
24+
return
25+
}
1926
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
2027
"err": err,
2128
})

templates/repo/migrate/migrating.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
<p id="repo_migrating_progress_message"></p>
2626
</div>
2727
<div id="repo_migrating_failed" hidden>
28-
<p>{{.i18n.Tr "repo.migrate.migrating_failed" .CloneAddr | Safe}}</p>
28+
{{if .CloneAddr}}
29+
<p>{{.i18n.Tr "repo.migrate.migrating_failed" .CloneAddr | Safe}}</p>
30+
{{else}}
31+
<p>{{.i18n.Tr "repo.migrate.migrating_failed_no_addr" | Safe}}</p>
32+
{{end}}
2933
<p id="repo_migrating_failed_error"></p>
3034
</div>
3135
{{if and .Failed .Permission.IsAdmin}}

0 commit comments

Comments
 (0)