Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "current-since" field to services API and CLI #115

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/url"
"strings"
"time"
)

type ServiceOptions struct {
Expand Down Expand Up @@ -80,9 +81,10 @@ type ServicesOptions struct {

// ServiceInfo holds status information for a single service.
type ServiceInfo struct {
Name string `json:"name"`
Startup ServiceStartup `json:"startup"`
Current ServiceStatus `json:"current"`
Name string `json:"name"`
Startup ServiceStartup `json:"startup"`
Current ServiceStatus `json:"current"`
StartTime time.Time `json:"start-time"`
}

// ServiceStartup defines the different startup modes for a service.
Expand Down
13 changes: 10 additions & 3 deletions cmd/pebble/cmd_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

type cmdServices struct {
clientMixin
timeMixin
Positional struct {
Services []string `positional-arg-name:"<service>"`
} `positional-args:"yes"`
Expand Down Expand Up @@ -59,14 +60,20 @@ func (cmd *cmdServices) Execute(args []string) error {
w := tabWriter()
defer w.Flush()

fmt.Fprintln(w, "Service\tStartup\tCurrent")
fmt.Fprintln(w, "Service\tStartup\tCurrent\tStart Time")

for _, svc := range services {
fmt.Fprintf(w, "%s\t%s\t%s\n", svc.Name, svc.Startup, svc.Current)
startTime := "-"
if !svc.StartTime.IsZero() {
startTime = cmd.fmtTime(svc.StartTime)
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", svc.Name, svc.Startup, svc.Current, startTime)
}
return nil
}

func init() {
addCommand("services", shortServicesHelp, longServicesHelp, func() flags.Commander { return &cmdServices{} }, nil, nil)
addCommand("services", shortServicesHelp, longServicesHelp,
func() flags.Commander { return &cmdServices{} },
timeDescs, nil)
}
18 changes: 9 additions & 9 deletions cmd/pebble/cmd_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (s *PebbleSuite) TestServices(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(rest, check.HasLen, 0)
c.Check(s.Stdout(), check.Equals, `
Service Startup Current
svc1 enabled inactive
svc2 enabled inactive
svc3 enabled backoff
Service Startup Current Start Time
svc1 enabled inactive -
svc2 enabled inactive -
svc3 enabled backoff -
`[1:])
c.Check(s.Stderr(), check.Equals, "")
}
Expand All @@ -60,18 +60,18 @@ func (s *PebbleSuite) TestServicesNames(c *check.C) {
"type": "sync",
"status-code": 200,
"result": [
{"name": "bar", "current": "active", "startup": "disabled"},
{"name": "bar", "current": "active", "startup": "disabled", "start-time": "2022-04-28T17:05:23+12:00"},
{"name": "foo", "current": "inactive", "startup": "enabled"}
]
}`)
})
rest, err := pebble.Parser(pebble.Client()).ParseArgs([]string{"services", "foo", "bar"})
rest, err := pebble.Parser(pebble.Client()).ParseArgs([]string{"services", "foo", "bar", "--abs-time"})
c.Assert(err, check.IsNil)
c.Assert(rest, check.HasLen, 0)
c.Check(s.Stdout(), check.Equals, `
Service Startup Current
bar disabled active
foo enabled inactive
Service Startup Current Start Time
bar disabled active 2022-04-28T17:05:23+12:00
foo enabled inactive -
`[1:])
c.Check(s.Stderr(), check.Equals, "")
}
11 changes: 8 additions & 3 deletions internal/daemon/api_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"net/http"
"sort"
"strings"
"time"

"github.com/canonical/pebble/internal/overlord/servstate"
"github.com/canonical/pebble/internal/overlord/state"
"github.com/canonical/pebble/internal/strutil"
)

type serviceInfo struct {
Name string `json:"name"`
Startup string `json:"startup"`
Current string `json:"current"`
Name string `json:"name"`
Startup string `json:"startup"`
Current string `json:"current"`
StartTime *time.Time `json:"start-time,omitempty"` // pointer as omitempty doesn't work with time.Time directly
}

func v1GetServices(c *Command, r *http.Request, _ *userState) Response {
Expand All @@ -48,6 +50,9 @@ func v1GetServices(c *Command, r *http.Request, _ *userState) Response {
Startup: string(svc.Startup),
Current: string(svc.Current),
}
if !svc.StartTime.IsZero() {
info.StartTime = &svc.StartTime
}
infos = append(infos, info)
}
return SyncResponse(infos)
Expand Down
5 changes: 3 additions & 2 deletions internal/overlord/servstate/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type serviceData struct {
backoffTime time.Duration
resetTimer *time.Timer
restarting bool
restarts int
startTime time.Time
}

func (m *ServiceManager) doStart(task *state.Task, tomb *tomb.Tomb) error {
Expand Down Expand Up @@ -366,6 +366,7 @@ func (s *serviceData) startInternal() error {
return fmt.Errorf("cannot start service: %w", err)
}
logger.Debugf("Service %q started with PID %d", serviceName, s.cmd.Process.Pid)
s.startTime = time.Now()
s.resetTimer = time.AfterFunc(s.config.BackoffLimit.Value, func() { logError(s.backoffResetElapsed()) })

// Start a goroutine to wait for the process to finish.
Expand Down Expand Up @@ -424,6 +425,7 @@ func (s *serviceData) exited(exitCode int) error {
s.manager.servicesLock.Lock()
defer s.manager.servicesLock.Unlock()

s.startTime = time.Time{}
if s.resetTimer != nil {
s.resetTimer.Stop()
}
Expand Down Expand Up @@ -597,7 +599,6 @@ func (s *serviceData) backoffTimeElapsed() error {

switch s.state {
case stateBackoff:
s.restarts++
err := s.startInternal()
if err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions internal/overlord/servstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,11 @@ func (m *ServiceManager) Ensure() error {
}

type ServiceInfo struct {
Name string
Startup ServiceStartup
Current ServiceStatus
Restarts int
Name string
Startup ServiceStartup
Current ServiceStatus
StartTime time.Time
Restarts int
}

type ServiceStartup string
Expand Down Expand Up @@ -300,7 +301,7 @@ func (m *ServiceManager) Services(names []string) ([]*ServiceInfo, error) {
default:
info.Current = StatusError
}
info.Restarts = s.restarts
info.StartTime = s.startTime
}
services = append(services, info)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/overlord/servstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ services:
}

func (s *S) TestServices(c *C) {
started := time.Now()
services, err := s.manager.Services(nil)
c.Assert(err, IsNil)
c.Assert(services, DeepEquals, []*servstate.ServiceInfo{
Expand All @@ -775,6 +776,8 @@ func (s *S) TestServices(c *C) {

services, err = s.manager.Services(nil)
c.Assert(err, IsNil)
c.Assert(services[1].StartTime.After(started) && services[1].StartTime.Before(started.Add(5*time.Second)), Equals, true)
services[1].StartTime = time.Time{}
c.Assert(services, DeepEquals, []*servstate.ServiceInfo{
{Name: "test1", Current: servstate.StatusInactive, Startup: servstate.StartupEnabled},
{Name: "test2", Current: servstate.StatusActive, Startup: servstate.StartupDisabled},
Expand Down