Compare commits

...

3 Commits

Author SHA1 Message Date
dailz
347b0e1229 fix: remove redundant binding tags and clarify logger compress logic
- Remove binding:"required" from model fields that are manually validated in handlers. - Add parentheses to logger compress default to clarify operator precedence.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-10 09:25:46 +08:00
dailz
c070dd8abc fix(slurm): add default 30s timeout to HTTP client
Replaces http.DefaultClient with a client that has a 30s timeout to prevent indefinite hangs when the Slurm REST API is unresponsive.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-10 09:25:35 +08:00
dailz
1359730300 fix(store): return ErrRecordNotFound when updating non-existent template
RowsAffected == 0 now returns gorm.ErrRecordNotFound so the handler can respond with 404 instead of silently returning 200.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-10 09:21:03 +08:00
9 changed files with 32 additions and 14 deletions

View File

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

View File

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

View File

@@ -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"`

View File

@@ -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"`

View File

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

View File

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

View File

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

View File

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

View File

@@ -100,7 +100,13 @@ 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.