Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ func (a *Agent) unloadUnit(unitName string) {
a.registry.ClearUnitHeartbeat(unitName)
a.cache.dropTargetState(unitName)

a.um.TriggerStop(unitName)
errStop := a.um.TriggerStop(unitName)
if errStop != nil {
log.Warningf("Failed stopping unit(%s): %v", unitName, errStop)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - these messages are a bit misleading - because it's just the trigger, not the actual stopping, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonboulle Right. I'll change it tomorrow. Thanks.

} else {
log.Infof("Stopped unit(%s)", unitName)
}

a.uGen.Unsubscribe(unitName)

a.um.Unload(unitName)
// unit should be unloaded and unit file should be removed, only if the unit
// could be successfully stopped. Otherwise the unit could get into a state
// where the unit cannot be stopped via fleet, because the unit file was
// already removed. See also https://github.com/coreos/fleet/issues/1216.
if errStop == nil {
a.um.Unload(unitName)
}
}

func (a *Agent) startUnit(unitName string) {
Expand Down
10 changes: 8 additions & 2 deletions functional/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@ ExecStart=/usr/bin/sleep 3000
t.Error(err.Error())
}

mgr.TriggerStart(name)
err = mgr.TriggerStart(name)
if err != nil {
t.Error(err.Error())
}

err = waitForUnitState(mgr, name, unit.UnitState{"loaded", "active", "running", "", hash, ""})
if err != nil {
t.Error(err.Error())
}

mgr.TriggerStop(name)
err = mgr.TriggerStop(name)
if err != nil {
t.Error(err.Error())
}

mgr.Unload(name)

Expand Down
18 changes: 10 additions & 8 deletions systemd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,26 @@ func (m *systemdUnitManager) Unload(name string) {

// TriggerStart asynchronously starts the unit identified by the given name.
// This function does not block for the underlying unit to actually start.
func (m *systemdUnitManager) TriggerStart(name string) {
func (m *systemdUnitManager) TriggerStart(name string) error {
jobID, err := m.systemd.StartUnit(name, "replace", nil)
if err == nil {
log.Infof("Triggered systemd unit %s start: job=%d", name, jobID)
} else {
if err != nil {
log.Errorf("Failed to trigger systemd unit %s start: %v", name, err)
return err
}
log.Infof("Triggered systemd unit %s start: job=%d", name, jobID)
return nil
}

// TriggerStop asynchronously starts the unit identified by the given name.
// This function does not block for the underlying unit to actually stop.
func (m *systemdUnitManager) TriggerStop(name string) {
func (m *systemdUnitManager) TriggerStop(name string) error {
jobID, err := m.systemd.StopUnit(name, "replace", nil)
if err == nil {
log.Infof("Triggered systemd unit %s stop: job=%d", name, jobID)
} else {
if err != nil {
log.Errorf("Failed to trigger systemd unit %s stop: %v", name, err)
return err
}
log.Infof("Triggered systemd unit %s stop: job=%d", name, jobID)
return nil
}

// GetUnitState generates a UnitState object representing the
Expand Down
4 changes: 2 additions & 2 deletions unit/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (fum *FakeUnitManager) Unload(name string) {
delete(fum.u, name)
}

func (fum *FakeUnitManager) TriggerStart(string) {}
func (fum *FakeUnitManager) TriggerStop(string) {}
func (fum *FakeUnitManager) TriggerStart(string) error { return nil }
func (fum *FakeUnitManager) TriggerStop(string) error { return nil }

func (fum *FakeUnitManager) Units() ([]string, error) {
fum.RLock()
Expand Down
4 changes: 2 additions & 2 deletions unit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type UnitManager interface {
Unload(string)
ReloadUnitFiles() error

TriggerStart(string)
TriggerStop(string)
TriggerStart(string) error
TriggerStop(string) error

Units() ([]string, error)
GetUnitStates(pkg.Set) (map[string]*UnitState, error)
Expand Down