Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: abstract away the panic recovery boilerplate (#1319)
Yilong Li491c2ea367d2
diff --git a/server/task_executor.go b/server/task_executor.goindex 061aa96fc2..96d7af67ed 100644--- a/server/task_executor.go+++ b/server/task_executor.go@@ -27,6 +27,24 @@ type TaskExecutor interface {RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error)}+// RunTaskExecutorOnce wraps a TaskExecutor.RunOnce call with panic recovery+func RunTaskExecutorOnce(ctx context.Context, l *zap.Logger, exec TaskExecutor, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {+ defer func() {+ if r := recover(); r != nil {+ panicErr, ok := r.(error)+ if !ok {+ panicErr = fmt.Errorf("%v", r)+ }+ l.Error("TaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))+ terminated = true+ result = nil+ err = fmt.Errorf("encounter internal error when executing task")+ }+ }()++ return exec.RunOnce(ctx, server, task)+}+func preMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) (*db.MigrationInfo, error) {if task.Database == nil {msg := "missing database when updating schema"diff --git a/server/task_executor_data_update.go b/server/task_executor_data_update.goindex 6e9193d1a6..65758b27cb 100644--- a/server/task_executor_data_update.go+++ b/server/task_executor_data_update.go@@ -24,18 +24,6 @@ type DataUpdateTaskExecutor struct {// RunOnce will run the data update (DML) task executor once.func (exec *DataUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("DataUpdateTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when executing sql")- }- }()-payload := &api.TaskDatabaseDataUpdatePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid database data update payload: %w", err)diff --git a/server/task_executor_database_backup.go b/server/task_executor_database_backup.goindex fc1ebb8045..89f3e6b366 100644--- a/server/task_executor_database_backup.go+++ b/server/task_executor_database_backup.go@@ -25,18 +25,6 @@ type DatabaseBackupTaskExecutor struct {// RunOnce will run database backup once.func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("DatabaseBackupTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when backing database")- }- }()-payload := &api.TaskDatabaseBackupPayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid database backup payload: %w", err)diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.goindex 0be1e4fc08..a36f1cd5fb 100644--- a/server/task_executor_database_create.go+++ b/server/task_executor_database_create.go@@ -26,18 +26,6 @@ type DatabaseCreateTaskExecutor struct {// RunOnce will run the database create task executor once.func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("DatabaseCreateTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when creating database")- }- }()-payload := &api.TaskDatabaseCreatePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid create database payload: %w", err)diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.goindex 41f19fcb6a..3dc0e0b6d4 100644--- a/server/task_executor_database_restore.go+++ b/server/task_executor_database_restore.go@@ -30,18 +30,6 @@ type DatabaseRestoreTaskExecutor struct {// RunOnce will run database restore once.func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("DatabaseRestoreTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when restoring the database")- }- }()-payload := &api.TaskDatabaseRestorePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid database backup payload: %w", err)diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.goindex 850fcb7bf6..8b75fe61ca 100644--- a/server/task_executor_schema_update.go+++ b/server/task_executor_schema_update.go@@ -23,18 +23,6 @@ type SchemaUpdateTaskExecutor struct {// RunOnce will run the schema update (DDL) task executor once.func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("SchemaUpdateTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when executing sql")- }- }()-payload := &api.TaskDatabaseSchemaUpdatePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid database schema update payload: %w", err)diff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.goindex 337f426284..bd298e4db9 100644--- a/server/task_executor_schema_update_ghost_sync.go+++ b/server/task_executor_schema_update_ghost_sync.go@@ -122,18 +122,6 @@ func newMigrationContext(config ghostConfig) (*base.MigrationContext, error) {// RunOnce will run SchemaUpdateGhostSync task once.func (exec *SchemaUpdateGhostSyncTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, err error) {- defer func() {- if r := recover(); r != nil {- panicErr, ok := r.(error)- if !ok {- panicErr = fmt.Errorf("%v", r)- }- exec.l.Error("SchemaUpdateGhostSyncTaskExecutor PANIC RECOVER", zap.Error(panicErr), zap.Stack("stack"))- terminated = true- err = fmt.Errorf("encounter internal error when executing migration using gh-ost")- }- }()-payload := &api.TaskDatabaseSchemaUpdateGhostSyncPayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {return true, nil, fmt.Errorf("invalid database schema update gh-ost sync payload: %w", err)diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex 8a81d1f36e..c70b430483 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -141,7 +141,7 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {delete(tasks.running, task.ID)tasks.mu.Unlock()}()- done, result, err := executor.RunOnce(ctx, s.server, task)+ done, result, err := RunTaskExecutorOnce(ctx, s.l, executor, s.server, task)if done {if err == nil {bytes, err := json.Marshal(*result)