github.com/uber/kraken@v0.1.4/lib/persistedretry/tagreplication/task.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package tagreplication 15 16 import ( 17 "fmt" 18 "time" 19 20 "github.com/uber/kraken/core" 21 ) 22 23 // Task contains information to replicate a tag and its dependencies to a 24 // remote destination. 25 type Task struct { 26 Tag string `db:"tag"` 27 Digest core.Digest `db:"digest"` 28 Dependencies core.DigestList `db:"dependencies"` 29 Destination string `db:"destination"` 30 CreatedAt time.Time `db:"created_at"` 31 LastAttempt time.Time `db:"last_attempt"` 32 Failures int `db:"failures"` 33 Delay time.Duration `db:"delay"` 34 } 35 36 // NewTask creates a new Task. 37 func NewTask( 38 tag string, 39 d core.Digest, 40 dependencies core.DigestList, 41 destination string, 42 delay time.Duration) *Task { 43 44 return &Task{ 45 Tag: tag, 46 Digest: d, 47 Dependencies: dependencies, 48 Destination: destination, 49 CreatedAt: time.Now(), 50 Delay: delay, 51 } 52 } 53 54 func (t *Task) String() string { 55 return fmt.Sprintf("tagreplication.Task(tag=%s, dest=%s)", t.Tag, t.Destination) 56 } 57 58 // GetLastAttempt returns when t was last attempted. 59 func (t *Task) GetLastAttempt() time.Time { 60 return t.LastAttempt 61 } 62 63 // GetFailures returns the number of times t has failed. 64 func (t *Task) GetFailures() int { 65 return t.Failures 66 } 67 68 // Ready returns whether t is ready to run. 69 func (t *Task) Ready() bool { 70 return time.Since(t.CreatedAt) >= t.Delay 71 } 72 73 // Tags returns the replication destination. 74 func (t *Task) Tags() map[string]string { 75 return map[string]string{ 76 "dest": t.Destination, 77 } 78 }