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 }
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 }
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 }
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 }