示例#1
0
func subscribeReqHF(params interface{}, t *rpc.Transmitter) error {
	subscribeParams := params.(SubscribeParams)

	mask := maskFromTypes(subscribeParams.EventTypes)
	if mask == 0 {
		return rpc.NewArgsError(errors.New("Required at least 1 valid event type"))
	}

	subscriber := Subscriber{
		Id:      t.Channel.Id,
		Mask:    mask,
		Channel: t.Channel.Events,
	}
	// Check whether subscriber should see previous logs or not
	if subscribeParams.After == "" {
		if err := AddSubscriber(subscribeParams.Pid, subscriber); err != nil {
			return asRpcError(err)
		}
	} else {
		after, err := time.Parse(DateTimeFormat, subscribeParams.After)
		if err != nil {
			return rpc.NewArgsError(errors.New("Bad format of 'after', " + err.Error()))
		}
		if err := RestoreSubscriber(subscribeParams.Pid, subscriber, after); err != nil {
			return err
		}
	}
	t.Send(&SubscribeResult{
		Pid:        subscribeParams.Pid,
		EventTypes: subscribeParams.EventTypes,
		Text:       "Successfully subscribed",
	})
	return nil
}
示例#2
0
func getProcessReqHF(body interface{}, t *rpc.Transmitter) error {
	params := body.(GetProcessParams)
	p, err := Get(params.Pid)
	if err != nil {
		return asRpcError(err)
	}
	t.Send(p)
	return nil
}
示例#3
0
func unsubscribeReqHF(params interface{}, t *rpc.Transmitter) error {
	unsubscribeParams := params.(UnsubscribeParams)
	if err := RemoveSubscriber(unsubscribeParams.Pid, t.Channel.Id); err != nil {
		return asRpcError(err)
	}
	t.Send(&ProcessResult{
		Pid:  unsubscribeParams.Pid,
		Text: "Successfully unsubscribed",
	})
	return nil
}
示例#4
0
func killProcessReqHF(params interface{}, t *rpc.Transmitter) error {
	killParams := params.(KillParams)
	if err := Kill(killParams.Pid); err != nil {
		return asRpcError(err)
	}
	t.Send(&ProcessResult{
		Pid:  killParams.Pid,
		Text: "Successfully killed",
	})
	return nil
}
示例#5
0
func updateSubscriberReqHF(params interface{}, t *rpc.Transmitter) error {
	updateParams := params.(UpdateSubscriberParams)
	if updateParams.EventTypes == "" {
		return rpc.NewArgsError(errors.New("'eventTypes' required for subscriber update"))
	}
	if err := UpdateSubscriber(updateParams.Pid, t.Channel.Id, maskFromTypes(updateParams.EventTypes)); err != nil {
		return asRpcError(err)
	}
	t.Send(&SubscribeResult{
		Pid:        updateParams.Pid,
		EventTypes: updateParams.EventTypes,
		Text:       "Subscriber successfully updated",
	})
	return nil
}
示例#6
0
func getProcessLogsReqHF(params interface{}, t *rpc.Transmitter) error {
	getLogsParams := params.(GetLogsParams)

	if getLogsParams.Skip < 0 {
		getLogsParams.Skip = 0
	}
	if getLogsParams.Limit < 0 {
		getLogsParams.Limit = 0
	}

	from, err := parseTime(getLogsParams.From, time.Time{})
	if err != nil {
		return rpc.NewArgsError(errors.New("Bad format of 'from', " + err.Error()))
	}

	till, err := parseTime(getLogsParams.Till, time.Now())
	if err != nil {
		return rpc.NewArgsError(errors.New("Bad format of 'till', " + err.Error()))
	}

	logs, err := ReadLogs(getLogsParams.Pid, from, till)
	if err != nil {
		return asRpcError(err)
	}

	limit := DefaultLogsPerPageLimit
	if getLogsParams.Limit != 0 {
		if limit < 1 {
			return rpc.NewArgsError(errors.New("Required 'limit' to be > 0"))
		}
		limit = getLogsParams.Limit
	}

	skip := 0
	if getLogsParams.Skip != 0 {
		if skip < 0 {
			return rpc.NewArgsError(errors.New("Required 'skip' to be >= 0"))
		}
		skip = getLogsParams.Skip
	}

	logsLen := len(logs)
	fromIdx := int(math.Max(float64(logsLen-limit-skip), 0))
	toIdx := logsLen - int(math.Min(float64(skip), float64(logsLen)))

	t.Send(logs[fromIdx:toIdx])
	return nil
}
示例#7
0
func startProcessReqHF(params interface{}, t *rpc.Transmitter) error {
	startParams := params.(StartParams)
	command := Command{
		Name:        startParams.Name,
		CommandLine: startParams.CommandLine,
		Type:        startParams.Type,
	}
	if err := checkCommand(&command); err != nil {
		return rpc.NewArgsError(err)
	}

	_, err := NewBuilder().
		Cmd(command).
		FirstSubscriber(Subscriber{
			Id:      t.Channel.Id,
			Mask:    parseTypes(startParams.EventTypes),
			Channel: t.Channel.Events,
		}).
		BeforeEventsHook(func(process MachineProcess) {
			t.Send(process)
		}).
		Start()
	return err
}
示例#8
0
func getProcessesReqHF(body interface{}, t *rpc.Transmitter) error {
	params := body.(GetProcessesParams)
	t.Send(GetProcesses(params.All))
	return nil
}