feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,284 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sea-vhost/internal/hosting"
|
||||
"sea-vhost/internal/isolation"
|
||||
"sea-vhost/internal/scope"
|
||||
"sea-vhost/internal/store"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type scheduleConfig struct {
|
||||
Target string `json:"target"`
|
||||
Arguments []string `json:"arguments"`
|
||||
}
|
||||
|
||||
func (s *Projects) openSchedule(v *store.UserSchedule) error {
|
||||
plain, e := s.credentials.Open("user-schedule:"+v.SecretRef, v.ConfigCipher)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
var cfg scheduleConfig
|
||||
if e = json.Unmarshal([]byte(plain), &cfg); e != nil {
|
||||
return e
|
||||
}
|
||||
v.Target = cfg.Target
|
||||
v.Arguments = cfg.Arguments
|
||||
return nil
|
||||
}
|
||||
func (s *Projects) UserSchedules(ctx context.Context, u store.User) ([]store.UserSchedule, error) {
|
||||
if u.Role != "tenant" || u.TenantID == nil {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
rows, e := s.store.UserSchedules(ctx, u.ID)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
for i := range rows {
|
||||
if e = s.openSchedule(&rows[i]); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
func (s *Projects) SaveUserSchedule(ctx context.Context, u store.User, v store.UserSchedule) (uint64, error) {
|
||||
if u.Role != "tenant" || u.TenantID == nil {
|
||||
return 0, store.ErrNotFound
|
||||
}
|
||||
if len(strings.TrimSpace(v.Name)) < 1 || len(v.Name) > 120 {
|
||||
return 0, errors.New("请输入不超过 120 字节的任务名称")
|
||||
}
|
||||
if len(v.Cron) > 120 || len(v.Timezone) > 80 {
|
||||
return 0, errors.New("计划表达式或时区过长")
|
||||
}
|
||||
if v.Timezone == "" {
|
||||
v.Timezone = "Asia/Shanghai"
|
||||
}
|
||||
cron, e := hosting.ParseCron(v.Cron, v.Timezone)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
next, e := cron.Next(time.Now())
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
ctx = scope.WithAccount(ctx, *u.TenantID)
|
||||
p, e := s.store.ProjectForUser(ctx, u, v.ProjectID)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
release, e := s.BeginHostAccess(p.ID)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
defer release()
|
||||
ctx = scope.WithProject(ctx, p.ID, p.NodeID)
|
||||
if e = s.ensureTenantWritable(ctx, p.TenantID); e != nil {
|
||||
return 0, e
|
||||
}
|
||||
site, e := s.store.Site(ctx, p.TenantID, p.ID, v.SiteID)
|
||||
if e != nil {
|
||||
return 0, store.ErrNotFound
|
||||
}
|
||||
params := hosting.Startup{Arguments: v.Arguments}
|
||||
if e = params.Validate(); e != nil {
|
||||
return 0, e
|
||||
}
|
||||
if len(v.Target) > 512 {
|
||||
return 0, errors.New("任务路径过长")
|
||||
}
|
||||
if v.Kind == "url" {
|
||||
uri, err := url.ParseRequestURI(v.Target)
|
||||
if err != nil || uri.IsAbs() || uri.Host != "" || !strings.HasPrefix(v.Target, "/") || strings.HasPrefix(v.Target, "//") || len(v.Arguments) > 0 {
|
||||
return 0, errors.New("URL 仅支持本站以 / 开头的路径,不能附带启动参数")
|
||||
}
|
||||
v.Version = ""
|
||||
if v.UseHTTPS && site.SSLStatus != "active" {
|
||||
return 0, errors.New("网站尚未启用 HTTPS")
|
||||
}
|
||||
} else {
|
||||
runtime := map[string]string{"php": "PHP", "python": "Python", "node": "Node.js"}[v.Kind]
|
||||
if runtime == "" || !hosting.ValidRelative(v.Target) {
|
||||
return 0, errors.New("请选择 PHP、Python、Node 脚本或本站 URL")
|
||||
}
|
||||
if e = s.validateRuntime(ctx, runtime, v.Version); e != nil {
|
||||
return 0, e
|
||||
}
|
||||
v.UseHTTPS = false
|
||||
}
|
||||
v.OwnershipVersion, e = s.store.ProjectOwnershipVersion(ctx, p.ID)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
v.UserID = u.ID
|
||||
v.AccountID = *u.TenantID
|
||||
v.NextRunAt = next
|
||||
token := make([]byte, 16)
|
||||
if _, e = rand.Read(token); e != nil {
|
||||
return 0, e
|
||||
}
|
||||
v.SecretRef = hex.EncodeToString(token)
|
||||
plain, _ := json.Marshal(scheduleConfig{v.Target, v.Arguments})
|
||||
v.ConfigCipher, e = s.credentials.Seal("user-schedule:"+v.SecretRef, string(plain))
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
v.Target = ""
|
||||
v.Arguments = []string{}
|
||||
return s.store.SaveUserSchedule(ctx, v)
|
||||
}
|
||||
func (s *Projects) ScheduleRuns(ctx context.Context, u store.User, id uint64) ([]store.ScheduleRun, error) {
|
||||
if u.Role != "tenant" || u.TenantID == nil {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
if _, e := s.store.UserSchedule(ctx, u.ID, id); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
rows, e := s.store.ScheduleRuns(ctx, u.ID, id)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
for i := range rows {
|
||||
if len(rows[i].OutputCipher) > 0 {
|
||||
rows[i].Output, e = s.credentials.Open(fmt.Sprintf("schedule-run:%d", rows[i].ID), rows[i].OutputCipher)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// Called only by the authenticated controller tick sent by the panel's cron.
|
||||
func (s *Projects) DispatchUserSchedules(ctx context.Context) error {
|
||||
return s.dispatchUserSchedules(ctx, true)
|
||||
}
|
||||
func (s *Projects) QueueUserSchedules(ctx context.Context) error {
|
||||
return s.dispatchUserSchedules(ctx, false)
|
||||
}
|
||||
func (s *Projects) dispatchUserSchedules(ctx context.Context, wait bool) error {
|
||||
rows, e := s.store.DueSchedules(ctx)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
s.scheduleInit.Do(func() { s.scheduleSlots = make(chan struct{}, 4) })
|
||||
var wg sync.WaitGroup
|
||||
for _, v := range rows {
|
||||
select {
|
||||
case s.scheduleSlots <- struct{}{}:
|
||||
default:
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(v store.UserSchedule) {
|
||||
defer wg.Done()
|
||||
defer func() { <-s.scheduleSlots }()
|
||||
task, cancel := context.WithTimeout(context.WithoutCancel(ctx), 310*time.Second)
|
||||
defer cancel()
|
||||
s.executeUserSchedule(task, v)
|
||||
}(v)
|
||||
}
|
||||
if wait {
|
||||
wg.Wait()
|
||||
}
|
||||
return s.store.PruneSchedules(ctx)
|
||||
}
|
||||
func (s *Projects) executeUserSchedule(ctx context.Context, v store.UserSchedule) {
|
||||
cron, e := hosting.ParseCron(v.Cron, v.Timezone)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
next, e := cron.Next(time.Now())
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
u, e := s.store.UserByID(ctx, v.UserID)
|
||||
if e != nil || u.Role != "tenant" || u.TenantID == nil || *u.TenantID != v.AccountID {
|
||||
return
|
||||
}
|
||||
ctx = scope.WithAccount(ctx, v.AccountID)
|
||||
p, e := s.store.ProjectForUser(ctx, u, v.ProjectID)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
ctx = scope.WithProject(ctx, p.ID, p.NodeID)
|
||||
release, e := s.BeginHostAccess(p.ID)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer release()
|
||||
nodeRelease, e := s.BeginRuntimeMutation(ctx)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer nodeRelease()
|
||||
run, e := s.store.ClaimSchedule(ctx, v.ID, next, v.SecretRef)
|
||||
if e != nil || run == 0 {
|
||||
return
|
||||
}
|
||||
code := 1
|
||||
output := "计划任务未执行"
|
||||
state := "failed"
|
||||
defer func() {
|
||||
cipher, sealErr := s.credentials.Seal(fmt.Sprintf("schedule-run:%d", run), output)
|
||||
if sealErr != nil {
|
||||
cipher = nil
|
||||
}
|
||||
done, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
_ = s.store.FinishSchedule(done, v.ID, run, state, code, cipher)
|
||||
s.store.Log(done, &p.TenantID, u.ID, "execute", "schedule", fmt.Sprint(v.ID), "", state == "succeeded", "执行用户计划任务:"+v.Name)
|
||||
}()
|
||||
if e = s.openSchedule(&v); e != nil {
|
||||
return
|
||||
}
|
||||
if e = s.ensureTenantWritable(ctx, p.TenantID); e != nil {
|
||||
output = e.Error()
|
||||
return
|
||||
}
|
||||
site, e := s.store.Site(ctx, p.TenantID, p.ID, v.SiteID)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
plan, _, e := s.store.PlanAndUsage(ctx, p.TenantID)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
env, e := s.loadStartup(ctx, site)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
port, e := s.store.ScheduleSitePort(ctx, site.ID, v.UseHTTPS)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
executor, ok := s.isolation.(isolation.ScheduleExecutor)
|
||||
if !ok {
|
||||
output = "节点不支持计划任务"
|
||||
return
|
||||
}
|
||||
task, cancel := context.WithTimeout(ctx, 300*time.Second)
|
||||
defer cancel()
|
||||
result, e := executor.ExecuteSchedule(task, isolation.ScheduleExecutionSpec{Project: isolation.ProjectSpec{TenantID: p.TenantID, HostName: p.HostName, RunPath: site.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(site.ProcessLimit)}, SiteName: lifecycleSiteName(p, site), Kind: v.Kind, Version: v.Version, Target: v.Target, Arguments: v.Arguments, Environment: env.Environment, Domain: site.Domain, Port: port, UseHTTPS: v.UseHTTPS})
|
||||
if e != nil {
|
||||
output = "节点任务执行失败或超时"
|
||||
return
|
||||
}
|
||||
code = result.ExitCode
|
||||
output = result.Output
|
||||
if result.Truncated {
|
||||
output += "\n[输出已截断至 64 KiB]"
|
||||
}
|
||||
if code == 0 {
|
||||
state = "succeeded"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user