// 处理系统信号 // 监听系统信号,重启或停止服务 func trapSignal(server client.Encoder) { sch := make(chan os.Signal, 10) signal.Notify(sch, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT, syscall.SIGHUP, syscall.SIGSTOP, syscall.SIGQUIT) go func(ch <-chan os.Signal) { sig := <-ch server.Shutdown("signal recieved " + sig.String() + ", at: " + time.Now().String()) if sig == syscall.SIGHUP { server.Info("autoencode restart now...") procAttr := new(os.ProcAttr) procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr} procAttr.Dir = os.Getenv("PWD") procAttr.Env = os.Environ() process, err := os.StartProcess(os.Args[0], os.Args, procAttr) if err != nil { server.Info("autoencode restart process failed:" + err.Error()) return } waitMsg, err := process.Wait() if err != nil { server.Info("autoencode restart wait error:" + err.Error()) } server.Info(waitMsg) } else { server.Info("autoencode shutdown now...") } }(sch) }
func (s *Session) Open(t int) *Error { if s.IsOpen() { return nil } s.Type = t s.Args = s.BuildTranscodeCommand() log.Printf("Opening transcoder session: %s", s.Args) // create output directory structure if err := s.createOutputDirectories(); err != nil { return ErrorIO } // create pipe pr, pw, err := os.Pipe() if err != nil { s.setState(TC_FAILED) return ErrorTranscodeFailed } s.Pipe = pw // create logfile logname := s.c.Transcode.Log_path + "/" + s.idstr + ".log" s.LogFile, _ = os.OpenFile(logname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, utils.PERM_FILE) // start transcode process var attr os.ProcAttr attr.Dir = s.c.Transcode.Output_path + "/" + s.idstr attr.Files = []*os.File{pr, s.LogFile, s.LogFile} s.Proc, err = os.StartProcess(s.c.Transcode.Command, strings.Fields(s.Args), &attr) if err != nil { log.Printf("Error starting process: %s", err) s.setState(TC_FAILED) pr.Close() pw.Close() s.LogFile.Close() s.Pipe = nil s.Type = 0 s.Args = "" return ErrorTranscodeFailed } // close read-end of pipe and logfile after successful start pr.Close() s.LogFile.Close() // set timeout for session cleanup s.Timer = time.AfterFunc(time.Duration(s.c.Server.Session_timeout)*time.Second, func() { s.HandleTimeout() }) // set state s.setState(TC_RUNNING) return nil }
// 执行截图 func exec(url, pic, width, height, delay string) (string, error) { if url == "" { return "", errors.New("url is none.") } if width == "" { width = strconv.Itoa(conf.width) } if height == "" { height = strconv.Itoa(conf.height) } if delay == "" { delay = strconv.Itoa(conf.delay) } procAttr := new(os.ProcAttr) procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr} procAttr.Dir = os.Getenv("PWD") procAttr.Env = os.Environ() var args []string args = make([]string, 7) args[0] = PHANTOMJS args[1] = conf.dir + "/" + SNAP_JS args[2] = url args[3] = pic args[4] = delay args[5] = width args[6] = height process, err := os.StartProcess(PHANTOMJS, args, procAttr) if err != nil { if conf.debug == true { log.Println("PhantomJS start failed:" + err.Error()) } return "", err } waitMsg, err := process.Wait() if err != nil { if conf.debug == true { log.Println("PhantomJS start wait error:" + err.Error()) } return "", err } if conf.debug == true { log.Println(waitMsg) } return args[3], nil }
func (t *TranscodeSession) Open() error { if t.IsOpen() { return nil } if err := t.createOutputDirectories(); err != nil { log.Printf("Failed to create output directories: %s", err) return err } // create pipe pr, pw, err := os.Pipe() if err != nil { t.setState(TS_STATE_FAILED) return err } t.Pipe = pw // Start the transcode process attr := os.ProcAttr{} attr.Dir = filepath.Join(t.OutputDir, t.ID) attr.Files = []*os.File{pr} t.OutputFile = filepath.Join(t.OutputDir, t.ID, "index.m3u8") t.Proc, err = os.StartProcess("/usr/local/bin/ffmpeg", strings.Fields(t.buildTranscodeCommand()), &attr) if err != nil { log.Printf("Error starting process: %s", err) t.setState(TS_STATE_FAILED) pr.Close() pw.Close() t.Pipe = nil return err } // close the read-end fo the pipe after successful start pr.Close() // set state t.setState(TS_STATE_RUNNING) return nil }