172 lines
5.9 KiB
Go
172 lines
5.9 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"sea-vhost/internal/isolation"
|
||
"sea-vhost/internal/scope"
|
||
"sea-vhost/internal/store"
|
||
"time"
|
||
)
|
||
|
||
func (s *Projects) processHostLifecycle(ctx context.Context) {
|
||
now := time.Now().UTC()
|
||
if err := s.store.AdvanceHostLifecycle(ctx, now); err != nil {
|
||
return
|
||
}
|
||
hosts, err := s.store.HostLifecycles(ctx)
|
||
if err != nil {
|
||
return
|
||
}
|
||
for _, h := range hosts {
|
||
operation, cancel := context.WithTimeout(scope.WithProject(ctx, h.ProjectID, h.NodeID), 15*time.Minute)
|
||
err = s.withHostOwnershipChange(operation, h.ProjectID, func() error {
|
||
// Reload after acquiring the host lock: a restore may have won the race.
|
||
current, e := s.store.HostLifecycle(operation, h.ProjectID)
|
||
if e != nil {
|
||
return e
|
||
}
|
||
if current.State != "grace" && current.State != "recycle" {
|
||
return nil
|
||
}
|
||
if e = s.suspendLifecycleHost(operation, current); e != nil {
|
||
return e
|
||
}
|
||
if current.State == "recycle" && current.PurgeAfter != nil && !current.PurgeAfter.After(time.Now().UTC()) {
|
||
p, e := s.store.Project(operation, current.TenantID, current.ProjectID)
|
||
if e != nil {
|
||
return e
|
||
}
|
||
plan, _, e := s.store.PlanAndUsage(operation, current.TenantID)
|
||
if e != nil {
|
||
return e
|
||
}
|
||
return s.purgeExpiredTenant(operation, store.TenantSummary{ID: current.TenantID, Name: p.HostName, BackupPurgeAfter: current.BackupPurgeAfter}, []store.Project{p}, plan)
|
||
}
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
_ = s.store.RecordRecycleError(context.WithoutCancel(operation), h.ProjectID, err.Error())
|
||
_ = s.store.EnqueueAdminNotification(context.WithoutCancel(operation), "critical", "lifecycle", "主机停用或回收清理未完成", fmt.Sprintf("主机 #%d,节点 %d:%v", h.ProjectID, h.NodeID, err))
|
||
}
|
||
cancel()
|
||
}
|
||
backups, err := s.store.ExpiredTenantBackups(ctx, now, 100)
|
||
if err != nil {
|
||
return
|
||
}
|
||
for _, backup := range backups {
|
||
op := scope.WithNode(ctx, backup.NodeID)
|
||
if err = s.isolation.DeleteProjectBackup(op, isolation.ProjectBackup{TenantID: backup.TenantID, HostName: backup.HostName, ArchivePath: backup.ArchivePath, SizeBytes: backup.SizeBytes}); err == nil {
|
||
_ = s.store.MarkTenantBackupDeleted(op, backup.ID)
|
||
}
|
||
}
|
||
}
|
||
|
||
// suspension is repeatable and attempts every service even if one fails.
|
||
func (s *Projects) suspendLifecycleHost(ctx context.Context, h store.HostLifecycle) error {
|
||
p, err := s.store.Project(ctx, h.TenantID, h.ProjectID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
plan, _, err := s.store.PlanAndUsage(ctx, h.TenantID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sites, err := s.store.ListSites(ctx, h.TenantID, h.ProjectID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
var failures []error
|
||
for _, site := range sites {
|
||
if site.Runtime == "PHP" {
|
||
phpSpec, e := s.phpIdentitySpec(ctx, p, site)
|
||
if e != nil {
|
||
failures = append(failures, e)
|
||
} else if e = s.isolation.SetPHPIdentityState(ctx, phpSpec, false); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
}
|
||
if site.PanelSiteID > 0 {
|
||
if e := s.panel.SetSiteState(ctx, site.PanelSiteID, site.Domain, false); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
}
|
||
}
|
||
spec := isolation.ProjectSpec{TenantID: h.TenantID, HostName: p.HostName, RunPath: p.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(p.ProcessLimit)}
|
||
if e := s.isolation.TerminateProcesses(ctx, spec); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
ftps, e := s.store.ListFTPResources(ctx, h.TenantID, &h.ProjectID)
|
||
if e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
for _, ftp := range ftps {
|
||
if e = s.panel.SetFTPState(ctx, ftp.PanelID, ftp.Username, false); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
}
|
||
databases, e := s.store.ListDatabaseResources(ctx, h.TenantID, &h.ProjectID)
|
||
if e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
for _, db := range databases {
|
||
if e = s.isolation.ConfigureDatabaseAccess(ctx, isolation.DatabaseAccessSpec{Project: spec, Database: db.Name, ReadOnly: true, BlockCleanup: true}); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
}
|
||
if manager, ok := s.isolation.(isolation.CredentialSessions); ok {
|
||
if e = manager.DisconnectCredentialSessions(ctx, spec); e != nil {
|
||
failures = append(failures, e)
|
||
}
|
||
} else {
|
||
failures = append(failures, errors.New("节点不支持撤销 FTP 与数据库现有连接"))
|
||
}
|
||
if len(failures) > 0 {
|
||
return errors.Join(failures...)
|
||
}
|
||
if e = s.store.ForceTenantStopped(ctx, h.TenantID); e != nil {
|
||
return e
|
||
}
|
||
return s.store.RecordRecycleError(ctx, h.ProjectID, "")
|
||
}
|
||
func (s *Projects) RecycleManagedHost(ctx context.Context, id uint64) error {
|
||
return s.withHostOwnershipChange(ctx, id, func() error {
|
||
if err := s.store.RecycleHost(ctx, id, time.Now().UTC()); err != nil {
|
||
return err
|
||
}
|
||
h, err := s.store.HostLifecycle(ctx, id)
|
||
if err == nil {
|
||
err = s.suspendLifecycleHost(ctx, h)
|
||
}
|
||
if err != nil {
|
||
_ = s.store.RecordRecycleError(context.WithoutCancel(ctx), id, err.Error())
|
||
return fmt.Errorf("主机已进入回收期,部分服务停用失败,调度器将重试:%w", err)
|
||
}
|
||
return nil
|
||
})
|
||
}
|
||
func (s *Projects) RestoreManagedHost(ctx context.Context, id uint64) error {
|
||
return s.withHostOwnershipChange(ctx, id, func() error {
|
||
// Complete suspension before lifting the access barrier; websites stay stopped.
|
||
h, err := s.store.HostLifecycle(ctx, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if h.State != "recycle" {
|
||
return errors.New("主机不在回收期")
|
||
}
|
||
if err = s.suspendLifecycleHost(ctx, h); err != nil {
|
||
return err
|
||
}
|
||
return s.store.RestoreHost(ctx, id, time.Now().UTC())
|
||
})
|
||
}
|
||
func (s *Projects) UpdateManagedNode(ctx context.Context, id uint64, input store.Node) error {
|
||
return s.withNodeMaintenanceWait(scope.WithNode(ctx, id), 10*time.Second, func() error { return s.store.UpdateNodeMetadata(ctx, id, input) })
|
||
}
|
||
func (s *Projects) DeleteManagedNode(ctx context.Context, id uint64) error {
|
||
return s.withNodeMaintenanceWait(scope.WithNode(ctx, id), 10*time.Second, func() error { return s.store.DeleteNode(ctx, id) })
|
||
}
|