55 "os"
66 "path/filepath"
77 "sort"
8+ "time"
89
910 "github.com/docker/docker/pkg/ioutils"
1011 "github.com/gofrs/flock"
@@ -15,6 +16,7 @@ import (
1516const (
1617 instanceDir = "instances"
1718 defaultsDir = "defaults"
19+ activityDir = "activity"
1820)
1921
2022func New (root string ) (* Store , error ) {
@@ -24,6 +26,9 @@ func New(root string) (*Store, error) {
2426 if err := os .MkdirAll (filepath .Join (root , defaultsDir ), 0700 ); err != nil {
2527 return nil , err
2628 }
29+ if err := os .MkdirAll (filepath .Join (root , activityDir ), 0700 ); err != nil {
30+ return nil , err
31+ }
2732 return & Store {root : root }, nil
2833}
2934
@@ -86,6 +91,9 @@ func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
8691 if err := json .Unmarshal (dt , & ng ); err != nil {
8792 return nil , err
8893 }
94+ if ng .LastActivity , err = t .GetLastActivity (& ng ); err != nil {
95+ return nil , err
96+ }
8997 return & ng , nil
9098}
9199
@@ -94,6 +102,9 @@ func (t *Txn) Save(ng *NodeGroup) error {
94102 if err != nil {
95103 return err
96104 }
105+ if err := t .UpdateLastActivity (ng ); err != nil {
106+ return err
107+ }
97108 dt , err := json .Marshal (ng )
98109 if err != nil {
99110 return err
@@ -106,6 +117,9 @@ func (t *Txn) Remove(name string) error {
106117 if err != nil {
107118 return err
108119 }
120+ if err := t .RemoveLastActivity (name ); err != nil {
121+ return err
122+ }
109123 return os .RemoveAll (filepath .Join (t .s .root , instanceDir , name ))
110124}
111125
@@ -135,6 +149,29 @@ func (t *Txn) SetCurrent(key, name string, global, def bool) error {
135149 return nil
136150}
137151
152+ func (t * Txn ) UpdateLastActivity (ng * NodeGroup ) error {
153+ return ioutils .AtomicWriteFile (filepath .Join (t .s .root , activityDir , ng .Name ), []byte (time .Now ().UTC ().Format (time .RFC3339 )), 0600 )
154+ }
155+
156+ func (t * Txn ) GetLastActivity (ng * NodeGroup ) (la time.Time , _ error ) {
157+ dt , err := os .ReadFile (filepath .Join (t .s .root , activityDir , ng .Name ))
158+ if err != nil {
159+ if os .IsNotExist (errors .Cause (err )) {
160+ return la , nil
161+ }
162+ return la , err
163+ }
164+ return time .Parse (time .RFC3339 , string (dt ))
165+ }
166+
167+ func (t * Txn ) RemoveLastActivity (name string ) error {
168+ name , err := ValidateName (name )
169+ if err != nil {
170+ return err
171+ }
172+ return os .RemoveAll (filepath .Join (t .s .root , activityDir , name ))
173+ }
174+
138175func (t * Txn ) reset (key string ) error {
139176 dt , err := json .Marshal (current {Key : key })
140177 if err != nil {
0 commit comments