1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Adding timestamp end to events endpoint. Modifying api docs.

Docker-DCO-1.1-Signed-off-by: Isabel Jimenez <contact@isabeljimenez.com> (github: jimenez)
This commit is contained in:
Isabel Jimenez 2014-03-23 16:14:40 -07:00
parent a278656330
commit 66dd4ea4e2
7 changed files with 1350 additions and 28 deletions

View file

@ -1484,7 +1484,8 @@ func (cli *DockerCli) CmdCommit(args ...string) error {
func (cli *DockerCli) CmdEvents(args ...string) error { func (cli *DockerCli) CmdEvents(args ...string) error {
cmd := cli.Subcmd("events", "[OPTIONS]", "Get real time events from the server") cmd := cli.Subcmd("events", "[OPTIONS]", "Get real time events from the server")
since := cmd.String([]string{"#since", "-since"}, "", "Show previously created events and then stream.") since := cmd.String([]string{"#since", "-since"}, "", "Show all events created since timestamp")
until := cmd.String([]string{"-until"}, "", "Stream events until this timestamp")
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {
return nil return nil
} }
@ -1493,22 +1494,27 @@ func (cli *DockerCli) CmdEvents(args ...string) error {
cmd.Usage() cmd.Usage()
return nil return nil
} }
var (
v := url.Values{} v = url.Values{}
if *since != "" { loc = time.FixedZone(time.Now().Zone())
loc := time.FixedZone(time.Now().Zone()) )
var setTime = func(key, value string) {
format := "2006-01-02 15:04:05 -0700 MST" format := "2006-01-02 15:04:05 -0700 MST"
if len(*since) < len(format) { if len(value) < len(format) {
format = format[:len(*since)] format = format[:len(value)]
} }
if t, err := time.ParseInLocation(format, value, loc); err == nil {
if t, err := time.ParseInLocation(format, *since, loc); err == nil { v.Set(key, strconv.FormatInt(t.Unix(), 10))
v.Set("since", strconv.FormatInt(t.Unix(), 10))
} else { } else {
v.Set("since", *since) v.Set(key, value)
} }
} }
if *since != "" {
setTime("since", *since)
}
if *until != "" {
setTime("until", *until)
}
if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil { if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil {
return err return err
} }

View file

@ -10,7 +10,7 @@ import (
) )
const ( const (
APIVERSION version.Version = "1.10" APIVERSION version.Version = "1.11"
DEFAULTHTTPHOST = "127.0.0.1" DEFAULTHTTPHOST = "127.0.0.1"
DEFAULTUNIXSOCKET = "/var/run/docker.sock" DEFAULTUNIXSOCKET = "/var/run/docker.sock"
) )

View file

@ -246,6 +246,7 @@ func getEvents(eng *engine.Engine, version version.Version, w http.ResponseWrite
var job = eng.Job("events", r.RemoteAddr) var job = eng.Job("events", r.RemoteAddr)
streamJSON(job, w, true) streamJSON(job, w, true)
job.Setenv("since", r.Form.Get("since")) job.Setenv("since", r.Form.Get("since"))
job.Setenv("until", r.Form.Get("until"))
return job.Run() return job.Run()
} }

View file

@ -28,15 +28,30 @@ Docker Remote API
2. Versions 2. Versions
=========== ===========
The current version of the API is 1.10 The current version of the API is 1.11
Calling /images/<name>/insert is the same as calling Calling /images/<name>/insert is the same as calling
/v1.10/images/<name>/insert /v1.11/images/<name>/insert
You can still call an old version of the api using You can still call an old version of the api using
/v1.0/images/<name>/insert /v1.11/images/<name>/insert
v1.11
*****
Full Documentation
------------------
:doc:`docker_remote_api_v1.11`
What's new
----------
.. http:get:: /events
**New!** You can now use the ``-until`` parameter to close connection after timestamp.
v1.10 v1.10
***** *****

File diff suppressed because it is too large Load diff

View file

@ -490,7 +490,9 @@ For example:
Get real time events from the server Get real time events from the server
--since="": Show previously created events and then stream. --since="": Show all events created since timestamp
(either seconds since epoch, or date string as below)
--until="": Show events created before timestamp
(either seconds since epoch, or date string as below) (either seconds since epoch, or date string as below)
.. _cli_events_example: .. _cli_events_example:

View file

@ -222,6 +222,8 @@ func (srv *Server) Events(job *engine.Job) engine.Status {
var ( var (
from = job.Args[0] from = job.Args[0]
since = job.GetenvInt64("since") since = job.GetenvInt64("since")
until = job.GetenvInt64("until")
timeout = time.NewTimer(time.Unix(until, 0).Sub(time.Now()))
) )
sendEvent := func(event *utils.JSONMessage) error { sendEvent := func(event *utils.JSONMessage) error {
b, err := json.Marshal(event) b, err := json.Marshal(event)
@ -250,9 +252,9 @@ func (srv *Server) Events(job *engine.Job) engine.Status {
srv.Unlock() srv.Unlock()
job.Stdout.Write(nil) // flush job.Stdout.Write(nil) // flush
if since != 0 { if since != 0 {
// If since, send previous events that happened after the timestamp // If since, send previous events that happened after the timestamp and until timestamp
for _, event := range srv.GetEvents() { for _, event := range srv.GetEvents() {
if event.Time >= since { if event.Time >= since && (event.Time <= until || until == 0) {
err := sendEvent(&event) err := sendEvent(&event)
if err != nil && err.Error() == "JSON error" { if err != nil && err.Error() == "JSON error" {
continue continue
@ -264,7 +266,14 @@ func (srv *Server) Events(job *engine.Job) engine.Status {
} }
} }
} }
for event := range listener {
// If no until, disable timeout
if until == 0 {
timeout.Stop()
}
for {
select {
case event := <-listener:
err := sendEvent(&event) err := sendEvent(&event)
if err != nil && err.Error() == "JSON error" { if err != nil && err.Error() == "JSON error" {
continue continue
@ -272,6 +281,9 @@ func (srv *Server) Events(job *engine.Job) engine.Status {
if err != nil { if err != nil {
return job.Error(err) return job.Error(err)
} }
case <-timeout.C:
return engine.StatusOK
}
} }
return engine.StatusOK return engine.StatusOK
} }