1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package utils
- import (
- "os"
- "os/exec"
- "strings"
- "sync"
- )
- type RunTask interface {
- AddTask()
- RunTask()
- }
- // T: Task任务
- type T struct {
- sync.Mutex
- // ch: 获取时间channel
- ch chan struct{}
- // 记录pid 用于kill
- pid int
- // 执行shell命令
- exec.Cmd
- }
- // NewT: 实例化方法
- func NewT(path string, args []string, environment ...[]string) *T {
- env := os.Environ()
- if len(environment) > 0 {
- for k, v := range environment[0] {
- env[k] = v
- }
- }
- t := &T{
- Mutex: sync.Mutex{},
- ch: make(chan struct{}),
- Cmd: exec.Cmd{
- Path: path,
- Args: []string{path},
- Env: env,
- Stdin: os.Stdin,
- Stdout: os.Stdout,
- Stderr: os.Stderr,
- ExtraFiles: make([]*os.File, 0),
- },
- pid: os.Getpid(),
- }
- t.Dir, _ = os.Getwd()
- if len(args) > 0 {
- // Exclude of current binary path.
- start := 0
- if strings.EqualFold(path, args[0]) {
- start = 1
- }
- t.Args = append(t.Args, args[start:]...)
- }
- return t
- }
- func (t *T) AddTask() {
- t.Lock()
- defer t.Unlock()
- if len(t.ch) == 1 {
- // 代表已经有任务了
- // 直接丢弃这次任务
- return
- }
- t.ch <- struct{}{}
- }
- func (t *T) RunTask() {
- for {
- _, ok := <-t.ch
- if !ok {
- return
- }
- // todo 执行任务
- }
- }
|