Skip to content
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
6 changes: 3 additions & 3 deletions docs/lifecycle-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ the `docker run` command line:
The timeout for all lifecycle commands is 60 seconds. After that, a timeout will
occur, forcing Watchtower to continue the update loop.

#### Pre-update timeouts
#### Pre- or Post-update timeouts

For the `pre-update` lifecycle command, it is possible to override this timeout to
For the `pre-update` or `post-update` lifecycle command, it is possible to override this timeout to
allow the script to finish before forcefully killing it. This is done by adding the
label `com.centurylinklabs.watchtower.lifecycle.pre-update-timeout` followed by
label `com.centurylinklabs.watchtower.lifecycle.pre-update-timeout` or post-update-timeout respectively followed by
the timeout expressed in minutes.

If the label value is explicitly set to `0`, the timeout will be disabled.
Expand Down
21 changes: 21 additions & 0 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ func (c Container) PreUpdateTimeout() int {
return minutes
}

// PostUpdateTimeout checks whether a container has a specific timeout set
// for how long the post-update command is allowed to run. This value is expressed
// either as an integer, in minutes, or as 0 which will allow the command/script
// to run indefinitely. Users should be cautious with the 0 option, as that
// could result in watchtower waiting forever.
func (c Container) PostUpdateTimeout() int {
var minutes int
var err error

val := c.getLabelValueOrEmpty(postUpdateTimeoutLabel)

minutes, err = strconv.Atoi(val)
if err != nil || val == "" {
return 1
}

return minutes
}



// StopSignal returns the custom stop signal (if any) that is encoded in the
// container's metadata. If the container has not specified a custom stop
// signal, the empty string "" is returned.
Expand Down
14 changes: 14 additions & 0 deletions pkg/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ var _ = Describe("the container", func() {
})
})
})

When("there is a pre or post update timeout", func() {
It("should return minute values", func() {
c = mockContainerWithLabels(map[string]string{
"com.centurylinklabs.watchtower.lifecycle.pre-update-timeout": "3",
"com.centurylinklabs.watchtower.lifecycle.post-update-timeout": "5",
})
preTimeout := c.PreUpdateTimeout()
Expect(preTimeout).To(Equal(3))
postTimeout := c.PostUpdateTimeout()
Expect(postTimeout).To(Equal(5))
})
})

})
})

Expand Down
1 change: 1 addition & 0 deletions pkg/container/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
preUpdateLabel = "com.centurylinklabs.watchtower.lifecycle.pre-update"
postUpdateLabel = "com.centurylinklabs.watchtower.lifecycle.post-update"
preUpdateTimeoutLabel = "com.centurylinklabs.watchtower.lifecycle.pre-update-timeout"
postUpdateTimeoutLabel = "com.centurylinklabs.watchtower.lifecycle.post-update-timeout"
)

// GetLifecyclePreCheckCommand returns the pre-check command set in the container metadata or an empty string
Expand Down
3 changes: 2 additions & 1 deletion pkg/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func ExecutePreUpdateCommand(client container.Client, container container.Contai
// ExecutePostUpdateCommand tries to run the post-update lifecycle hook for a single container.
func ExecutePostUpdateCommand(client container.Client, newContainerID types.ContainerID) {
newContainer, err := client.GetContainer(newContainerID)
timeout := newContainer.PostUpdateTimeout()

if err != nil {
log.WithField("containerID", newContainerID.ShortID()).Error(err)
Expand All @@ -97,7 +98,7 @@ func ExecutePostUpdateCommand(client container.Client, newContainerID types.Cont
}

clog.Debug("Executing post-update command.")
_, err = client.ExecuteCommand(newContainerID, command, 1)
_, err = client.ExecuteCommand(newContainerID, command, timeout)

if err != nil {
clog.Error(err)
Expand Down