Skip to content

Commit

Permalink
Purge build dir after 10mins
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 24, 2023
1 parent ff8c39b commit d4f69cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
21 changes: 19 additions & 2 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func (task *BuildTask) Build() (esm *ESMBuild, err error) {
task.Deprecated = p.Deprecated
}

versionName := task.Pkg.VersionName()
if task.wd == "" {
task.wd = path.Join(cfg.WorkDir, fmt.Sprintf("npm/%s", task.Pkg.VersionName()))
ensureDir(task.wd)
task.wd = path.Join(cfg.WorkDir, fmt.Sprintf("npm/%s", versionName))

err = ensureDir(task.wd)
if err != nil {
return
}
Expand All @@ -76,6 +77,22 @@ func (task *BuildTask) Build() (esm *ESMBuild, err error) {
}
}

purgeDelay := 10 * time.Minute
v, loaded := purgeTimers.LoadAndDelete(versionName)
if loaded {
if t, ok := v.(*time.Timer); ok {
t.Stop()
}
}
defer func() {
wd := task.wd
purgeTimers.Store(versionName, time.AfterFunc(purgeDelay, func() {
purgeTimers.Delete(versionName)
log.Debugf("Purging %s...", versionName)
os.RemoveAll(wd)
}))
}()

task.stage = "install"
err = installPackage(task.wd, task.Pkg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/node_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func startNodeServices() (err error) {
}

// install services
cmd := exec.Command("pnpm", "add", "esm-node-services")
cmd := exec.Command("pnpm", "add", "esm-node-services@0.7.14")
cmd.Dir = wd
var output []byte
output, err = cmd.CombinedOutput()
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
embedFS EmbedFS
fetchLock sync.Map
installLock sync.Map
purgeTimers sync.Map
)

type EmbedFS interface {
Expand Down
19 changes: 14 additions & 5 deletions server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ func getHandler() rex.Handle {
return rex.Content("index.html", startTime, bytes.NewReader(html))

case "/status.json":
buildQueue.lock.RLock()
q := make([]map[string]interface{}, buildQueue.list.Len())
i := 0
buildQueue.lock.RLock()
for el := buildQueue.list.Front(); el != nil; el = el.Next() {
t, ok := el.Value.(*queueTask)
if ok {
Expand All @@ -236,6 +236,13 @@ func getHandler() rex.Handle {
}
}
buildQueue.lock.RUnlock()

n := 0
purgeTimers.Range(func(key, value interface{}) bool {
n++
return true
})

res, err := fetch(fmt.Sprintf("http://localhost:%d", cfg.NsPort))
if err != nil {
kill(nsPidFile)
Expand All @@ -246,12 +253,14 @@ func getHandler() rex.Handle {
if err != nil {
return err
}

ctx.SetHeader("Cache-Control", "private, no-store, no-cache, must-revalidate")
return map[string]interface{}{
"buildQueue": q[:i],
"ns": string(out),
"version": VERSION,
"uptime": time.Since(startTime).String(),
"buildQueue": q[:i],
"purgeTimers": n,
"ns": string(out),
"version": VERSION,
"uptime": time.Since(startTime).String(),
}

case "/build-target":
Expand Down

0 comments on commit d4f69cd

Please sign in to comment.