feat: add event cleanup configuration and functionality

Signed-off-by: zhenyus <zhenyus@mathmast.com>
This commit is contained in:
zhenyus 2025-05-11 22:33:47 +08:00
parent 0c55ce90fa
commit 70c4951bdb
2 changed files with 42 additions and 6 deletions

View File

@ -48,3 +48,7 @@ worker:
queueSize: 100 queueSize: 100
maxRetries: 3 maxRetries: 3
retryBackoff: 1 retryBackoff: 1
eventCleanup:
interval: 3600
expireAfter: 7200

View File

@ -53,6 +53,11 @@ type Configuration struct {
MaxRetries int `yaml:"maxRetries" default:"3" validate:"gte=0"` MaxRetries int `yaml:"maxRetries" default:"3" validate:"gte=0"`
RetryBackoff int `yaml:"retryBackoff" default:"1" validate:"gt=0"` // seconds RetryBackoff int `yaml:"retryBackoff" default:"1" validate:"gt=0"` // seconds
} `yaml:"worker"` } `yaml:"worker"`
EventCleanup struct {
Interval int `yaml:"interval" default:"3600"` // seconds
ExpireAfter int `yaml:"expireAfter" default:"7200"` // seconds
} `yaml:"eventCleanup"`
} }
// ProjectConfig represents the configuration for a specific repository // ProjectConfig represents the configuration for a specific repository
@ -150,6 +155,9 @@ func main() {
// Setup config file watcher for auto-reload // Setup config file watcher for auto-reload
setupConfigWatcher(*configFile) setupConfigWatcher(*configFile)
// Start event cleanup goroutine
go cleanupEvents()
// Configure HTTP client with timeout // Configure HTTP client with timeout
configMutex.RLock() configMutex.RLock()
httpClient = &http.Client{ httpClient = &http.Client{
@ -299,6 +307,12 @@ func loadConfig(file string) error {
if newConfig.Worker.RetryBackoff == 0 { if newConfig.Worker.RetryBackoff == 0 {
newConfig.Worker.RetryBackoff = 1 newConfig.Worker.RetryBackoff = 1
} }
if newConfig.EventCleanup.Interval == 0 {
newConfig.EventCleanup.Interval = 3600
}
if newConfig.EventCleanup.ExpireAfter == 0 {
newConfig.EventCleanup.ExpireAfter = 7200
}
// Handle legacy configuration format (where Projects is map[string]string) // Handle legacy configuration format (where Projects is map[string]string)
// This is to maintain backward compatibility with existing configs // This is to maintain backward compatibility with existing configs
@ -525,11 +539,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
} }
// Store in processed events with a TTL (we'll use a goroutine to remove after 1 hour) // Store in processed events with a TTL (we'll use a goroutine to remove after 1 hour)
processedEvents.Store(eventID, true) processedEvents.Store(eventID, time.Now())
go func(key string) {
time.Sleep(1 * time.Hour)
processedEvents.Delete(key)
}(eventID)
// Check if we have a Jenkins job mapping for this repository // Check if we have a Jenkins job mapping for this repository
configMutex.RLock() configMutex.RLock()
@ -742,3 +752,25 @@ func logError(format string, v ...interface{}) {
// Error level logs are always shown // Error level logs are always shown
logger.Printf("[ERROR] "+format, v...) logger.Printf("[ERROR] "+format, v...)
} }
func cleanupEvents() {
for {
configMutex.RLock()
interval := time.Duration(config.EventCleanup.Interval) * time.Second
expireAfter := time.Duration(config.EventCleanup.ExpireAfter) * time.Second
configMutex.RUnlock()
time.Sleep(interval)
now := time.Now()
processedEvents.Range(func(key, value interface{}) bool {
if timestamp, ok := value.(time.Time); ok {
if now.Sub(timestamp) > expireAfter {
processedEvents.Delete(key)
logDebug("Cleaned up expired event: %v", key)
}
}
return true
})
}
}