Commit 851616f6 authored by Ronny Bangsund's avatar Ronny Bangsund

Janitor function implemented properly.

-Janitor launches once per scrap after the loader determines it's not expired yet
-Scrap.delete() is called after the delay
parent 4c97977e
......@@ -33,9 +33,6 @@ func main() {
}
info("Scraps starting up.")
janitorQuit := initJanitor()
defer close(janitorQuit)
loadScraps()
initWeb()
......
......@@ -91,6 +91,7 @@ func loadScrap(hash string) Scrap {
if s.hasExpired() {
return Scrap{}
}
initJanitor(&s)
var content []byte
content, _ = ioutil.ReadFile("./data/" + s.Language + "/" + s.Hash)
s.Content = fmt.Sprintf("%s", content)
......@@ -98,9 +99,9 @@ func loadScrap(hash string) Scrap {
}
func (s *Scrap) delete() {
delete(scraps, s.Hash)
os.Remove("./data/" + s.Hash)
os.Remove("./data/" + s.Language + "/" + s.Hash)
delete(scraps, s.Hash)
}
// Open all index files and load the associated scrap,
......
......@@ -45,19 +45,17 @@ func fexists(path string) bool {
return err == nil
}
func initJanitor() chan bool {
ticker := time.NewTicker(time.Second * 300)
quit := make(chan bool)
func initJanitor(s *Scrap) {
delay := s.Expiry - time.Now().Unix()
ticker := time.NewTicker(time.Duration(delay))
go func() {
for {
select {
case <-ticker.C:
info(" Tick! Tock!")
case <-quit:
ticker.Stop()
s.delete()
return
}
}
}()
return quit
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment