Skip to content

Commit

Permalink
Integrate restart policy into the container object
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Apr 25, 2024
1 parent 52e85d5 commit 27ce67b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions agent/api/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,23 @@ type Container struct {
ContainerPortSet map[int]struct{}
// ContainerPortRangeMap is a map of containerPortRange to its associated hostPortRange
ContainerPortRangeMap map[string]string

RestartPolicy RestartPolicy `json:"restartPolicy,omitempty"`
RestartCount int `json:"restartCount,omitempty"`
LastRestartAt time.Time `json:"lastRestartAt,omitempty"`
}

type DependsOn struct {
ContainerName string `json:"containerName"`
Condition string `json:"condition"`
}

type RestartPolicy struct {
Enabled bool `json:"enabled"`
IgnoredExitCodes []int `json:"ignoredExitCodes"`
RestartAttemptPeriod time.Duration `json:"restartAttemptPeriod"`
}

// DockerContainer is a mapping between containers-as-docker-knows-them and
// containers-as-we-know-them.
// This is primarily used in DockerState, but lives here such that tasks and
Expand Down Expand Up @@ -632,6 +642,35 @@ func (c *Container) AWSLogAuthExecutionRole() bool {
return c.LogsAuthStrategy == awslogsAuthExecutionRole
}

// RestartPolicyEnabled returns whether the restart policy is enabled
func (c *Container) RestartPolicyEnabled() bool {
c.lock.RLock()
defer c.lock.RUnlock()
return c.RestartPolicy.Enabled
}

// GetRestartCount returns the number of times the container has been restarted
func (c *Container) GetRestartCount() int {
c.lock.RLock()
defer c.lock.RUnlock()
return c.RestartCount
}

// GetLastRestartAt returns the timestamp of the most recent restart
func (c *Container) GetLastRestartAt() time.Time {
c.lock.RLock()
defer c.lock.RUnlock()
return c.LastRestartAt
}

// RecordRestart increments the restart counter and sets LastRestartAt
func (c *Container) RecordRestart() {
c.lock.Lock()
defer c.lock.Unlock()
c.RestartCount++
c.LastRestartAt = time.Now()
}

// SetCreatedAt sets the timestamp for container's creation time
func (c *Container) SetCreatedAt(createdAt time.Time) {
if createdAt.IsZero() {
Expand Down

0 comments on commit 27ce67b

Please sign in to comment.