1
0

feat(migration): migration from other services now happens in the background

This commit is contained in:
kolaente
2023-11-09 00:15:11 +01:00
parent 707bb6f89e
commit 46683a2516
10 changed files with 325 additions and 18 deletions

View File

@ -0,0 +1,33 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"code.vikunja.io/api/pkg/user"
)
// MigrationRequestedEvent represents a MigrationRequestedEvent event
type MigrationRequestedEvent struct {
Migrator interface{} `json:"migrator"`
User *user.User `json:"user"`
MigratorKind string `json:"migrator_kind"`
}
// Name defines the name for MigrationRequestedEvent
func (t *MigrationRequestedEvent) Name() string {
return "migration.requested"
}

View File

@ -19,6 +19,7 @@ package handler
import (
"net/http"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/migration"
user2 "code.vikunja.io/api/pkg/user"
@ -26,6 +27,12 @@ import (
"github.com/labstack/echo/v4"
)
var registeredMigrators map[string]*MigrationWeb
func init() {
registeredMigrators = make(map[string]*MigrationWeb)
}
// MigrationWeb holds the web migration handler
type MigrationWeb struct {
MigrationStruct func() migration.Migrator
@ -36,12 +43,13 @@ type AuthURL struct {
URL string `json:"url"`
}
// RegisterRoutes registers all routes for migration
func (mw *MigrationWeb) RegisterRoutes(g *echo.Group) {
// RegisterMigrator registers all routes for migration
func (mw *MigrationWeb) RegisterMigrator(g *echo.Group) {
ms := mw.MigrationStruct()
g.GET("/"+ms.Name()+"/auth", mw.AuthURL)
g.GET("/"+ms.Name()+"/status", mw.Status)
g.POST("/"+ms.Name()+"/migrate", mw.Migrate)
registeredMigrators[ms.Name()] = mw
}
// AuthURL is the web handler to get the auth url
@ -60,19 +68,29 @@ func (mw *MigrationWeb) Migrate(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
stats, err := migration.GetMigrationStatus(ms, user)
if err != nil {
return handler.HandleHTTPError(err, c)
}
if stats.FinishedAt.IsZero() {
return c.JSON(http.StatusOK, map[string]string{
"message": "Migration already running",
"running_since": stats.StartedAt.String(),
})
}
// Bind user request stuff
err = c.Bind(ms)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided: "+err.Error())
}
// Do the migration
err = ms.Migrate(user)
if err != nil {
return handler.HandleHTTPError(err, c)
}
err = migration.SetMigrationStatus(ms, user)
err = events.Dispatch(&MigrationRequestedEvent{
Migrator: ms,
MigratorKind: ms.Name(),
User: user,
})
if err != nil {
return handler.HandleHTTPError(err, c)
}

View File

@ -57,13 +57,18 @@ func (fw *FileMigratorWeb) Migrate(c echo.Context) error {
}
defer src.Close()
m, err := migration.StartMigration(ms, user)
if err != nil {
return handler.HandleHTTPError(err, c)
}
// Do the migration
err = ms.Migrate(user, src, file.Size)
if err != nil {
return handler.HandleHTTPError(err, c)
}
err = migration.SetMigrationStatus(ms, user)
err = migration.FinishMigration(m)
if err != nil {
return handler.HandleHTTPError(err, c)
}

View File

@ -0,0 +1,88 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"encoding/json"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/migration"
"code.vikunja.io/api/pkg/notifications"
"github.com/ThreeDotsLabs/watermill/message"
)
func RegisterListeners() {
events.RegisterListener((&MigrationRequestedEvent{}).Name(), &MigrationListener{})
}
// MigrationListener represents a listener
type MigrationListener struct {
}
// Name defines the name for the MigrationListener listener
func (s *MigrationListener) Name() string {
return "migration.listener"
}
// Handle is executed when the event MigrationListener listens on is fired
func (s *MigrationListener) Handle(msg *message.Message) (err error) {
event := &MigrationRequestedEvent{}
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return
}
mstr := registeredMigrators[event.MigratorKind]
event.Migrator = mstr.MigrationStruct()
// unmarshaling again to make sure the migrator has the correct type now
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return
}
ms := event.Migrator.(migration.Migrator)
m, err := migration.StartMigration(ms, event.User)
if err != nil {
return
}
log.Debugf("[Migration] Starting migration %d from %s for user %d", m.ID, event.MigratorKind, event.User.ID)
err = ms.Migrate(event.User)
if err != nil {
return
}
err = migration.FinishMigration(m)
if err != nil {
return
}
err = notifications.Notify(event.User, &MigrationDoneNotification{
MigratorName: ms.Name(),
})
if err != nil {
return
}
log.Debugf("[Migration] Successfully done migration %d from %s for user %d", m.ID, event.MigratorKind, event.User.ID)
return
}

View File

@ -0,0 +1,51 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/notifications"
)
// MigrationDoneNotification represents a MigrationDoneNotification notification
type MigrationDoneNotification struct {
MigratorName string
}
// ToMail returns the mail notification for MigrationDoneNotification
func (n *MigrationDoneNotification) ToMail() *notifications.Mail {
kind := cases.Title(language.English).String(n.MigratorName)
return notifications.NewMail().
Subject("The migration from "+kind+" to Vikunja was completed").
Line("Vikunja has imported all lists/projects, tasks, notes, reminders and files from "+kind+" you have access to.").
Action("View your imported projects in Vikunja", config.ServiceFrontendurl.GetString()).
Line("Have fun with your new (old) projects!")
}
// ToDB returns the MigrationDoneNotification notification in a format which can be saved in the db
func (n *MigrationDoneNotification) ToDB() interface{} {
return nil
}
// Name returns the name of the notification
func (n *MigrationDoneNotification) Name() string {
return "migration.done"
}