Mega patch

This is still a dirty prototype, so please excuse the mess. Please
excuse the fact that this is a mega patch. Once things settle down this
won't happen any more.

Some of the changes squashed into here include:
* Merge vertex loop with type loop
(The file watcher seems to cache events anyways)
* Improve pgraph library
* Add indegree, outdegree, and topological sort with tests
* Add reverse function for vertex list
* Tons of additional cleanup!

Amazingly, on my first successful compile, this seemed to run!

A special thanks to Ira Cooper who helped me talk through some of the
algorithmic decisions and for his help in finding better ones!
This commit is contained in:
James Shubin
2015-12-10 03:34:51 -05:00
parent 0ea6f30ef2
commit 6b4fa21074
20 changed files with 1411 additions and 363 deletions

View File

@@ -20,7 +20,6 @@
package main
import (
"code.google.com/p/go-uuid/uuid"
"fmt"
systemd "github.com/coreos/go-systemd/dbus" // change namespace
"github.com/coreos/go-systemd/util"
@@ -29,29 +28,27 @@ import (
)
type ServiceType struct {
uuid string
Type string // always "service"
Name string // name variable
Events chan string // FIXME: eventually a struct for the event?
State string // state: running, stopped
Startup string // enabled, disabled, undefined
BaseType `yaml:",inline"`
State string `yaml:"state"` // state: running, stopped
Startup string `yaml:"startup"` // enabled, disabled, undefined
}
func NewServiceType(name, state, startup string) *ServiceType {
return &ServiceType{
uuid: uuid.New(),
Type: "service",
Name: name,
Events: make(chan string, 1), // XXX: chan size?
BaseType: BaseType{
Name: name,
events: make(chan Event),
vertex: nil,
},
State: state,
Startup: startup,
}
}
// Service watcher
func (obj ServiceType) Watch(v *Vertex) {
func (obj *ServiceType) Watch() {
// obj.Name: service name
//vertex := obj.GetVertex() // stored with SetVertex
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
}
@@ -119,12 +116,11 @@ func (obj ServiceType) Watch(v *Vertex) {
// loop so that we can see the changed invalid signal
log.Printf("Service[%v]->DaemonReload()\n", service)
case exit := <-obj.Events:
if exit == "exit" {
return
} else {
log.Fatal("Unknown event: %v\n", exit)
case event := <-obj.events:
if ok := obj.ReadEvent(&event); !ok {
return // exit
}
send = true
}
} else {
if !activeSet {
@@ -156,31 +152,25 @@ func (obj ServiceType) Watch(v *Vertex) {
case err := <-subErrors:
log.Println("error:", err)
log.Fatal(err)
v.Events <- fmt.Sprintf("service: %v", "error")
//vertex.events <- fmt.Sprintf("service: %v", "error") // XXX: how should we handle errors?
case exit := <-obj.Events:
if exit == "exit" {
return
} else {
log.Fatal("Unknown event: %v\n", exit)
case event := <-obj.events:
if ok := obj.ReadEvent(&event); !ok {
return // exit
}
send = true
}
}
if send {
send = false
//log.Println("Sending event!")
v.Events <- fmt.Sprintf("service(%v): %v", obj.Name, "event!") // FIXME: use struct
obj.Process(obj) // XXX: rename this function
}
}
}
func (obj ServiceType) Exit() bool {
obj.Events <- "exit"
return true
}
func (obj ServiceType) StateOK() bool {
func (obj *ServiceType) StateOK() bool {
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
@@ -232,8 +222,8 @@ func (obj ServiceType) StateOK() bool {
return true // all is good, no state change needed
}
func (obj ServiceType) Apply() bool {
fmt.Printf("Apply->%v[%v]\n", obj.Type, obj.Name)
func (obj *ServiceType) Apply() bool {
fmt.Printf("Apply->Service[%v]\n", obj.Name)
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
@@ -292,3 +282,25 @@ func (obj ServiceType) Apply() bool {
return true
}
func (obj *ServiceType) Compare(typ Type) bool {
switch typ.(type) {
case *ServiceType:
return obj.compare(typ.(*ServiceType))
default:
return false
}
}
func (obj *ServiceType) compare(typ *ServiceType) bool {
if obj.Name != typ.Name {
return false
}
if obj.State != typ.State {
return false
}
if obj.Startup != typ.Startup {
return false
}
return true
}