137 lines
2.3 KiB
Go
137 lines
2.3 KiB
Go
package crawler_manager
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
"time"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
|
|
func RunContainer(container *string, cpm *map[string][]string) {
|
|
cmdStr := getRunCommand(container)
|
|
|
|
for {
|
|
pArg := "-Port=" + strconv.Itoa(currentPort)
|
|
cmd := exec.Command(cmdStr, pArg)
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
time.Sleep(time.Duration( time.Second * 2))
|
|
|
|
b := CallInit2("localhost:" + strconv.Itoa(currentPort), cpm)
|
|
log.Println("current Port:" , currentPort)
|
|
if b == false {
|
|
currentPort++
|
|
continue;
|
|
}
|
|
log.Println(*container + " run success port:" , currentPort , "pid:", cmd.Process.Pid )
|
|
currentPort++
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func getRunCommand(container *string ) string {
|
|
return BinaryFolder + "/" + *container + "/" + runFile
|
|
}
|
|
|
|
|
|
//FIXME:: process check!!!!!!
|
|
func ExeCrawler(crawlerName string, exePath string) {
|
|
log.Println("Run Crawler")
|
|
|
|
for {
|
|
pArg := "-Port=" + strconv.Itoa(currentPort)
|
|
cmd := exec.Command(exePath, pArg)
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
time.Sleep(time.Duration( time.Second * 2))
|
|
|
|
log.Println("current Pid : " , cmd.Process.Pid)
|
|
|
|
if IsAlive(cmd.Process.Pid) == false || IsState(cmd.Process.Pid) == false {
|
|
log.Println("run fail port:",currentPort)
|
|
currentPort++
|
|
continue
|
|
}
|
|
|
|
portMap[crawlerName] = strconv.Itoa(currentPort)
|
|
log.Println("current port:", currentPort)
|
|
//FIXME::remove
|
|
break
|
|
}
|
|
|
|
|
|
}
|
|
|
|
func IsAlive(pid int) bool {
|
|
if pid < 0 {
|
|
return false
|
|
}
|
|
|
|
ps := exec.Command("ps", "-aux")
|
|
awk := exec.Command("awk", "{print $2}")
|
|
grep := exec.Command("grep", strconv.Itoa(pid))
|
|
|
|
awk.Stdin, _ = ps.StdoutPipe()
|
|
grep.Stdin,_ = awk.StdoutPipe()
|
|
|
|
ps.Start()
|
|
awk.Start()
|
|
|
|
byt, _ := grep.Output()
|
|
|
|
str := strconv.Itoa(pid)
|
|
|
|
if len(str) +1 == len(byt) {
|
|
return true
|
|
}
|
|
return false
|
|
|
|
}
|
|
|
|
func IsState(pid int) bool {
|
|
ps := exec.Command("ps", "-aux")
|
|
awk := exec.Command("awk", "{print $2, $8}")
|
|
grep := exec.Command("grep", strconv.Itoa(pid))
|
|
awk2 := exec.Command("awk", "{print $2}")
|
|
|
|
awk.Stdin, _ = ps.StdoutPipe()
|
|
grep.Stdin,_ = awk.StdoutPipe()
|
|
awk2.Stdin,_ = grep.StdoutPipe()
|
|
|
|
ps.Start()
|
|
awk.Start()
|
|
grep.Start()
|
|
|
|
byt, _ := awk2.Output()
|
|
|
|
if len(byt) <= 0 {
|
|
return false
|
|
}
|
|
|
|
str := string(byt)
|
|
str = strings.TrimSpace(str)
|
|
|
|
log.Println(str)
|
|
idx := strings.Index(str, "Z")
|
|
if idx >= 0 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|