Exemple #1
0
// DiskInfo is a task handler to retrieve information about disks.
func (s *Simple) DiskInfo(req *acomm.Request) (interface{}, *url.URL, error) {
	var args CPUInfoArgs
	if err := req.UnmarshalArgs(&args); err != nil {
		return nil, nil, err
	}
	if args.GuestID == "" {
		return nil, nil, errors.New("missing guest_id")
	}

	result := &DiskInfoResult{
		&DiskInfo{
			Device: "vda1",
			Size:   10 * (1024 * 1024 * 1024), // 10 GB in bytes
		},
	}

	return result, nil, nil
}
Exemple #2
0
// DelayedResp is a task handler that waits a specified time before returning.
func (s *Simple) DelayedResp(req *acomm.Request) (interface{}, *url.URL, error) {
	var args DelayedRespArgs
	if err := req.UnmarshalArgs(&args); err != nil {
		return nil, nil, err
	}
	if args.Delay <= 0 {
		return nil, nil, errors.New("delay must be positive")
	}

	start := time.Now()
	time.Sleep(time.Second * args.Delay)

	result := &DelayedRespResult{
		Delay:       args.Delay,
		ReceivedAt:  start,
		RespondedAt: time.Now(),
	}

	return result, nil, nil
}
Exemple #3
0
// CPUInfo is a task handler to retrieve information about CPUs.
func (s *Simple) CPUInfo(req *acomm.Request) (interface{}, *url.URL, error) {
	var args CPUInfoArgs
	if err := req.UnmarshalArgs(&args); err != nil {
		return nil, nil, err
	}
	if args.GuestID == "" {
		return nil, nil, errors.New("missing guest_id")
	}

	result := &CPUInfoResult{
		&CPUInfo{
			Processor: 0,
			MHz:       2600,
		},
		&CPUInfo{
			Processor: 1,
			MHz:       2600,
		},
	}
	return result, nil, nil
}
Exemple #4
0
// SystemStatus is a task handler to retrieve info look up and return system
// information. It depends on and makes requests for several other tasks.
func (s *Simple) SystemStatus(req *acomm.Request) (interface{}, *url.URL, error) {
	var args SystemStatusArgs
	if err := req.UnmarshalArgs(&args); err != nil {
		return nil, nil, err
	}
	if args.GuestID == "" {
		return nil, nil, errors.New("missing guest_id")
	}

	// Prepare multiple requests
	multiRequest := acomm.NewMultiRequest(s.tracker, 0)

	cpuReq, err := acomm.NewRequest("CPUInfo", s.tracker.URL().String(), &CPUInfoArgs{GuestID: args.GuestID}, nil, nil)
	if err != nil {
		return nil, nil, err
	}
	diskReq, err := acomm.NewRequest("DiskInfo", s.tracker.URL().String(), &DiskInfoArgs{GuestID: args.GuestID}, nil, nil)
	if err != nil {
		return nil, nil, err
	}

	requests := map[string]*acomm.Request{
		"CPUInfo":  cpuReq,
		"DiskInfo": diskReq,
	}

	for name, req := range requests {
		if err := multiRequest.AddRequest(name, req); err != nil {
			continue
		}
		if err := acomm.Send(s.config.CoordinatorURL(), req); err != nil {
			multiRequest.RemoveRequest(req)
			continue
		}
	}

	// Wait for the results
	responses := multiRequest.Responses()
	result := &SystemStatusResult{}

	if resp, ok := responses["CPUInfo"]; ok {
		if err := resp.UnmarshalResult(&(result.CPUs)); err != nil {
			log.WithFields(log.Fields{
				"name":  "CPUInfo",
				"resp":  resp,
				"error": err,
			}).Error("failed to unarshal result")
		}
	}

	if resp, ok := responses["DiskInfo"]; ok {
		if err := resp.UnmarshalResult(&(result.Disks)); err != nil {
			log.WithFields(log.Fields{
				"name":  "DiskInfo",
				"resp":  resp,
				"error": err,
			}).Error("failed to unarshal result")
		}
	}

	return result, nil, nil
}