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