github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/postsql/events/events.go (about)

     1  package events
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	eventsapi "github.com/kyma-project/kyma-environment-broker/common/events"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage/postsql"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  type events struct {
    13  	postsql.Factory
    14  
    15  	log logrus.FieldLogger
    16  }
    17  
    18  func New(fac postsql.Factory, log logrus.FieldLogger) *events {
    19  	return &events{Factory: fac, log: log}
    20  }
    21  
    22  func (e *events) ListEvents(filter eventsapi.EventFilter) ([]eventsapi.EventDTO, error) {
    23  	if e == nil {
    24  		return nil, fmt.Errorf("events are disabled")
    25  	}
    26  	sess := e.NewReadSession()
    27  	return sess.ListEvents(filter)
    28  }
    29  
    30  func (e *events) InsertEvent(eventLevel eventsapi.EventLevel, message, instanceID, operationID string) {
    31  	if e == nil {
    32  		return
    33  	}
    34  	sess := e.NewWriteSession()
    35  	if err := sess.InsertEvent(eventLevel, message, instanceID, operationID); err != nil {
    36  		e.log.Errorf("failed to insert event [%v] %v/%v %q: %v", eventLevel, instanceID, operationID, message, err)
    37  	}
    38  }
    39  
    40  func (e *events) RunGarbageCollection(pollingPeriod, retention time.Duration) {
    41  	if e == nil {
    42  		return
    43  	}
    44  	if retention == 0 {
    45  		return
    46  	}
    47  	ticker := time.NewTicker(pollingPeriod)
    48  	for {
    49  		select {
    50  		case <-ticker.C:
    51  			sess := e.NewWriteSession()
    52  			if err := sess.DeleteEvents(time.Now().Add(-retention)); err != nil {
    53  				e.log.Errorf("failed to delete old events: %v", err)
    54  			}
    55  		}
    56  	}
    57  }