Example #1
0
func startProcessHF(w http.ResponseWriter, r *http.Request, p rest.Params) error {
	command := Command{}
	if err := restutil.ReadJson(r, &command); err != nil {
		return err
	}
	if err := checkCommand(&command); err != nil {
		return rest.BadRequest(err)
	}

	// If channel is provided then check whether it is ready to be
	// first process subscriber and use it if it is
	var subscriber *Subscriber
	channelId := r.URL.Query().Get("channel")
	if channelId != "" {
		channel, ok := rpc.GetChannel(channelId)
		if !ok {
			m := fmt.Sprintf("Channel with id '%s' doesn't exist. Process won't be started", channelId)
			return rest.NotFound(errors.New(m))
		}
		subscriber = &Subscriber{
			Id:      channelId,
			Mask:    parseTypes(r.URL.Query().Get("types")),
			Channel: channel.Events,
		}
	}

	pb := NewBuilder().Cmd(command)

	if subscriber != nil {
		pb.FirstSubscriber(*subscriber)
	}

	process, err := pb.Start()
	if err != nil {
		return err
	}
	return restutil.WriteJson(w, process)
}
Example #2
0
func asHttpError(err error) error {
	if npErr, ok := err.(*NoProcessError); ok {
		return rest.NotFound(npErr.error)
	}
	return err
}