Example #1
0
File: main.go Project: nqn/angstrom
func taskHandler(driver *mesos.ExecutorDriver, taskInfo mesos.TaskInfo) {
	for {
		var request payload.SampleRequest
		err := json.Unmarshal(taskInfo.Data, &request)
		if err != nil {
			log.Println("Could not parse request: " + err.Error())
		} else {
			log.Println(request.Slave)

			samples := sample(request.Slave)
			if samples == nil {
				continue
			}

			// TODO(nnielsen): Do local aggregation so samples can be sent in bulks.
			// TODO(nnielsen): Return type should be (node_count, available, allocated, used).
			body, err := json.Marshal(samples)
			if err != nil {
				continue
			}
			log.Println("Sending framework data...")
			driver.SendFrameworkMessage(string(body))

			// TODO(nnielsen): Add terminal state.
			// driver.SendStatusUpdate(&mesos.TaskStatus{
			// 	TaskId:  taskInfo.TaskId,
			// 	State:   mesos.NewTaskState(mesos.TaskState_TASK_FINISHED),
			// 	Message: proto.String("Angstrom task YYY sampling completed"),
			// })
		}

		time.Sleep(500 * time.Millisecond)
	}

}
Example #2
0
File: main.go Project: ngaut/tyrant
func main() {
	println("executor started")
	log.SetHighlighting(false)
	f, err := os.Create("executor.log")
	if err != nil {
		println(err.Error())
	}

	log.SetOutput(f)
	log.Warning("executor start...")
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	se := &ShellExecutor{pwd: pwd, finish: make(chan string),
		process: make(map[string]*contex)}
	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()
}
Example #3
0
File: main.go Project: ngaut/tyrant
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 := path.Join(pwd, genTyrantFile(taskId, "sh"))
		arg, err := base64.StdEncoding.DecodeString(os.Args[1])
		if err != nil {
			log.Error(err, arg)
		}
		ioutil.WriteFile(fname, arg, 0777)
		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, "")
				//todo: wait for tailf finish
				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
}
Example #4
0
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()
}
Example #5
0
File: main.go Project: nqn/angstrom
func main() {
	driver := mesos.ExecutorDriver{
		Executor: &mesos.Executor{
			Registered: func(
				driver *mesos.ExecutorDriver,
				executor mesos.ExecutorInfo,
				framework mesos.FrameworkInfo,
				slave mesos.SlaveInfo) {
				fmt.Println("Angstrom executor registered!")
			},

			LaunchTask: func(driver *mesos.ExecutorDriver, taskInfo mesos.TaskInfo) {
				fmt.Println("Launch sample task!")

				// TODO(nnielsen): Launched tasks corresponds to resource samples in round robin fashion.
				driver.SendStatusUpdate(&mesos.TaskStatus{
					TaskId:  taskInfo.TaskId,
					State:   mesos.NewTaskState(mesos.TaskState_TASK_RUNNING),
					Message: proto.String("Angstrom task " + *taskInfo.TaskId.Value + " is sampling slave XXX"),
				})

				go taskHandler(driver, taskInfo)
			},
		},
	}

	driver.Init()
	defer driver.Destroy()

	driver.Run()
	driver.Join()
}