// dirToUnit takes a Node containing a Job's constituent objects (in child // nodes) and returns a *job.Unit, or any error encountered func (r *EtcdRegistry) dirToUnit(dir *etcd.Node, unitHashLookupFunc func(unit.Hash) *unit.UnitFile) (*job.Unit, error) { objKey := path.Join(dir.Key, "object") var objNode *etcd.Node for _, node := range dir.Nodes { node := node if node.Key == objKey { objNode = node } } if objNode == nil { return nil, nil } u, err := r.getUnitFromObjectNode(objNode, unitHashLookupFunc) if err != nil { return nil, err } if u == nil { return nil, fmt.Errorf("unable to parse Unit in Registry at key %s", objKey) } if tgtstate := dirToTargetState(dir); tgtstate != "" { ts, err := job.ParseJobState(tgtstate) if err != nil { return nil, fmt.Errorf("failed to parse Unit(%s) target-state: %v", u.Name, err) } u.TargetState = ts } return u, nil }
func (r *EtcdRegistry) GetJobTargetState(jobName string) (*job.JobState, error) { key := r.jobTargetStatePath(jobName) resp, err := r.etcd.Get(key, false, false) if err != nil { if err.(*goetcd.EtcdError).ErrorCode != etcd.EcodeNodeExist { log.Errorf("Unable to determine target-state of Job(%s): %v", jobName, err) } return nil, err } return job.ParseJobState(resp.Node.Value), nil }
func (es *EventStream) filterJobTargetStateChanges(resp *etcd.Result) *event.Event { if resp.Action != "set" { return nil } dir, baseName := path.Split(resp.Node.Key) if baseName != "target-state" { return nil } dir = strings.TrimSuffix(dir, "/") jobName := path.Base(dir) ts := job.ParseJobState(resp.Node.Value) if ts == nil { return nil } cs := es.registry.determineJobState(jobName) if *cs == *ts { return nil } var cType string switch *cs { case job.JobStateInactive: cType = "CommandLoadJob" case job.JobStateLoaded: if *ts == job.JobStateInactive { cType = "CommandUnloadJob" } else if *ts == job.JobStateLaunched { cType = "CommandStartJob" } case job.JobStateLaunched: if *ts == job.JobStateLoaded { cType = "CommandStopJob" } else if *ts == job.JobStateInactive { cType = "CommandUnloadJob" } } if cType == "" { return nil } agent, err := es.registry.jobTargetMachine(jobName) if err != nil { log.Errorf("Failed to look up target of Job(%s): %v", jobName, err) return nil } return &event.Event{cType, jobName, agent} }
func (r *EtcdRegistry) jobTargetState(jobName string) (job.JobState, error) { req := etcd.Get{ Key: r.jobTargetStatePath(jobName), } resp, err := r.etcd.Do(&req) if err != nil { if isKeyNotFound(err) { err = nil } return job.JobStateInactive, err } return job.ParseJobState(resp.Node.Value) }
func (es *EventStream) filterJobTargetStateChanges(resp *goetcd.Response) *event.Event { if resp.Action != "set" { return nil } dir, baseName := path.Split(resp.Node.Key) if baseName != "target-state" { return nil } dir = strings.TrimSuffix(dir, "/") jobName := path.Base(dir) ts := job.ParseJobState(resp.Node.Value) if ts == nil { return nil } cs := es.registry.determineJobState(jobName) if *cs == *ts { return nil } var cType string switch *cs { case job.JobStateInactive: cType = "CommandLoadJob" case job.JobStateLoaded: if *ts == job.JobStateInactive { cType = "CommandUnloadJob" } else if *ts == job.JobStateLaunched { cType = "CommandStartJob" } case job.JobStateLaunched: if *ts == job.JobStateLoaded { cType = "CommandStopJob" } else if *ts == job.JobStateInactive { cType = "CommandUnloadJob" } } if cType == "" { return nil } agent, _ := es.registry.GetJobTarget(jobName) return &event.Event{cType, jobName, agent} }
func (rc *RegistryClient) CreateUnit(u *schema.Unit) error { rUnit := job.Unit{ Name: u.Name, Unit: *schema.MapSchemaUnitOptionsToUnitFile(u.Options), TargetState: job.JobStateInactive, } if len(u.DesiredState) > 0 { ts, err := job.ParseJobState(u.DesiredState) if err != nil { return err } rUnit.TargetState = ts } return rc.Registry.CreateUnit(&rUnit) }
func (r *EtcdRegistry) parseJobDir(j *job.Job, dir *etcd.Node) (err error) { var heartbeat string for _, node := range dir.Nodes { switch node.Key { case r.jobTargetStatePath(j.Name): j.TargetState, err = job.ParseJobState(node.Value) if err != nil { return } case r.jobTargetAgentPath(j.Name): j.TargetMachineID = node.Value case r.jobHeartbeatPath(j.Name): heartbeat = node.Value } } j.UnitState = r.getUnitState(j.Name) js := determineJobState(j, heartbeat) j.State = &js return }