github.com/uber/kraken@v0.1.4/lib/persistedretry/writeback/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 writeback
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	"github.com/uber/kraken/core"
    21  )
    22  
    23  // Task contains information to write back a blob to remote storage.
    24  type Task struct {
    25  	Namespace   string        `db:"namespace"`
    26  	Name        string        `db:"name"`
    27  	CreatedAt   time.Time     `db:"created_at"`
    28  	LastAttempt time.Time     `db:"last_attempt"`
    29  	Failures    int           `db:"failures"`
    30  	Delay       time.Duration `db:"delay"`
    31  
    32  	// Deprecated. Use name instead.
    33  	Digest core.Digest `db:"digest"`
    34  }
    35  
    36  // NewTask creates a new Task.
    37  func NewTask(namespace, name string, delay time.Duration) *Task {
    38  	return &Task{
    39  		Namespace: namespace,
    40  		Name:      name,
    41  		CreatedAt: time.Now(),
    42  		Delay:     delay,
    43  	}
    44  }
    45  
    46  func (t *Task) String() string {
    47  	return fmt.Sprintf("writeback.Task(namespace=%s, name=%s)", t.Namespace, t.Name)
    48  }
    49  
    50  // GetLastAttempt returns when t was last attempted.
    51  func (t *Task) GetLastAttempt() time.Time {
    52  	return t.LastAttempt
    53  }
    54  
    55  // GetFailures returns the number of times t has failed.
    56  func (t *Task) GetFailures() int {
    57  	return t.Failures
    58  }
    59  
    60  // Ready returns whether t is ready to run.
    61  func (t *Task) Ready() bool {
    62  	return time.Since(t.CreatedAt) >= t.Delay
    63  }
    64  
    65  // Tags is unused.
    66  func (t *Task) Tags() map[string]string {
    67  	return nil
    68  }