func main() { pwd, err := os.Getwd() if err != nil { log.Fatal(err) } se := &ShellExecutor{pwd: pwd, finish: make(chan string), process: make(map[string]*exec.Cmd)} driver := mesos.ExecutorDriver{ Executor: &mesos.Executor{ Registered: se.OnRegister, KillTask: se.OnKillTask, LaunchTask: se.OnLaunchTask, Shutdown: se.OnShutdown, Error: se.OnError, Disconnected: se.OnDisconnected, }, } go se.EventLoop() driver.Init() defer driver.Destroy() driver.Run() }
func (self *ShellExecutor) OnLaunchTask(driver *mesos.ExecutorDriver, taskInfo mesos.TaskInfo) { fmt.Println("Launch task:", taskInfo.TaskId.GetValue()) log.Debug("send running state") self.sendStatusUpdate(taskInfo.TaskId.GetValue(), mesos.TaskState_TASK_RUNNING, "task is running!") log.Debugf("%+v", os.Args) startch := make(chan struct{}, 1) if len(os.Args) == 2 { fname := taskInfo.TaskId.GetValue() ioutil.WriteFile(fname, []byte(os.Args[1]), 0644) cmd := exec.Command("/bin/sh", fname) go func() { defer func() { self.finish <- taskInfo.TaskId.GetValue() log.Debug("send finish state") self.sendStatusUpdate(taskInfo.TaskId.GetValue(), mesos.TaskState_TASK_FINISHED, "Go task is done!") time.Sleep(10 * time.Second) driver.Stop() }() self.lock.Lock() self.process[taskInfo.TaskId.GetValue()] = cmd self.lock.Unlock() startch <- struct{}{} out, err := cmd.Output() if err != nil { log.Error(err.Error()) } else { fmt.Println(string(out)) // log.Debug(string(out)) } }() } else { log.Debug("send finish state") self.sendStatusUpdate(taskInfo.TaskId.GetValue(), mesos.TaskState_TASK_FINISHED, "Go task is done!") time.Sleep(10 * time.Second) driver.Stop() } <-startch }
func main() { driver := mesos.ExecutorDriver{ Executor: &mesos.Executor{ Registered: func( driver *mesos.ExecutorDriver, executor mesos.ExecutorInfo, framework mesos.FrameworkInfo, slave mesos.SlaveInfo) { fmt.Println("Executor registered!") }, LaunchTask: func(driver *mesos.ExecutorDriver, taskInfo mesos.TaskInfo) { fmt.Println("Launch task!") driver.SendStatusUpdate(&mesos.TaskStatus{ TaskId: taskInfo.TaskId, State: mesos.NewTaskState(mesos.TaskState_TASK_RUNNING), Message: proto.String("Go task is running!"), }) driver.SendStatusUpdate(&mesos.TaskStatus{ TaskId: taskInfo.TaskId, State: mesos.NewTaskState(mesos.TaskState_TASK_FINISHED), Message: proto.String("Go task is done!"), }) }, }, } driver.Init() defer driver.Destroy() driver.Run() }
func (self *ShellExecutor) OnLaunchTask(driver *mesos.ExecutorDriver, taskInfo mesos.TaskInfo) { taskId := taskInfo.TaskId.GetValue() fmt.Println("Launch task:", taskId) log.Debug("send running state") self.sendStatusUpdate(taskId, mesos.TaskState_TASK_RUNNING, "") pwd, err := os.Getwd() if err != nil { log.Error(err) } eventFile := path.Join(pwd, genTyrantFile(taskId, "event")) touch(eventFile) os.Setenv("TyrantStatusFile", eventFile) f := self.tailf(eventFile, taskId) log.Debugf("%+v", os.Args) startch := make(chan struct{}, 1) if len(os.Args) == 2 { fname := genTyrantFile(taskId, "sh") arg, err := base64.StdEncoding.DecodeString(os.Args[1]) if err != nil { log.Error(err, arg) } ioutil.WriteFile(fname, arg, 0644) cmd := exec.Command("/bin/sh", fname) go func(taskId string) { var err error defer func() { s := mesos.TaskState_TASK_FINISHED if err != nil { s = mesos.TaskState_TASK_FAILED } self.finish <- taskId log.Debug("send taskend state") self.sendStatusUpdate(taskId, s, "") f.Stop() time.Sleep(3 * time.Second) driver.Stop() }() self.lock.Lock() self.process[taskId] = &contex{cmd: cmd, statusFile: f} self.lock.Unlock() startch <- struct{}{} cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout err = cmd.Start() if err != nil { log.Warning(err) return } log.Debug("pid", cmd.Process.Pid) err = cmd.Wait() if err != nil { log.Warning(err) return } }(taskId) } else { log.Debug("argc", len(os.Args), os.Args) log.Debug("send finish state") self.sendStatusUpdate(taskId, mesos.TaskState_TASK_FINISHED, "") time.Sleep(10 * time.Second) driver.Stop() } <-startch }