package rabbitmq import ( "context" "encoding/json" "fmt" "time" amqp "github.com/rabbitmq/amqp091-go" ) type ruleTriggeredEvent struct { RuleID int64 `json:"rule_id"` ZoneID int64 `json:"zone_id"` DeviceID string `json:"device_id"` ActionType string `json:"action_type"` Success bool `json:"success"` Error string `json:"error,omitempty"` At time.Time `json:"at"` } // PublishRuleTriggered emits one event per rule match+dispatch attempt, so // notification-service can (eventually) alert on it. func (c *Client) PublishRuleTriggered(ctx context.Context, ruleID, zoneID int64, deviceID, actionType string, success bool, errMsg string) error { body, err := json.Marshal(ruleTriggeredEvent{ RuleID: ruleID, ZoneID: zoneID, DeviceID: deviceID, ActionType: actionType, Success: success, Error: errMsg, At: time.Now(), }) if err != nil { return fmt.Errorf("marshal event: %w", err) } err = c.publishCh.PublishWithContext(ctx, "", RuleTriggeredQueue, false, false, amqp.Publishing{ ContentType: "application/json", DeliveryMode: amqp.Persistent, Timestamp: time.Now(), Body: body, }) if err != nil { return fmt.Errorf("publish rule triggered event for rule %d: %w", ruleID, err) } return nil }