//ResourceOffers will be called by the Mesos framework to provide an array of
//offers to this framework. Is up to you to check the content of the offers
//and to accept or reject them if they don't fit the needs of the framework
ResourceOffers will be called by the Mesos framework to provide an array of
offers to this framework. Is up to you to check the content of the offers
and to accept or reject them if they don't fit the needs of the framework
// ResourceOffers will be called by the Mesos framework to provide an array of // offers to this framework . Is up to you to check the content of the offers // and to accept or reject them if they don ' t fit the needs of the framework
func (s *ExampleScheduler) ResourceOffers(driver scheduler.SchedulerDriver, offers []*mesosproto.Offer) {
	for _, offer := range offers {
		if s.launched {
			driver.DeclineOffer(offer.Id, &mesosproto.Filters{RefuseSeconds: proto.Float64(1)})
			continue
		}
		offeredCpu := 0.0
		offeredMem := 0.0
		var offeredPort []*mesosproto.Value_Range = make([]*mesosproto.Value_Range, 1)
		for _, resource := range offer.Resources {
			switch resource.GetName() {
			case "cpus":
				offeredCpu += resource.GetScalar().GetValue()
			case "mem":
				offeredMem += resource.GetScalar().GetValue()
			case "ports":
				ranges := resource.GetRanges()
				if len(ranges.Range) > 0 {
					firstRange := ranges.Range[0]
					uniquePortRange := mesosproto.Value_Range{
						Begin: firstRange.Begin,
						End:   firstRange.Begin,
					}
					offeredPort[0] = &uniquePortRange
				}
			}
		}
		log.Infof("Received Offer <%v> with cpus=%v mem=%v, port=%v from %s",
			offer.Id.GetValue(),
			offeredCpu,
			offeredMem,
			offeredPort[0].GetBegin(),
			*offer.Hostname)
		if offeredCpu < s.NeededCpu || offeredMem < s.NeededRam || len(offeredPort) == 0 {
			log.Infof("Declining offer <%v>\n", offer.Id.GetValue())
			driver.DeclineOffer(offer.Id, &mesosproto.Filters{RefuseSeconds: proto.Float64(1)})
			continue
		}
		taskId := &mesosproto.TaskID{
			Value: proto.String(uuid.NewV4().String()),
		}
		task := &mesosproto.TaskInfo{
			Name:     proto.String("go-task-" + taskId.GetValue()),
			TaskId:   taskId,
			SlaveId:  offer.SlaveId,
			Executor: s.ExecutorInfo,
			Resources: []*mesosproto.Resource{
				mesosutil.NewScalarResource("cpus", s.NeededCpu),
				mesosutil.NewScalarResource("mem", s.NeededRam),
				mesosutil.NewRangesResource("ports", offeredPort),
			},
			Data: []byte("Hello from Server"),
		}
		log.Infof("Prepared task: %s with offer %s for launch\n", task.GetName(), offer.Id.GetValue())
		var tasks []*mesosproto.TaskInfo
		tasks = append(tasks, task)
		log.Infoln("Launching task for offer", offer.Id.GetValue())
		status, err := driver.LaunchTasks([]*mesosproto.OfferID{offer.Id}, tasks, &mesosproto.Filters{RefuseSeconds: proto.Float64(10)})
		if err != nil {
			log.Fatal(err)
		}
		log.Infof("Launch task status: %v", status)
		s.launched = true
	}
}
func ( s * ExampleScheduler ) ResourceOffers ( driver scheduler . SchedulerDriver , offers [ ] * mesosproto . Offer ) { for _ , offer := range offers { if s . launched { driver . DeclineOffer ( offer . Id , & mesosproto . Filters { RefuseSeconds : proto . Float64 ( 1 ) } ) continue } offeredCpu := 0.0 offeredMem := 0.0 var offeredPort [ ] * mesosproto . Value_Range = make ( [ ] * mesosproto . Value_Range , 1 ) for _ , resource := range offer . Resources { switch resource . GetName ( ) { case " " : offeredCpu += resource . GetScalar ( ) . GetValue ( ) case " " : offeredMem += resource . GetScalar ( ) . GetValue ( ) case " " : ranges := resource . GetRanges ( ) if len ( ranges . Range ) > 0 { firstRange := ranges . Range [ 0 ] uniquePortRange := mesosproto . Value_Range { Begin : firstRange . Begin , End : firstRange . Begin , } offeredPort [ 0 ] = & uniquePortRange } } } log . Infof ( " " , offer . Id . GetValue ( ) , offeredCpu , offeredMem , offeredPort [ 0 ] . GetBegin ( ) , * offer . Hostname ) if offeredCpu < s . NeededCpu || offeredMem < s . NeededRam || len ( offeredPort ) == 0 { log . Infof ( " \n " , offer . Id . GetValue ( ) ) driver . DeclineOffer ( offer . Id , & mesosproto . Filters { RefuseSeconds : proto . Float64 ( 1 ) } ) continue } taskId := & mesosproto . TaskID { Value : proto . String ( uuid . NewV4 ( ) . String ( ) ) , } task := & mesosproto . TaskInfo { Name : proto . String ( " " + taskId . GetValue ( ) ) , TaskId : taskId , SlaveId : offer . SlaveId , Executor : s . ExecutorInfo , Resources : [ ] * mesosproto . Resource { mesosutil . NewScalarResource ( " " , s . NeededCpu ) , mesosutil . NewScalarResource ( " " , s . NeededRam ) , mesosutil . NewRangesResource ( " " , offeredPort ) , } , Data : [ ] byte ( " " ) , } log . Infof ( " \n " , task . GetName ( ) , offer . Id . GetValue ( ) ) var tasks [ ] * mesosproto . TaskInfo tasks = append ( tasks , task ) log . Infoln ( " " , offer . Id . GetValue ( ) ) status , err := driver . LaunchTasks ( [ ] * mesosproto . OfferID { offer . Id } , tasks , & mesosproto . Filters { RefuseSeconds : proto . Float64 ( 10 ) } ) if err != nil { log . Fatal ( err ) } log . Infof ( " " , status ) s . launched = true } }
ResourceOffers will be called by the Mesos framework to provide an array of
offers to this framework.
ResourceOffers will be called by the Mesos framework to provide an array of offers to this framework .
// Iterate over resources to find one that fits all our needs. This // usually isn't done this way as you must accept an offer that cover // partially your needs and keep accepting until you fit all of them //Take the first value of the range as we only need one port //Print information about the received offer //Decline offer if the offer doesn't satisfy our needs // At this point we have determined we accept the offer // We have to create a TaskID so we use the go-uuid library to create // a random id. //Provide information about the name of the task, id, the slave will //be run of, the executor (that contains the command to execute as well //as the uri to download the executor or executors from and the amount //of resource the taks will use (not neccesary all from the offer) //Launch the task