Basic impl added

This commit is contained in:
2025-10-16 21:22:51 -07:00
parent 097ca99788
commit 9087a710e5
43 changed files with 7616 additions and 107 deletions
+115
View File
@@ -0,0 +1,115 @@
package domain
import (
"time"
)
// Priority defines the urgency level of a notification
type Priority int
const (
PriorityLow Priority = iota
PriorityNormal
PriorityHigh
PriorityCritical
)
// NotificationType defines the channel through which to send the notification
type NotificationType string
const (
TypeEmail NotificationType = "email"
TypeSlack NotificationType = "slack"
TypeNtfy NotificationType = "ntfy"
TypeStdout NotificationType = "stdout"
)
// NotificationStatus represents the current state of a notification
type NotificationStatus string
const (
StatusPending NotificationStatus = "pending"
StatusQueued NotificationStatus = "queued"
StatusProcessing NotificationStatus = "processing"
StatusSent NotificationStatus = "sent"
StatusFailed NotificationStatus = "failed"
StatusRetrying NotificationStatus = "retrying"
)
// Notification represents a notification message with metadata
type Notification struct {
// ID is a unique identifier for the notification
ID string `json:"id"`
// Type specifies which notifier should handle this notification
Type NotificationType `json:"type"`
// Priority determines urgency and retry behavior
Priority Priority `json:"priority"`
// Status tracks the current state of the notification
Status NotificationStatus `json:"status"`
// Subject is the notification subject/title (used for email, slack, ntfy)
Subject string `json:"subject"`
// Body is the main content of the notification
Body string `json:"body"`
// Recipients contains the target addresses (email, slack channel, ntfy topic, etc.)
Recipients []string `json:"recipients"`
// Metadata contains additional provider-specific data
Metadata map[string]interface{} `json:"metadata,omitempty"`
// CreatedAt is when the notification was created
CreatedAt time.Time `json:"created_at"`
// ScheduledFor allows delayed sending (optional)
ScheduledFor *time.Time `json:"scheduled_for,omitempty"`
// SentAt is when the notification was successfully sent
SentAt *time.Time `json:"sent_at,omitempty"`
// RetryCount tracks how many times sending has been attempted
RetryCount int `json:"retry_count"`
// MaxRetries defines the maximum retry attempts
MaxRetries int `json:"max_retries"`
// LastError stores the most recent error message if failed
LastError string `json:"last_error,omitempty"`
}
// NotificationResult represents the outcome of sending a notification
type NotificationResult struct {
// NotificationID references the original notification
NotificationID string `json:"notification_id"`
// Success indicates if the notification was sent successfully
Success bool `json:"success"`
// Message provides additional context about the result
Message string `json:"message,omitempty"`
// Error contains error details if the notification failed
Error string `json:"error,omitempty"`
// SentAt is when the notification was sent
SentAt time.Time `json:"sent_at"`
// ProviderResponse contains raw response data from the notification provider
ProviderResponse map[string]interface{} `json:"provider_response,omitempty"`
}
// NotificationFilter is used for querying notifications
type NotificationFilter struct {
IDs []string `json:"ids,omitempty"`
Types []NotificationType `json:"types,omitempty"`
Statuses []NotificationStatus `json:"statuses,omitempty"`
Recipients []string `json:"recipients,omitempty"`
CreatedAfter *time.Time `json:"created_after,omitempty"`
CreatedBefore *time.Time `json:"created_before,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
}
+67
View File
@@ -0,0 +1,67 @@
package domain
import (
"context"
)
// Notifier is the core interface that all notification implementations must satisfy
type Notifier interface {
// Send sends a notification and returns the result
Send(ctx context.Context, notification *Notification) (*NotificationResult, error)
// Type returns the notification type this notifier handles
Type() NotificationType
// Validate checks if a notification can be sent with this notifier
Validate(notification *Notification) error
// Close performs cleanup when the notifier is no longer needed
Close() error
}
// NotifierFactory creates notifier instances based on configuration
type NotifierFactory interface {
// Create creates a notifier for the given type
Create(notificationType NotificationType) (Notifier, error)
// RegisterNotifier registers a custom notifier implementation
RegisterNotifier(notificationType NotificationType, notifier Notifier) error
// SupportedTypes returns all supported notification types
SupportedTypes() []NotificationType
}
// NotificationService is the high-level service interface for managing notifications
type NotificationService interface {
// Send queues a notification for delivery
Send(ctx context.Context, notification *Notification) (*NotificationResult, error)
// SendBatch queues multiple notifications for delivery
SendBatch(ctx context.Context, notifications []*Notification) ([]*NotificationResult, error)
// GetNotification retrieves a notification by ID
GetNotification(ctx context.Context, id string) (*Notification, error)
// ListNotifications retrieves notifications matching the filter
ListNotifications(ctx context.Context, filter *NotificationFilter) ([]*Notification, error)
// CancelNotification cancels a pending notification
CancelNotification(ctx context.Context, id string) error
// RetryNotification retries a failed notification
RetryNotification(ctx context.Context, id string) (*NotificationResult, error)
// GetStats returns notification statistics
GetStats(ctx context.Context) (*NotificationStats, error)
}
// NotificationStats contains statistics about notification processing
type NotificationStats struct {
TotalSent int64 `json:"total_sent"`
TotalFailed int64 `json:"total_failed"`
TotalPending int64 `json:"total_pending"`
TotalQueued int64 `json:"total_queued"`
ByType map[string]int64 `json:"by_type"`
ByStatus map[string]int64 `json:"by_status"`
AverageLatency float64 `json:"average_latency_ms"`
}
+111
View File
@@ -0,0 +1,111 @@
package domain
import (
"context"
)
// QueueMessage wraps a notification with queue-specific metadata
type QueueMessage struct {
// ID is a unique identifier for this queue message
ID string `json:"id"`
// Notification is the actual notification to be sent
Notification *Notification `json:"notification"`
// Attempt is the current delivery attempt number
Attempt int `json:"attempt"`
// EnqueuedAt is when the message was added to the queue
EnqueuedAt int64 `json:"enqueued_at"`
}
// Queue defines the interface for a notification queue
type Queue interface {
// Enqueue adds a notification to the queue
Enqueue(ctx context.Context, notification *Notification) error
// EnqueueBatch adds multiple notifications to the queue
EnqueueBatch(ctx context.Context, notifications []*Notification) error
// Dequeue retrieves the next notification from the queue
// Returns nil if the queue is empty
Dequeue(ctx context.Context) (*QueueMessage, error)
// Ack acknowledges successful processing of a message
Ack(ctx context.Context, messageID string) error
// Nack indicates processing failure and may requeue the message
Nack(ctx context.Context, messageID string, requeue bool) error
// Size returns the current number of messages in the queue
Size(ctx context.Context) (int64, error)
// Purge removes all messages from the queue
Purge(ctx context.Context) error
// Close cleanly shuts down the queue
Close() error
// HealthCheck verifies the queue is operational
HealthCheck(ctx context.Context) error
}
// QueueConfig contains configuration for queue implementations
type QueueConfig struct {
// Type specifies the queue implementation (local, kafka, etc.)
Type string `mapstructure:"type"`
// MaxSize is the maximum number of messages the queue can hold
MaxSize int64 `mapstructure:"max_size"`
// WorkerCount is the number of concurrent workers processing the queue
WorkerCount int `mapstructure:"worker_count"`
// RetryAttempts is the number of times to retry failed notifications
RetryAttempts int `mapstructure:"retry_attempts"`
// RetryBackoff is the backoff strategy for retries (exponential, linear, fixed)
RetryBackoff string `mapstructure:"retry_backoff"`
// Local queue specific config
Local *LocalQueueConfig `mapstructure:"local,omitempty"`
// Kafka specific config
Kafka *KafkaQueueConfig `mapstructure:"kafka,omitempty"`
}
// LocalQueueConfig contains configuration for the in-memory queue
type LocalQueueConfig struct {
// BufferSize is the channel buffer size
BufferSize int `mapstructure:"buffer_size"`
// PersistToDisk enables writing queue state to disk for recovery
PersistToDisk bool `mapstructure:"persist_to_disk"`
// PersistPath is where to store the queue state
PersistPath string `mapstructure:"persist_path"`
}
// KafkaQueueConfig contains configuration for Kafka queue
type KafkaQueueConfig struct {
// Brokers is the list of Kafka broker addresses
Brokers []string `mapstructure:"brokers"`
// Topic is the Kafka topic for notifications
Topic string `mapstructure:"topic"`
// ConsumerGroup is the Kafka consumer group ID
ConsumerGroup string `mapstructure:"consumer_group"`
// PartitionCount is the number of partitions for the topic
PartitionCount int `mapstructure:"partition_count"`
// ReplicationFactor is the replication factor for the topic
ReplicationFactor int `mapstructure:"replication_factor"`
// EnableIdempotence ensures exactly-once delivery semantics
EnableIdempotence bool `mapstructure:"enable_idempotence"`
// CompressionType defines compression (none, gzip, snappy, lz4, zstd)
CompressionType string `mapstructure:"compression_type"`
}