crawler_manager_go/crawler_manager.go

308 lines
4.8 KiB
Go
Raw Normal View History

2017-04-13 02:53:00 +00:00
package crawler_manager
import (
2017-04-27 09:07:56 +00:00
//"loafle.com/overflow/crawler_go/config"
//"encoding/json"
2017-04-14 02:29:55 +00:00
"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-27 09:07:56 +00:00
"errors"
2017-04-27 09:07:56 +00:00
"loafle.com/overflow/agent_api/config_manager"
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
2017-04-17 04:48:53 +00:00
rootFolder = "/home/cm2/"
2017-04-27 09:07:56 +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-27 09:07:56 +00:00
ConfigMgr config_manager.ConfigManager
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-27 09:07:56 +00:00
GetInstance();
listenEvent()
//g_CrawlerMananger.init()
}
func GetInstance() *CrawlerManager {
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-27 09:07:56 +00:00
return g_CrawlerMananger;
2017-04-15 11:10:33 +00:00
}
2017-04-13 02:53:00 +00:00
2017-04-27 09:07:56 +00:00
2017-04-17 04:33:11 +00:00
func (c *CrawlerManager)GetClient(container string) (*grpc.ClientConn, error) {
b := c.checkContainer(&container)
2017-04-14 08:23:32 +00:00
2017-04-15 11:10:33 +00:00
if b == false {
2017-04-27 09:07:56 +00:00
err := c.runAndInitContainerOne(&container)
2017-04-15 11:10:33 +00:00
if err != nil {
return nil, err
}
}
2017-04-17 04:33:11 +00:00
gport := c.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
}
2017-04-15 11:10:33 +00:00
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-27 09:07:56 +00:00
cmap := c.ConfigMgr.GetCrawlers()
scm := sortContainer(cmap)
2017-04-14 07:23:42 +00:00
2017-04-27 09:07:56 +00:00
for ctn := range scm {
c.runAndInitContainer(&ctn, scm[ctn])
2017-04-14 07:23:42 +00:00
}
}
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-27 11:24:35 +00:00
func (c *CrawlerManager)checkAndRunContainer(container *string) bool {
b := callStatus(container)
if b == false {
err := c.runAndInitContainerOne(container)
if err != nil {
return false
}
}
return true
}
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-27 09:07:56 +00:00
func (c *CrawlerManager)runAndInitContainerOne(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
}
2017-04-27 09:07:56 +00:00
cmap := c.ConfigMgr.GetCrawlers()
scm := sortContainer(cmap)
b := callInitConfig(container, scm[*container])
2017-04-15 11:10:33 +00:00
2017-04-27 09:07:56 +00:00
if b == false {
return errors.New("call init failed")
2017-04-15 11:10:33 +00:00
}
2017-04-27 09:07:56 +00:00
return nil
}
func (c *CrawlerManager)runAndInitContainer(container *string,cl []*config_manager.Config) error {
err := c.runContainer(container)
if err != nil {
return err
}
b := callInitConfig(container, cl)
2017-04-15 11:10:33 +00:00
if b == false {
return errors.New("call init failed")
}
return nil
}
2017-04-27 09:07:56 +00:00
func sortContainer(cm map[string]*config_manager.Config) map[string][]*config_manager.Config {
m := make(map[string][]*config_manager.Config)
var cn string;
for key := range cm {
cn = cm[key].Crawler.Container
m[cn] = append(m[cn], cm[key])
}
return m
}
2017-04-15 11:10:33 +00:00
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-27 11:24:35 +00:00
func (c *CrawlerManager)addSensor(id string) {
conf := c.ConfigMgr.GetCrawlerById(id)
b := c.checkAndRunContainer(&conf.Crawler.Name)
if b == false {
return
}
b = callAdd(&conf.Crawler.Container, conf)
if b {
//FIXME :: Noti ADD_SENSOR_2_END
}
}
func (c *CrawlerManager)removeSensor(id string) {
conf := c.ConfigMgr.GetCrawlerById(id)
callRemove(&conf.Crawler.Name, conf)
//remove and stop
}
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)
}
}