eclipse/lib/common/jobtypes.go

117 lines
2.1 KiB
Go

// Copyright (C) 2024 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package common
import (
"encoding"
"fmt"
"io/fs"
"os"
"time"
)
type JobID int
type JobStatus int
const (
StatusNew JobStatus = iota
StatusRunning
StatusSucceeded
StatusFailed
StatusError
StatusAll JobStatus = -1
)
var (
_ fmt.Stringer = JobStatus(0)
_ encoding.TextMarshaler = JobStatus(0)
_ encoding.TextUnmarshaler = (*JobStatus)(nil)
)
// String implements [fmt.Stringer].
func (s JobStatus) String() string {
switch s {
case StatusNew:
return "new"
case StatusRunning:
return "running"
case StatusSucceeded:
return "succeeded"
case StatusFailed:
return "failed"
case StatusError:
return "error"
default:
return fmt.Sprintf("InvalidStatus(%d)", s)
}
}
// MarshalText implements [encoding.TextMarshaler].
func (s JobStatus) MarshalText() ([]byte, error) {
switch s {
case StatusNew:
return []byte("new"), nil
case StatusRunning:
return []byte("running"), nil
case StatusSucceeded:
return []byte("succeeded"), nil
case StatusFailed:
return []byte("failed"), nil
case StatusError:
return []byte("error"), nil
default:
return nil, fmt.Errorf("unknown job status: %d", s)
}
}
// UnmarshalText implements [encoding.TextUnmarshaler].
func (s *JobStatus) UnmarshalText(text []byte) error {
switch string(text) {
case "new":
*s = StatusNew
case "running":
*s = StatusRunning
case "succeeded":
*s = StatusSucceeded
case "failed":
*s = StatusFailed
case "error":
*s = StatusError
default:
return fmt.Errorf("unknown job status: %q", text)
}
return nil
}
func (s JobStatus) HasLog() bool {
return s != StatusNew
}
func (s JobStatus) Finished() bool {
switch s {
case StatusSucceeded, StatusFailed, StatusError:
return true
default:
return false
}
}
type Job struct {
ID JobID
TriggerExpressions []string
TriggerValues []string
Command string
Status JobStatus
StatusMsg string `json:",omitempty"`
CreatedAt time.Time
UpdatedAt time.Time
Dir string `json:"-"`
}
func (j Job) DirFS() fs.FS {
return os.DirFS(j.Dir)
}