Compare commits
3 Commits
4ff02d4a80
...
347b0e1229
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
347b0e1229 | ||
|
|
c070dd8abc | ||
|
|
1359730300 |
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"gcy_hpc_server/internal/model"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TemplateHandler struct {
|
||||
@@ -111,8 +113,13 @@ func (h *TemplateHandler) UpdateTemplate(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := h.store.Update(c.Request.Context(), id, &req); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
h.logger.Warn("template not found for update", zap.Int64("id", id))
|
||||
server.NotFound(c, "template not found")
|
||||
return
|
||||
}
|
||||
h.logger.Error("failed to update template", zap.Int64("id", id), zap.Error(err))
|
||||
server.InternalError(c, err.Error())
|
||||
server.InternalError(c, "failed to update template")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func NewLogger(cfg config.LogConfig) (*zap.Logger, error) {
|
||||
maxSize := applyDefaultInt(cfg.MaxSize, 100)
|
||||
maxBackups := applyDefaultInt(cfg.MaxBackups, 5)
|
||||
maxAge := applyDefaultInt(cfg.MaxAge, 30)
|
||||
compress := cfg.Compress || cfg.MaxSize == 0 && cfg.MaxBackups == 0 && cfg.MaxAge == 0
|
||||
compress := cfg.Compress || (cfg.MaxSize == 0 && cfg.MaxBackups == 0 && cfg.MaxAge == 0)
|
||||
|
||||
lj := &lumberjack.Logger{
|
||||
Filename: cfg.FilePath,
|
||||
|
||||
@@ -2,7 +2,7 @@ package model
|
||||
|
||||
// SubmitJobRequest is the API request for submitting a job.
|
||||
type SubmitJobRequest struct {
|
||||
Script string `json:"script" binding:"required"`
|
||||
Script string `json:"script"`
|
||||
Partition string `json:"partition,omitempty"`
|
||||
QOS string `json:"qos,omitempty"`
|
||||
CPUs int32 `json:"cpus,omitempty"`
|
||||
|
||||
@@ -22,9 +22,9 @@ func (JobTemplate) TableName() string { return "job_templates" }
|
||||
|
||||
// CreateTemplateRequest is the API request for creating a template.
|
||||
type CreateTemplateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Script string `json:"script" binding:"required"`
|
||||
Script string `json:"script"`
|
||||
Partition string `json:"partition,omitempty"`
|
||||
QOS string `json:"qos,omitempty"`
|
||||
CPUs int `json:"cpus,omitempty"`
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,6 +17,8 @@ const (
|
||||
DefaultBaseURL = "http://localhost:6820/"
|
||||
// DefaultUserAgent is the default User-Agent header value.
|
||||
DefaultUserAgent = "slurm-go-sdk"
|
||||
// DefaultTimeout is the default HTTP request timeout.
|
||||
DefaultTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
// Client manages communication with the Slurm REST API.
|
||||
@@ -85,7 +88,7 @@ type Response struct {
|
||||
// http.DefaultClient is used.
|
||||
func NewClient(baseURL string, httpClient *http.Client) (*Client, error) {
|
||||
if httpClient == nil {
|
||||
httpClient = http.DefaultClient
|
||||
httpClient = &http.Client{Timeout: DefaultTimeout}
|
||||
}
|
||||
|
||||
parsedURL, err := url.Parse(baseURL)
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestNewClient(t *testing.T) {
|
||||
t.Errorf("expected UserAgent %q, got %q", DefaultUserAgent, client.UserAgent)
|
||||
}
|
||||
if client.client == nil {
|
||||
t.Error("expected http.Client to be initialized (nil httpClient should default to http.DefaultClient)")
|
||||
t.Error("expected http.Client to be initialized (nil httpClient should create a client with default timeout)")
|
||||
}
|
||||
|
||||
_, err = NewClient("://invalid", nil)
|
||||
|
||||
@@ -24,6 +24,8 @@ func defaultClientConfig() *clientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
const defaultHTTPTimeout = 30 * time.Second
|
||||
|
||||
// WithJWTKey specifies the path to the JWT key file.
|
||||
func WithJWTKey(path string) ClientOption {
|
||||
return func(c *clientConfig) error {
|
||||
@@ -89,11 +91,12 @@ func NewClientWithOpts(baseURL string, opts ...ClientOption) (*Client, error) {
|
||||
}
|
||||
|
||||
tr := NewJWTAuthTransport(cfg.username, key, transportOpts...)
|
||||
httpClient = tr.Client()
|
||||
httpClient = &http.Client{
|
||||
Transport: tr,
|
||||
Timeout: defaultHTTPTimeout,
|
||||
}
|
||||
} else if cfg.httpClient != nil {
|
||||
httpClient = cfg.httpClient
|
||||
} else {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
|
||||
return NewClient(baseURL, httpClient)
|
||||
|
||||
@@ -2,7 +2,6 @@ package slurm
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -61,8 +60,8 @@ func TestNewClientWithOpts_BackwardCompatible(t *testing.T) {
|
||||
if client == nil {
|
||||
t.Fatal("expected non-nil client")
|
||||
}
|
||||
if client.client != http.DefaultClient {
|
||||
t.Error("expected http.DefaultClient when no options provided")
|
||||
if client.client.Timeout != DefaultTimeout {
|
||||
t.Errorf("expected Timeout=%v, got %v", DefaultTimeout, client.client.Timeout)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -100,8 +100,14 @@ func (s *TemplateStore) Update(ctx context.Context, id int64, req *model.UpdateT
|
||||
}
|
||||
|
||||
result := s.db.WithContext(ctx).Model(&model.JobTemplate{}).Where("id = ?", id).Updates(updates)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a job template by ID. Idempotent — returns nil even if the row doesn't exist.
|
||||
func (s *TemplateStore) Delete(ctx context.Context, id int64) error {
|
||||
|
||||
Reference in New Issue
Block a user