feat(application): add Environment field and inject into Slurm job submission

This commit is contained in:
dailz
2026-04-21 10:23:31 +08:00
parent d9ca9233b3
commit 4fd331ebd8
4 changed files with 29 additions and 0 deletions

View File

@@ -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"` // 作用域
}

View File

@@ -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}

View File

@@ -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))

View File

@@ -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
}