-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Make Migrations Cancelable #12917
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
Make Migrations Cancelable #12917
Changes from all commits
b5c9eaf
fe9557a
8da1497
714351a
fd09715
e174d8c
ab941d7
6d91ba8
cab5d46
a009150
1d206d8
e757442
d9b6d09
934ed01
6a89ee6
f7e9022
cfed3be
b5c2461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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 migrations | ||
|
||
import ( | ||
"xorm.io/xorm" | ||
) | ||
|
||
func addProcessToTask(x *xorm.Engine) error { | ||
type Task struct { | ||
Process string // process GUID and PID | ||
} | ||
|
||
return x.Sync2(new(Task)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ type Task struct { | |
PayloadContent string `xorm:"TEXT"` | ||
Errors string `xorm:"TEXT"` // if task failed, saved the error reason | ||
Created timeutil.TimeStamp `xorm:"created"` | ||
Process string // process GUID and PID | ||
} | ||
|
||
// LoadRepo loads repository of the task | ||
|
@@ -114,6 +115,17 @@ func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) { | |
return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name()) | ||
} | ||
|
||
// GUIDandPID return GUID and PID of a running task | ||
func (task *Task) GUIDandPID() (guid int64, pid int64) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think simply returning |
||
if task.Status != structs.TaskStatusRunning { | ||
return 0, 0 | ||
} | ||
if _, err := fmt.Sscanf(task.Process, "%d/%d", &guid, &pid); err != nil { | ||
return 0, 0 | ||
} | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does that compile? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it compiles, I can understand why, but I wouldn't have dreamed up such a syntax ever… There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it do compile :) |
||
} | ||
|
||
// ErrTaskDoesNotExist represents a "TaskDoesNotExist" kind of error. | ||
type ErrTaskDoesNotExist struct { | ||
ID int64 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ import ( | |
"sort" | ||
"sync" | ||
"time" | ||
|
||
gouuid "github.com/google/uuid" | ||
) | ||
|
||
// TODO: This packages still uses a singleton for the Manager. | ||
|
@@ -44,18 +46,27 @@ type Manager struct { | |
|
||
counter int64 | ||
processes map[int64]*Process | ||
|
||
// guid is a unique id for a gitea instance | ||
guid int64 | ||
} | ||
|
||
// GetManager returns a Manager and initializes one as singleton if there's none yet | ||
func GetManager() *Manager { | ||
if manager == nil { | ||
manager = &Manager{ | ||
processes: make(map[int64]*Process), | ||
guid: int64(gouuid.New().ID()), | ||
} | ||
} | ||
return manager | ||
} | ||
|
||
// GUID return a unique id of a gitea instance | ||
func (pm *Manager) GUID() int64 { | ||
return pm.guid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The guid will be changed once the Gitea restarted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
} | ||
|
||
// Add a process to the ProcessManager and returns its PID. | ||
func (pm *Manager) Add(description string, cancel context.CancelFunc) int64 { | ||
pm.mutex.Lock() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,22 @@ | |
package repo | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/auth" | ||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/migrations" | ||
"code.gitea.io/gitea/modules/process" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/task" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
"code.gitea.io/gitea/modules/util" | ||
repo_service "code.gitea.io/gitea/services/repository" | ||
) | ||
|
||
const ( | ||
|
@@ -188,3 +193,53 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) { | |
|
||
handleMigrateError(ctx, ctxUser, err, "MigratePost", tpl, &form) | ||
} | ||
|
||
// CancelMigration cancels a running migration | ||
func CancelMigration(ctx *context.Context) { | ||
if !ctx.Repo.IsAdmin() { | ||
ctx.Error(http.StatusNotFound) | ||
return | ||
} | ||
|
||
if ctx.Repo.Repository.Status != models.RepositoryBeingMigrated { | ||
ctx.Error(http.StatusConflict, "repo already migrated") | ||
return | ||
} | ||
|
||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
guid := process.GetManager().GUID() | ||
task, err := models.GetMigratingTask(ctx.Repo.Repository.ID) | ||
if err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
if task.Status != structs.TaskStatusRunning { | ||
task.Status = structs.TaskStatusStopped | ||
task.EndTime = timeutil.TimeStampNow() | ||
task.RepoID = 0 | ||
|
||
if err = task.UpdateCols("status", "repo_id", "end_time"); err != nil { | ||
log.Error("Task UpdateCols failed: %v", err) | ||
} | ||
|
||
if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { | ||
ctx.ServerError("DeleteRepository", err) | ||
return | ||
} | ||
|
||
ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) | ||
ctx.Redirect(ctx.Repo.Owner.DashboardLink()) | ||
return | ||
} | ||
|
||
tGUID, tPID := task.GUIDandPID() | ||
if guid == tGUID { | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Trace("Migration [%s] cancel PID [%d] ", ctx.Repo.Repository.FullName(), tPID) | ||
process.GetManager().Cancel(tPID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be best to delete the repository here explicitly. |
||
ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) | ||
ctx.Redirect(ctx.Repo.Owner.DashboardLink()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it even sensible to flash a message if the next action is to redirect back to the homepage? |
||
return | ||
} | ||
|
||
ctx.Flash.Error(ctx.Tr("repo.migrate.task_not_found", ctx.Repo.Repository.FullName())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user will see a migrating task, but when click cancel, gitea will say it's not exist. This is still a problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it even sensible to flash a message if the next action is to redirect back to the homepage? |
||
ctx.Redirect(ctx.Repo.Owner.DashboardLink()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this file certainly needs to be renamed until this PR will be merged.