feat: add event cleanup configuration and functionality
Signed-off-by: zhenyus <zhenyus@mathmast.com>
This commit is contained in:
parent
0c55ce90fa
commit
70c4951bdb
@ -47,4 +47,8 @@ worker:
|
|||||||
poolSize: 10
|
poolSize: 10
|
||||||
queueSize: 100
|
queueSize: 100
|
||||||
maxRetries: 3
|
maxRetries: 3
|
||||||
retryBackoff: 1
|
retryBackoff: 1
|
||||||
|
|
||||||
|
eventCleanup:
|
||||||
|
interval: 3600
|
||||||
|
expireAfter: 7200
|
||||||
@ -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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user