forked from ALHP/ALHP.GO
sigterm build processes when exiting
This commit is contained in:
55
main.go
55
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Jguer/go-alpm/v2"
|
"github.com/Jguer/go-alpm/v2"
|
||||||
@@ -51,13 +52,15 @@ type BuildPackage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BuildManager struct {
|
type BuildManager struct {
|
||||||
toBuild chan *BuildPackage
|
toBuild chan *BuildPackage
|
||||||
toParse chan *BuildPackage
|
toParse chan *BuildPackage
|
||||||
toPurge chan *BuildPackage
|
toPurge chan *BuildPackage
|
||||||
toRepoAdd chan *BuildPackage
|
toRepoAdd chan *BuildPackage
|
||||||
exit bool
|
exit bool
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
failedMutex sync.RWMutex
|
failedMutex sync.RWMutex
|
||||||
|
buildProcesses []*os.Process
|
||||||
|
buildProcMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
@@ -229,7 +232,8 @@ func (b *BuildManager) buildWorker(id int) {
|
|||||||
select {
|
select {
|
||||||
case pkg := <-b.toBuild:
|
case pkg := <-b.toBuild:
|
||||||
if b.exit {
|
if b.exit {
|
||||||
continue
|
log.Infof("Worker %d exited...", id)
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
b.wg.Add(1)
|
b.wg.Add(1)
|
||||||
}
|
}
|
||||||
@@ -245,7 +249,27 @@ func (b *BuildManager) buildWorker(id int) {
|
|||||||
cmd := backgroundCmd("sh", "-c",
|
cmd := backgroundCmd("sh", "-c",
|
||||||
"cd "+filepath.Dir(pkg.Pkgbuild)+"&&makechrootpkg -c -D "+conf.Basedir.Makepkg+" -l worker-"+strconv.Itoa(id)+" -r "+conf.Basedir.Chroot+" -- "+
|
"cd "+filepath.Dir(pkg.Pkgbuild)+"&&makechrootpkg -c -D "+conf.Basedir.Makepkg+" -l worker-"+strconv.Itoa(id)+" -r "+conf.Basedir.Chroot+" -- "+
|
||||||
"--config "+filepath.Join(conf.Basedir.Makepkg, fmt.Sprintf("makepkg-%s.conf", pkg.March)))
|
"--config "+filepath.Join(conf.Basedir.Makepkg, fmt.Sprintf("makepkg-%s.conf", pkg.March)))
|
||||||
res, err := cmd.CombinedOutput()
|
var out bytes.Buffer
|
||||||
|
cmd.Stdout = &out
|
||||||
|
cmd.Stderr = &out
|
||||||
|
|
||||||
|
check(cmd.Start())
|
||||||
|
|
||||||
|
b.buildProcMutex.Lock()
|
||||||
|
b.buildProcesses = append(b.buildProcesses, cmd.Process)
|
||||||
|
b.buildProcMutex.Unlock()
|
||||||
|
|
||||||
|
err := cmd.Wait()
|
||||||
|
|
||||||
|
b.buildProcMutex.Lock()
|
||||||
|
for i := range b.buildProcesses {
|
||||||
|
if b.buildProcesses[i].Pid == cmd.Process.Pid {
|
||||||
|
b.buildProcesses = append(b.buildProcesses[:i], b.buildProcesses[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.buildProcMutex.Unlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if b.exit {
|
if b.exit {
|
||||||
gitClean(pkg)
|
gitClean(pkg)
|
||||||
@@ -270,7 +294,7 @@ func (b *BuildManager) buildWorker(id int) {
|
|||||||
b.failedMutex.Unlock()
|
b.failedMutex.Unlock()
|
||||||
|
|
||||||
check(os.MkdirAll(filepath.Join(conf.Basedir.Repo, "logs"), os.ModePerm))
|
check(os.MkdirAll(filepath.Join(conf.Basedir.Repo, "logs"), os.ModePerm))
|
||||||
check(os.WriteFile(filepath.Join(conf.Basedir.Repo, "logs", pkg.Pkgbase+".log"), res, os.ModePerm))
|
check(os.WriteFile(filepath.Join(conf.Basedir.Repo, "logs", pkg.Pkgbase+".log"), out.Bytes(), os.ModePerm))
|
||||||
|
|
||||||
gitClean(pkg)
|
gitClean(pkg)
|
||||||
b.wg.Done()
|
b.wg.Done()
|
||||||
@@ -337,13 +361,13 @@ func (b *BuildManager) parseWorker() {
|
|||||||
cmd := backgroundCmd("sh", "-c", "cd "+filepath.Dir(pkg.Pkgbuild)+"&&"+"makepkg --printsrcinfo")
|
cmd := backgroundCmd("sh", "-c", "cd "+filepath.Dir(pkg.Pkgbuild)+"&&"+"makepkg --printsrcinfo")
|
||||||
res, err := cmd.Output()
|
res, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("Failed generate SRCINFO for %s: %s", pkg.Pkgbase, err)
|
log.Warningf("Failed generate SRCINFO for %s: %v", pkg.Pkgbase, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := srcinfo.Parse(string(res))
|
info, err := srcinfo.Parse(string(res))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("Failed to parse SRCINFO for %s: %s", pkg.Pkgbase, err)
|
log.Warningf("Failed to parse SRCINFO for %s: %v", pkg.Pkgbase, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pkg.Srcinfo = info
|
pkg.Srcinfo = info
|
||||||
@@ -489,7 +513,7 @@ func (b *BuildManager) repoWorker() {
|
|||||||
res, err := cmd.CombinedOutput()
|
res, err := cmd.CombinedOutput()
|
||||||
log.Debug(string(res))
|
log.Debug(string(res))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("%s while repo-add: %s", err, string(res))
|
log.Panicf("%v while repo-add: %s", err, string(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = backgroundCmd("paccache",
|
cmd = backgroundCmd("paccache",
|
||||||
@@ -649,5 +673,10 @@ func main() {
|
|||||||
<-killSignals
|
<-killSignals
|
||||||
|
|
||||||
buildManager.exit = true
|
buildManager.exit = true
|
||||||
|
|
||||||
|
for _, p := range buildManager.buildProcesses {
|
||||||
|
check(p.Signal(syscall.SIGTERM))
|
||||||
|
}
|
||||||
|
|
||||||
buildManager.wg.Wait()
|
buildManager.wg.Wait()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user