-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yevhenii Voevodin
committed
Dec 5, 2016
1 parent
dca9b2a
commit b88605c
Showing
5 changed files
with
128 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package process | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
type Cleaner struct { | ||
CleanupPeriod time.Duration | ||
CleanupThreshold time.Duration | ||
} | ||
|
||
func NewCleaner(period int, threshold int) *Cleaner { | ||
return &Cleaner{ | ||
time.Duration(period) * time.Minute, | ||
time.Duration(threshold) * time.Minute, | ||
} | ||
} | ||
|
||
func (c *Cleaner) CleanPeriodically() { | ||
ticker := time.NewTicker(c.CleanupPeriod) | ||
defer ticker.Stop() | ||
for range ticker.C { | ||
c.CleanOnce() | ||
} | ||
} | ||
|
||
func (pc *Cleaner) CleanOnce() { | ||
deadPoint := time.Now().Add(-pc.CleanupThreshold) | ||
processes.Lock() | ||
for _, mp := range processes.items { | ||
mp.lastUsedLock.RLock() | ||
if !mp.Alive && mp.lastUsed.Before(deadPoint) { | ||
delete(processes.items, mp.Pid) | ||
if err := os.Remove(mp.logfileName); err != nil { | ||
if !os.IsNotExist(err) { | ||
log.Printf("Couldn't remove process logs file, '%s'", mp.logfileName) | ||
} | ||
} | ||
} | ||
mp.lastUsedLock.RUnlock() | ||
} | ||
processes.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package process_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/eclipse/che/exec-agent/process" | ||
"time" | ||
) | ||
|
||
func TestCleanWithZeroThreshold(t *testing.T) { | ||
p := startAndWaitTestProcess(testCmd, t) | ||
defer cleanupLogsDir() | ||
|
||
process.NewCleaner(0, 0).CleanOnce() | ||
|
||
_, err := process.Get(p.Pid) | ||
if err == nil { | ||
t.Fatal("Must not exist") | ||
} | ||
if _, ok := err.(*process.NoProcessError); !ok { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCleansOnlyUnusedProcesses(t *testing.T) { | ||
p1 := startAndWaitTestProcess(testCmd, t) | ||
p2 := startAndWaitTestProcess(testCmd, t) | ||
|
||
time.Sleep(500 * time.Millisecond) | ||
|
||
// use one of the processes, so it is used now | ||
process.Get(p1.Pid) | ||
|
||
// cleanup immediately | ||
(&process.Cleaner{CleanupPeriod: 0, CleanupThreshold: 500 * time.Millisecond}).CleanOnce() | ||
|
||
_, err1 := process.Get(p1.Pid) | ||
_, err2 := process.Get(p2.Pid) | ||
|
||
// process 1 must be cleaned | ||
if err1 != nil { | ||
t.Fatalf("Expected process 2 to exist, but got an error: %s", err1.Error()) | ||
} | ||
|
||
// process 2 must exist | ||
if _, ok := err2.(*process.NoProcessError); !ok { | ||
t.Fatal("Expected process 2 to be cleaned") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters