func (core *Core) updateTaskByDockerInfo(task *registry.Task, dockerInspectOutput []byte) { // glog.Infof("Docker Inspect: %s", dockerInspectOutput) if len(dockerInspectOutput) == 0 { return } var dockerTasks []*registry.DockerTask err := json.Unmarshal(dockerInspectOutput, &dockerTasks) if err != nil { log.Errorf("UpdateTaskWithDockerInfo error: %v\n", err) return } task.DockerID = dockerTasks[0].DockerID task.DockerName = dockerTasks[0].DockerName[1:] var dockerState *registry.DockerState err = json.Unmarshal(dockerTasks[0].DockerState, &dockerState) if err != nil { log.Errorf("UpdateTaskWithDockerInfo error: %v\n", err) return } task.ProcessID = dockerState.Pid task.LastUpdateTime = time.Now().Unix() }
func SlAMatch(task *registry.Task, offer *mesosproto.Offer) bool { for _, attribute := range offer.GetAttributes() { if attribute.GetName() == "SLATasks" { items := attribute.GetSet().GetItem() for _, item := range items { if item == task.Parse() { return false } } return true } } return true }
// BuildBasicResources build basic resources // including cpus, mem, disk, ports func BuildBasicResources(task *registry.Task) { if task.Build { return } if task.Cpus > 0 { task.Resources = append(task.Resources, &mesosproto.Resource{ Name: proto.String("cpus"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Cpus)}, }) } if task.Mem > 0 { task.Resources = append(task.Resources, &mesosproto.Resource{ Name: proto.String("mem"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Mem)}, }) } if task.Disk > 0 { task.Resources = append(task.Resources, &mesosproto.Resource{ Name: proto.String("disk"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Disk)}, }) } if len(task.Ports) > 0 { ranges := &mesosproto.Value_Ranges{} for _, port := range task.Ports { if port.HostPort == 0 { continue } ranges.Range = append(ranges.Range, &mesosproto.Value_Range{ Begin: proto.Uint64(uint64(port.HostPort)), End: proto.Uint64(uint64(port.HostPort)), }) } task.Resources = append(task.Resources, &mesosproto.Resource{ Name: proto.String("ports"), Type: mesosproto.Value_RANGES.Enum(), Ranges: ranges, }) } task.Build = true }
// LaunchTask with specific offer and resources func (core *Core) LaunchTask(task *registry.Task, offer *mesosproto.Offer, offers []*mesosproto.Offer) error { core.generateResource(task) resources := resource.BuildResources(task) log.Infof("Launch task %v, on node %v", task.ID, offer.GetHostname()) taskInfo := &mesosproto.TaskInfo{} var err error if task.Type == registry.TaskTypeTest { taskInfo, err = core.CreateSingleTaskInfo(offer, resources, task) log.Debug(taskInfo) } else if task.Type == registry.TaskTypeBuild { taskInfo, err = core.CreateBuildImageTaskInfo(offer, resources, task) } else { return errors.New("Unknown task type received.") } if err != nil { return err } for _, value := range offers { if offer.GetSlaveId() == value.GetSlaveId() { if err := core.AcceptOffer(value, resources, taskInfo); err != nil { return err } core.deleteOffer(value) task.State = "TASK_STAGING" } } return nil }
func (core *Core) generateResource(task *registry.Task) { var resources = []*mesosproto.Resource{} resources = append(resources, &mesosproto.Resource{ Name: proto.String("cpus"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Cpus)}, }) resources = append(resources, &mesosproto.Resource{ Name: proto.String("mem"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Mem)}, }) resources = append(resources, &mesosproto.Resource{ Name: proto.String("disk"), Type: mesosproto.Value_SCALAR.Enum(), Scalar: &mesosproto.Value_Scalar{Value: proto.Float64(task.Disk)}, }) resources = append(resources, core.MergePorts(task.Ports)) task.Resources = resources }
// CollectResult collect result for task func (core *Core) CollectResult(job *registry.Job, task *registry.Task) { taskInstance := ®istry.Task{ Cpus: CollectCPU, Mem: CollectMem, ID: "collect-" + job.ID + "-" + task.ID, Name: job.Name, Type: registry.TaskTypeBuild, CreateTime: time.Now().UnixNano(), JobID: job.ID, State: "TASK_WAITING", SLA: registry.SLAOnePerNode, Directory: task.Directory, } err := core.AddTask(taskInstance.ID, taskInstance) job.PushTask(taskInstance) if err != nil { log.Errorf("Error when add %d result collector: %v", err) task.State = "TASK_FAILED" job.PopLastTask() } }