From 4fd331ebd89ef208e39f1e9d758af9283d0c5896 Mon Sep 17 00:00:00 2001 From: dailz Date: Tue, 21 Apr 2026 10:23:31 +0800 Subject: [PATCH] feat(application): add Environment field and inject into Slurm job submission --- internal/model/application.go | 3 +++ internal/service/job_service.go | 3 +++ internal/service/task_service.go | 9 +++++++++ internal/store/application_store.go | 14 ++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/internal/model/application.go b/internal/model/application.go index c6a52bd..b572f9e 100644 --- a/internal/model/application.go +++ b/internal/model/application.go @@ -24,6 +24,7 @@ type Application struct { Category string `gorm:"size:255" json:"category,omitempty"` // 分类 ScriptTemplate string `gorm:"type:text;not null" json:"script_template"` // 脚本模板 Parameters json.RawMessage `gorm:"type:json" json:"parameters,omitempty"` // 参数表单JSON + Environment json.RawMessage `gorm:"type:text" json:"environment,omitempty"` // 环境变量JSON Scope string `gorm:"size:50;default:'system'" json:"scope,omitempty"` // 作用域(system/user) CreatedBy int64 `json:"created_by,omitempty"` // 创建者ID CreatedAt time.Time `json:"created_at"` // 创建时间 @@ -53,6 +54,7 @@ type CreateApplicationRequest struct { Category string `json:"category,omitempty"` // 分类 ScriptTemplate string `json:"script_template" binding:"required"` // 脚本模板(必填) Parameters json.RawMessage `json:"parameters,omitempty"` // 参数表单JSON + Environment map[string]string `json:"environment,omitempty"` // 环境变量 Scope string `json:"scope,omitempty"` // 作用域 } @@ -64,6 +66,7 @@ type UpdateApplicationRequest struct { Category *string `json:"category,omitempty"` // 分类 ScriptTemplate *string `json:"script_template,omitempty"` // 脚本模板 Parameters *json.RawMessage `json:"parameters,omitempty"` // 参数表单JSON + Environment *json.RawMessage `json:"environment,omitempty"` // 环境变量 Scope *string `json:"scope,omitempty"` // 作用域 } diff --git a/internal/service/job_service.go b/internal/service/job_service.go index 90656ae..dec5130 100644 --- a/internal/service/job_service.go +++ b/internal/service/job_service.go @@ -49,6 +49,9 @@ func (s *JobService) SubmitJob(ctx context.Context, req *model.SubmitJobRequest) "PATH=/usr/local/bin:/usr/bin:/bin", "HOME=/root", } + for k, v := range req.Environment { + jobDesc.Environment = append(jobDesc.Environment, k+"="+v) + } if req.MemoryPerNode != nil { jobDesc.MemoryPerNode = &slurm.Uint64NoVal{Set: slurm.Ptr(true), Number: req.MemoryPerNode} diff --git a/internal/service/task_service.go b/internal/service/task_service.go index 7a5f393..8a8d031 100644 --- a/internal/service/task_service.go +++ b/internal/service/task_service.go @@ -276,6 +276,14 @@ func (s *TaskService) ProcessTask(ctx context.Context, taskID int64) error { } } + // 15a. Parse app environment + var appEnv map[string]string + if len(app.Environment) > 0 { + if err := json.Unmarshal(app.Environment, &appEnv); err != nil { + return fail(model.TaskStepSubmitting, fmt.Sprintf("parse application environment: %v", err)) + } + } + // 16. Parse task values values := make(map[string]string) if len(task.Values) > 0 { @@ -413,6 +421,7 @@ func (s *TaskService) ProcessTask(ctx context.Context, taskID int64) error { Dependency: task.Dependency, Requeue: task.Requeue, KillOnNodeFail: task.KillOnNodeFail, + Environment: appEnv, }) if err != nil { return fail(model.TaskStepSubmitting, fmt.Sprintf("submit job: %v", err)) diff --git a/internal/store/application_store.go b/internal/store/application_store.go index 6d0600a..ee7abf1 100644 --- a/internal/store/application_store.go +++ b/internal/store/application_store.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "gcy_hpc_server/internal/model" @@ -52,6 +53,15 @@ func (s *ApplicationStore) Create(ctx context.Context, req *model.CreateApplicat params = json.RawMessage(`[]`) } + var envJSON json.RawMessage + if len(req.Environment) > 0 { + b, err := json.Marshal(req.Environment) + if err != nil { + return 0, fmt.Errorf("marshal environment: %w", err) + } + envJSON = b + } + app := &model.Application{ Name: req.Name, Description: req.Description, @@ -59,6 +69,7 @@ func (s *ApplicationStore) Create(ctx context.Context, req *model.CreateApplicat Category: req.Category, ScriptTemplate: req.ScriptTemplate, Parameters: params, + Environment: envJSON, Scope: req.Scope, } if err := s.db.WithContext(ctx).Create(app).Error; err != nil { @@ -87,6 +98,9 @@ func (s *ApplicationStore) Update(ctx context.Context, id int64, req *model.Upda if req.Parameters != nil { updates["parameters"] = *req.Parameters } + if req.Environment != nil { + updates["environment"] = *req.Environment + } if req.Scope != nil { updates["scope"] = *req.Scope }