chore: 并入 iotlcm-bot 历史到 bot/(原提交 31dbc1d)
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
RunID string `json:"runId"`
|
||||
SentAt time.Time `json:"sentAt"`
|
||||
}
|
||||
|
||||
type Store struct{ dir string }
|
||||
|
||||
func New(dir string) Store { return Store{dir: dir} }
|
||||
func (s Store) Prepare() error {
|
||||
dir := filepath.Join(s.dir, "sent")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return fmt.Errorf("create sent marker directory: %w", err)
|
||||
}
|
||||
file, err := os.CreateTemp(dir, ".write-check-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("check sent marker directory: %w", err)
|
||||
}
|
||||
name := file.Name()
|
||||
if err := file.Close(); err != nil {
|
||||
_ = os.Remove(name)
|
||||
return fmt.Errorf("close sent marker write check: %w", err)
|
||||
}
|
||||
if err := os.Remove(name); err != nil {
|
||||
return fmt.Errorf("remove sent marker write check: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Store) Has(runID string) (bool, error) {
|
||||
_, err := os.Stat(filepath.Join(s.dir, "sent", runID+".json"))
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("check sent marker: %w", err)
|
||||
}
|
||||
func (s Store) Mark(runID string) error {
|
||||
if err := s.Prepare(); err != nil {
|
||||
return err
|
||||
}
|
||||
dir := filepath.Join(s.dir, "sent")
|
||||
body, _ := json.Marshal(State{RunID: runID, SentAt: time.Now()})
|
||||
tmp := filepath.Join(dir, fmt.Sprintf(".%s.tmp", runID))
|
||||
final := filepath.Join(dir, runID+".json")
|
||||
if err := os.WriteFile(tmp, body, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, final)
|
||||
}
|
||||
|
||||
// Dir 返回状态目录,供调用方复用(例如告警节流状态落盘)。
|
||||
func (s Store) Dir() string { return s.dir }
|
||||
@@ -0,0 +1,31 @@
|
||||
package state
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPrepareCreatesWritableSentDirectory(t *testing.T) {
|
||||
s := New(t.TempDir())
|
||||
if err := s.Prepare(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkIsIdempotentForLookup(t *testing.T) {
|
||||
s := New(t.TempDir())
|
||||
sent, err := s.Has("run-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sent {
|
||||
t.Fatal("run should not be marked before send")
|
||||
}
|
||||
if err := s.Mark("run-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sent, err = s.Has("run-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sent {
|
||||
t.Fatal("run should be marked")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user