pgraph, resources: Improve Init/Close and Worker status

This should do some rough cleanups around the Init/Close of resources,
and tracking of Worker function status.
This commit is contained in:
James Shubin
2017-02-02 19:48:42 -05:00
parent bec7f1726f
commit 2da21f90f4
5 changed files with 54 additions and 34 deletions

View File

@@ -140,7 +140,6 @@ type Base interface {
Events() chan *event.Event
AssociateData(*Data)
IsWorking() bool
SetWorking(bool)
Converger() converger.Converger
RegisterConverger()
UnregisterConverger()
@@ -286,6 +285,9 @@ func (obj *BaseRes) Validate() error {
// Init initializes structures like channels if created without New constructor.
func (obj *BaseRes) Init() error {
if obj.debug {
log.Printf("%s[%s]: Init()", obj.Kind(), obj.GetName())
}
if obj.kind == "" {
return fmt.Errorf("Resource did not set kind!")
}
@@ -307,13 +309,18 @@ func (obj *BaseRes) Init() error {
//}
// TODO: this StatefulBool implementation could be eventually swappable
//obj.refreshState = &DiskBool{Path: path.Join(dir, refreshPathToken)}
obj.working = true // Worker method should now be running...
return nil
}
// Close shuts down and performs any cleanup.
func (obj *BaseRes) Close() error {
if obj.debug {
log.Printf("%s[%s]: Close()", obj.Kind(), obj.GetName())
}
obj.mutex.Lock()
obj.working = false // obj.SetWorking(false)
obj.working = false // Worker method should now be closing...
close(obj.events) // this is where we properly close this channel!
obj.mutex.Unlock()
return nil
@@ -357,20 +364,11 @@ func (obj *BaseRes) AssociateData(data *Data) {
obj.debug = data.Debug
}
// IsWorking tells us if the Worker() function is running.
// IsWorking tells us if the Worker() function is running. Not thread safe.
func (obj *BaseRes) IsWorking() bool {
obj.mutex.Lock()
defer obj.mutex.Unlock()
return obj.working
}
// SetWorking tracks the state of if Worker() function is running.
func (obj *BaseRes) SetWorking(b bool) {
obj.mutex.Lock()
defer obj.mutex.Unlock()
obj.working = b
}
// Converger returns the converger object used by the system. It can be used to
// register new convergers if needed.
func (obj *BaseRes) Converger() converger.Converger {