Live video on the AT Protocol
fork

Configure Feed

Select the types of activity you want to include in your feed.

at eli/fix-postgres-locking 74 lines 2.3 kB view raw
1package misttriggers 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "strconv" 9 10 "github.com/golang/glog" 11 "stream.place/streamplace/pkg/errors" 12 "stream.place/streamplace/pkg/mist/mistclient" 13) 14 15type PushEndPayload struct { 16 PushID int 17 StreamName string 18 Destination string 19 ActualDestination string 20 Last10LogLines string 21 PushStatus *mistclient.MistPushStats 22} 23 24func ParsePushEndPayload(body MistTriggerBody) (PushEndPayload, error) { 25 lines := body.Lines() 26 if len(lines) != 6 { 27 return PushEndPayload{}, fmt.Errorf("expected 6 lines in PUSH_END payload but got %d. Payload: %s", len(lines), body) 28 } 29 30 pushID, err := strconv.Atoi(lines[0]) 31 if err != nil { 32 return PushEndPayload{}, fmt.Errorf("error converting pushID to number pushID=%s err=%w", lines[0], err) 33 } 34 35 stats := &mistclient.MistPushStats{} 36 err = json.Unmarshal([]byte(lines[5]), stats) 37 if err != nil { 38 return PushEndPayload{}, fmt.Errorf("error unmarhsaling PushStatus: %w", err) 39 } 40 41 return PushEndPayload{ 42 PushID: pushID, 43 StreamName: lines[1], 44 Destination: lines[2], 45 ActualDestination: lines[3], 46 Last10LogLines: lines[4], 47 PushStatus: stats, 48 }, nil 49} 50 51// TriggerPushEnd responds to PUSH_END trigger 52// This trigger is run whenever an outgoing push stops, for any reason. 53// This trigger is stream-specific and non-blocking. The payload for this trigger is multiple lines, 54// each separated by a single newline character (without an ending newline), containing data: 55// 56// push ID (integer) 57// stream name (string) 58// target URI, before variables/triggers affected it (string) 59// target URI, afterwards, as actually used (string) 60// last 10 log messages (JSON array string) 61// most recent push status (JSON object string) 62func (d *MistCallbackHandlersCollection) TriggerPushEnd(ctx context.Context, w http.ResponseWriter, req *http.Request, body MistTriggerBody) { 63 payload, err := ParsePushEndPayload(body) 64 if err != nil { 65 errors.WriteHTTPBadRequest(w, "Error parsing PUSH_END payload", err) 66 return 67 } 68 err = d.broker.TriggerPushEnd(ctx, &payload) 69 if err != nil { 70 glog.Infof("Error handling PUSH_END payload error=%q payload=%q", err, string(body)) 71 errors.WriteHTTPInternalServerError(w, "Error handling PUSH_END payload", err) 72 return 73 } 74}