Beispiel #1
0
func main() {
	go func() {
		for {

			<-time.Tick(time.Second)
			log.Info(runtime.NumGoroutine())
		}
	}()
	w := watcher.NewWatcher()

	w.SetBuffSize(10)
	w.Listen()

	w.SetHandle(syscall.SIGHUP, func() {
		log.Info("SIGHUP")
	})

	w.SetHandle(syscall.SIGINT, func() {
		log.Info("SIGINT")
		w.Exit(0)
	})

	w.SetHandle(syscall.SIGUSR1, func() {
		log.Info("RESTART")
		w.Exit(1)
	})

	w.SetDefaultHandle(func(_signal os.Signal) {
		log.Info("Default ---", _signal)
		w.ClearDefaultHandle()
	})

	w.GetExitCode()

}
Beispiel #2
0
func (this *Watcher) Switch() {
	log.Info("Switch")

	for {
		signal_value := <-this.signal_channel
		log.Info(signal_value)
		this.Handle(signal_value)
	}
}
Beispiel #3
0
func main() {

	if len(os.Args) < 2 {
		log.ErrorNoLine(`
	Please Input Server Name And Params:
	Absolute path
		`)
		return
	}

	filePath, err := filepath.Abs(os.Args[1])
	if err != nil {
		log.Error(err)
	}

	server := Exec(filePath, os.Args[2:]...)
	err = server.Start()
	if err != nil {
		filePath = os.Args[1]
		server = Exec(filePath, os.Args[2:]...)
		err2 := server.Start()
		if err2 != nil {
			log.Error(err2)
			return
		}
		return
	}

	log.Info("Start Daemon Server Ok!\n", os.Args[1])

	return
}
Beispiel #4
0
func (this *Watcher) Handle(_signal os.Signal) {
	log.Info("Handle")

	this.list_lock.RLock()
	handle, ok := this.handler_list[_signal]
	this.list_lock.RUnlock()

	if ok && handle != nil {
		if this.GetRunModel() == Linear {
			handle()
		} else {
			go handle()
		}
		return
	}

	if this.GetRunModel() == Linear {
		if this.defaultHandler != nil {
			this.defaultHandler(_signal)
		} else {
			DefaultHandle(_signal)
		}
	} else {
		if this.defaultHandler != nil {
			go this.defaultHandler(_signal)
		} else {
			go DefaultHandle(_signal)
		}
	}

}
Beispiel #5
0
func (this *Watcher) SetHandle(_signal os.Signal, handle Handler) {
	log.Info("SetHandle")

	this.list_lock.Lock()
	defer this.list_lock.Unlock()

	this.handler_list[_signal] = handle
}
Beispiel #6
0
func main() {
	if len(os.Args) < 2 {
		log.Info("Please input process id :")
		return
	}

	pid, err := strconv.Atoi(os.Args[1])
	if err != nil {
		log.Error(err)
		return
	}

	watcher.ShutDown(pid)
}
Beispiel #7
0
func (this *Watcher) Listen() {
	log.Info("Listen")
	this.signal_channel = make(chan os.Signal, this.signal_buff_size)

	signal.Notify(this.signal_channel,
		syscall.SIGHUP,  /*终端的挂断或进程死亡*/
		syscall.SIGINT,  /*来自键盘的中断信号*/
		syscall.SIGQUIT, /*来自键盘的离开信号*/
		syscall.SIGILL,  /*非法指令*/
		syscall.SIGABRT, /*来自abort的异常信号*/
		syscall.SIGFPE,  /*浮点例外*/
		syscall.SIGKILL, /*杀死*/
		syscall.SIGSEGV, /*段非法错误(内存引用无效)*/
		syscall.SIGPIPE, /*管道损坏:向一个没有读进程的管道写数据*/
		syscall.SIGALRM, /*来自alarm的计时器到时信号*/
		syscall.SIGTERM, /*终止*/
		syscall.SIGUSR1, /*用户自定义信号1*/
		syscall.SIGUSR2, /*用户自定义信号2*/
		syscall.SIGCHLD, /*子进程停止或终止*/
		syscall.SIGCONT, /*如果停止,继续执行*/
		syscall.SIGSTOP, /*非来自终端的停止信号*/
		syscall.SIGTSTP, /*来自终端的停止信号*/
		syscall.SIGTTIN, /*后台进程读终端*/
		syscall.SIGTTOU, /*后台进程写终端*/

		syscall.SIGBUS,    /*总线错误(内存访问错误)*/
		syscall.SIGPOLL,   /*Pollable事件发生(Sys V),与SIGIO同义*/
		syscall.SIGPROF,   /*统计分布图用计时器到时*/
		syscall.SIGSYS,    /*非法系统调用(SVr4)*/
		syscall.SIGTRAP,   /*跟踪/断点自陷*/
		syscall.SIGURG,    /*socket紧急信号(4.2BSD)*/
		syscall.SIGVTALRM, /*虚拟计时器到时(4.2BSD)*/
		syscall.SIGXCPU,   /*超过CPU时限(4.2BSD)*/
		syscall.SIGXFSZ,   /*超过文件长度限制(4.2BSD)*/

		syscall.SIGIOT,    /*IOT自陷,与SIGABRT同义*/
		syscall.SIGSTKFLT, /*协处理器堆栈错误(不使用)*/
		syscall.SIGIO,     /*描述符上可以进行I/O操作*/
		syscall.SIGTERM,   /*Term*/
		syscall.SIGCLD,    /*与SIGCHLD同义*/
		syscall.SIGPWR,    /*电力故障(System V)*/
		syscall.SIGWINCH,  /*窗口大小改变(4.3BSD, Sun)*/
		syscall.SIGUNUSED, /*未使用信号(will be SIGSYS)*/
	)

	go this.Switch()
}
Beispiel #8
0
func (this *StringBuffer) Write(buff []byte) (int, error) {
	log.Info(*(*string)(unsafe.Pointer(&buff)))
	var relative int

	for i := 0; i < len(buff); i++ {
		if buff[i] == '\n' || buff[i] == '\r' {
			buf := buff[relative:i]
			relative = i

			data := *(*string)(unsafe.Pointer(&buf))
			this.data = append(this.data, data)
			this.length++
		}
	}

	return len(buff), nil
}
Beispiel #9
0
func StartApp(root string, app string) {
	cmd := exec.Command("run", root+app)
	log.Info("Start : ", root+app)

	file, err := os.OpenFile(root+app+".log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
	if err != nil {
		log.Error(err)
		return
	}

	cmd.Stdout = file
	cmd.Stdin = os.Stdin
	cmd.Stderr = file

	err = cmd.Run()
	if err != nil {
		log.Error(err)
		return
	}

	return
}
Beispiel #10
0
func main() {

	if len(os.Args) < 2 {
		log.ErrorNoLine(`
	Please Input Server Name And Params:
	Absolute path
		`)
		return
	}

	filePath, err := filepath.Abs(os.Args[1])
	if err != nil {
		log.Error(err)
		return
	}

	server := exec.Command(filePath, os.Args[2:]...)
	server.Stdout = os.Stdout
	server.Stderr = os.Stderr
	server.Stdin = os.Stdin

	var path string

	if GetRelative(path) >= 0 {
		path = GetPath(os.Args[1])
		server.Dir = path
	}

	err = server.Start()
	if err != nil {
		log.Error(err)
		return
	}

	log.Info("Start Daemon Server Ok!\n", os.Args[1])

	return
}
Beispiel #11
0
func (this *Watcher) Stop() {
	log.Info("Stop")

	signal.Stop(this.signal_channel)
}
Beispiel #12
0
func (this *Watcher) GetExitCode() int {
	log.Info("GetExit")

	return <-this.exit_channel
}
Beispiel #13
0
func (this *Watcher) Exit(code int) {
	log.Info("Exit")

	signal.Stop(this.signal_channel)
	this.exit_channel <- code
}
Beispiel #14
0
func DefaultHandle(_signal os.Signal) {
	log.Info("DefaultHandle")

	log.Info(_signal)
}
Beispiel #15
0
func main() {

	if len(os.Args) < 3 {
		log.ErrorNoLine(`
	-root		:	server root path(can empty)
	-filter		:	process filter(if empty, no effect)
	-server		:	server relative path
	-time		:	search time	
	`)
		return
	}

	runtime.GOMAXPROCS(runtime.NumCPU())

	var root string
	var server string
	var filter string
	var t int

	flag.StringVar(&root, "root", "", "server root path")
	flag.StringVar(&server, "server", "", "server relative path")
	flag.StringVar(&filter, "filter", "", "process filter")
	flag.IntVar(&t, "time", 5, "search time")

	flag.Parse()

	if len(root) > 0 && root[len(root)-1:len(root)] != "/" {
		root += "/"
	}

	serverlist := strings.Split(server, ",")
	if len(serverlist) <= 0 {
		log.ErrorNoLine("Please Input Server Path")
		return
	}

	servers := make([]string, 0)
	for i := 0; i < len(serverlist); i++ {
		if serverlist[i] != "" {
			servers = append(servers, serverlist[i])
		}
	}

	waitgroup := &sync.WaitGroup{}
	buff := NewStringBuffer()
	timer := time.NewTimer(time.Second * time.Duration(t))

	waitgroup.Add(1)

	fl := GetFilter(ParseFilter(filter))

	go func() {
		defer waitgroup.Done()

		for {

			GetApp(buff, fl)
			buff.Msg <- 1

			<-timer.C
			timer.Reset(time.Second * time.Duration(t))
		}
	}()

	go func() {
		for {
			<-buff.Msg

			data, err := buff.Read()
			if err != nil {
				log.ErrorNoLine(err)
				break
			}

			apps := ParseApp(data)

			noStartServer, err := CheckApp(apps, root, servers)
			if err != nil {
				log.ErrorNoLine(err)
				return
			}

			log.Info("Need Start Server:\n", noStartServer)
			StartApps(root, noStartServer)

		}
	}()

	waitgroup.Wait()
	log.InfoNoLine("Exit")
}
Beispiel #16
0
func TestFuzzyFile(t *testing.T) {
	log.Info(FuzzyQuery("./", "utils"))
}
Beispiel #17
0
func init() {
	log.Info("init")
}