Merge pull request #4821 from jimenez/3903-add_event_end_timestamp-feature

Adding timestamp end to events endpoint. Modifying api docs.
This commit is contained in:
O.S. Tezer 2014-04-10 20:54:53 +03:00
commit a521388863
7 changed files with 1350 additions and 28 deletions

View file

@ -1491,7 +1491,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
} }
@ -1500,22 +1501,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

@ -411,7 +411,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

@ -221,8 +221,10 @@ 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)
@ -251,9 +253,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
@ -265,13 +267,23 @@ func (srv *Server) Events(job *engine.Job) engine.Status {
} }
} }
} }
for event := range listener {
err := sendEvent(&event) // If no until, disable timeout
if err != nil && err.Error() == "JSON error" { if until == 0 {
continue timeout.Stop()
} }
if err != nil { for {
return job.Error(err) select {
case event := <-listener:
err := sendEvent(&event)
if err != nil && err.Error() == "JSON error" {
continue
}
if err != nil {
return job.Error(err)
}
case <-timeout.C:
return engine.StatusOK
} }
} }
return engine.StatusOK return engine.StatusOK