crawler_manager_go/crawler_manager.go

287 lines
4.3 KiB
Go
Raw Normal View History

2017-04-13 02:53:00 +00:00
package crawler_manager
import (
2017-04-14 02:29:55 +00:00
"loafle.com/overflow/crawler_go/config"
"encoding/json"
"io/ioutil"
2017-04-14 08:23:32 +00:00
"google.golang.org/grpc"
2017-04-15 11:10:33 +00:00
"strconv"
"os/exec"
"time"
"os"
"log"
2017-04-15 11:36:15 +00:00
"path/filepath"
"errors"
2017-04-13 02:53:00 +00:00
)
const (
2017-04-14 02:29:55 +00:00
address = "localhost:"
2017-04-15 11:27:40 +00:00
portArgsName = "-Port="
2017-04-14 08:19:02 +00:00
defaultPort = 50000
2017-04-14 07:23:42 +00:00
rootFolder = "/home/cm/"
2017-04-15 11:10:33 +00:00
ConfigFolder = rootFolder + "/config/" // +container
2017-04-14 07:23:42 +00:00
BinaryFolder = rootFolder + "/container/"
PidFolder = rootFolder + "/pids/"
2017-04-15 11:10:33 +00:00
runFile = "ttnc"
2017-04-13 02:53:00 +00:00
)
2017-04-15 11:10:33 +00:00
var g_CrawlerMananger *CrawlerManager = nil
2017-04-14 07:23:42 +00:00
2017-04-14 08:23:32 +00:00
type CrawlerManager struct {
2017-04-15 11:10:33 +00:00
currentPort int
portMap map[string]string
pidMap map[string]string
2017-04-14 07:23:42 +00:00
}
2017-04-14 02:29:55 +00:00
func init() {
2017-04-13 02:53:00 +00:00
2017-04-15 11:10:33 +00:00
if g_CrawlerMananger == nil {
g_CrawlerMananger = &CrawlerManager{portMap:make(map[string]string),pidMap:make(map[string]string), currentPort:defaultPort}
}
2017-04-14 02:29:55 +00:00
2017-04-17 02:15:28 +00:00
g_CrawlerMananger.init()
2017-04-15 11:10:33 +00:00
}
2017-04-13 02:53:00 +00:00
func GetClient(container string) (*grpc.ClientConn, error) {
crm := GetInstance()
b := crm.checkContainer(&container)
2017-04-14 08:23:32 +00:00
2017-04-15 11:10:33 +00:00
if b == false {
err := crm.runAndInitContainer(&container)
2017-04-15 11:10:33 +00:00
if err != nil {
return nil, err
}
}
gport := crm.portMap[container]
2017-04-14 08:23:32 +00:00
2017-04-15 11:10:33 +00:00
return grpc.Dial(address+gport, grpc.WithInsecure())
2017-04-14 08:23:32 +00:00
}
func GetInstance() *CrawlerManager {
return g_CrawlerMananger;
}
2017-04-14 08:23:32 +00:00
2017-04-15 11:10:33 +00:00
func readConfig(path string ) *config.Config {
2017-04-14 02:29:55 +00:00
bytes, err := ioutil.ReadFile(path)
2017-04-13 02:53:00 +00:00
if err != nil {
2017-04-14 02:29:55 +00:00
return nil
2017-04-13 02:53:00 +00:00
}
2017-04-15 11:10:33 +00:00
cc := config.Config{}
json.Unmarshal(bytes, &cc)
2017-04-13 02:53:00 +00:00
2017-04-15 11:10:33 +00:00
return &cc
}
2017-04-13 03:45:00 +00:00
2017-04-13 02:53:00 +00:00
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)init() {
c.checkPid()
2017-04-13 02:53:00 +00:00
2017-04-15 11:10:33 +00:00
cs := c.isStartContainer()
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
for _, cc := range cs {
err := c.runAndInitContainer(&cc)
if err != nil {
log.Println(err)
continue
}
2017-04-14 07:23:42 +00:00
}
}
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)isStartContainer() []string {
2017-04-14 07:23:42 +00:00
files, _ := ioutil.ReadDir(ConfigFolder)
var cs []string
for _,file := range files {
if file.IsDir() {
2017-04-15 11:10:33 +00:00
b := existConfigFile(ConfigFolder, file.Name())
2017-04-14 07:23:42 +00:00
if b {
cs = append(cs, file.Name())
}
}
}
return cs
}
2017-04-15 11:16:14 +00:00
func (c *CrawlerManager)checkContainer(container *string) bool {
2017-04-15 11:10:33 +00:00
return callStatus(container)
}
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)checkPid() {
files, err := ioutil.ReadDir(PidFolder)
if err != nil {
log.Println(err)
return
}
for _, file := range files {
2017-04-14 07:23:42 +00:00
if file.IsDir() {
2017-04-15 11:10:33 +00:00
continue
2017-04-14 07:23:42 +00:00
}
2017-04-15 11:10:33 +00:00
str := file.Name()
c.stopProcess(&str)
c.removeProcessFile(&str)
2017-04-13 02:53:00 +00:00
}
2017-04-15 11:10:33 +00:00
2017-04-14 07:23:42 +00:00
}
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)runAndInitContainer(container *string) error {
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
err := c.runContainer(container)
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
if err != nil {
return err
}
dirs := getConfigPaths(container)
if dirs == nil {
return errors.New("not found config")
}
b := callInit(container, dirs)
if b == false {
return errors.New("call init failed")
}
return nil
}
func (c *CrawlerManager)runContainer(container *string) error {
2017-04-15 11:16:14 +00:00
b := c.checkContainer(container)
if b {
return nil
}
2017-04-15 11:10:33 +00:00
cmdStr := getRunCommand(container)
for {
2017-04-15 11:27:40 +00:00
pArg := portArgsName + strconv.Itoa(c.currentPort)
2017-04-15 11:10:33 +00:00
cmd := exec.Command(cmdStr, pArg)
err := cmd.Start()
if err != nil {
//run error break;
log.Println(err)
return err
}
time.Sleep(time.Duration( time.Second * 2))
b := callStatusAddress(address + strconv.Itoa(c.currentPort))
if b == false {
c.currentPort++
continue;
2017-04-14 07:23:42 +00:00
}
2017-04-15 11:10:33 +00:00
log.Println(*container + " run success port:" , c.currentPort , "pid:", cmd.Process.Pid )
writePid(cmd.Process.Pid)
c.portMap[*container] = strconv.Itoa(c.currentPort)
c.pidMap[*container] = strconv.Itoa(cmd.Process.Pid)
c.currentPort++
break;
}
return nil
}
2017-04-15 11:16:14 +00:00
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)stopContainerAll() {
for k, _ := range c.pidMap {
c.stopContainer(&k)
}
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
}
func (c *CrawlerManager)stopContainer(container *string) {
pid := c.pidMap[*container]
if len(pid) <= 0 {
return
2017-04-14 07:23:42 +00:00
}
2017-04-15 11:10:33 +00:00
c.stopProcess(&pid)
c.removeProcessFile(&pid)
delete(c.pidMap, *container)
delete(c.portMap, *container)
2017-04-13 02:53:00 +00:00
2017-04-14 02:29:55 +00:00
}
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
func (c *CrawlerManager)stopProcess(pid *string) {
pidi, err := strconv.Atoi(*pid)
if err != nil {
log.Println(err)
}
p, err := os.FindProcess(pidi)
if err != nil {
log.Println(err)
}
p.Kill()
}
func (c *CrawlerManager)removeProcessFile(pid *string) {
err := os.Remove(PidFolder + "/" +*pid)
if err != nil {
log.Println(err)
}
}
2017-04-15 11:36:15 +00:00
func (c *CrawlerManager)activeCrawler(container *string) []string {
2017-04-15 11:10:33 +00:00
2017-04-15 11:36:15 +00:00
var dirs []string
existConfigFileDir(ConfigFolder,*container,&dirs)
if len(dirs) <= 0 {
return nil
}
var r []string
for _, path := range dirs {
r = append(r, filepath.Base(path))
}
return r
}
2017-04-15 11:10:33 +00:00