github.com/Cloud-Foundations/Dominator@v0.3.4/imageserver/scanner/expire.go (about)

     1  package scanner
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/image"
     9  )
    10  
    11  func imageIsExpired(img *image.Image) bool {
    12  	if !img.ExpiresAt.IsZero() && img.ExpiresAt.Sub(time.Now()) <= 0 {
    13  		return true
    14  	}
    15  	return false
    16  }
    17  
    18  // This must not be called with the lock held.
    19  func (imdb *ImageDataBase) expireImage(img *image.Image, name string) {
    20  	if img.ExpiresAt.IsZero() {
    21  		return
    22  	}
    23  	duration := img.ExpiresAt.Sub(time.Now())
    24  	if duration > 0 {
    25  		time.AfterFunc(duration, func() { imdb.expireImage(img, name) })
    26  		return
    27  	}
    28  	imdb.Lock()
    29  	defer imdb.Unlock()
    30  	imdb.Logger.Printf("Auto expiring (deleting) image: %s\n", name)
    31  	if err := os.Remove(path.Join(imdb.BaseDirectory, name)); err != nil {
    32  		imdb.Logger.Println(err)
    33  	}
    34  	imdb.deleteImageAndUpdateUnreferencedObjectsList(name)
    35  }
    36  
    37  // This may be called with the lock held.
    38  func (imdb *ImageDataBase) scheduleExpiration(img *image.Image,
    39  	name string) {
    40  	if img.ExpiresAt.IsZero() {
    41  		return
    42  	}
    43  	duration := img.ExpiresAt.Sub(time.Now())
    44  	if duration <= 0 {
    45  		return
    46  	}
    47  	time.AfterFunc(duration, func() { imdb.expireImage(img, name) })
    48  	return
    49  }