MCPcopy Create free account
hub / github.com/cloudbase/garm / AddInstanceEvent

Method AddInstanceEvent

database/sql/instances.go:255–300  ·  view source on GitHub ↗
(ctx context.Context, instanceName string, event params.EventType, eventLevel params.EventLevel, statusMessage string)

Source from the content-addressed store, hash-verified

253}
254
255func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceName string, event params.EventType, eventLevel params.EventLevel, statusMessage string) error {
256 instance, err := s.getInstance(ctx, s.conn, instanceName)
257 if err != nil {
258 return fmt.Errorf("error updating instance: %w", err)
259 }
260
261 msg := InstanceStatusUpdate{
262 Message: statusMessage,
263 EventType: event,
264 EventLevel: eventLevel,
265 }
266
267 // Use Create instead of Association.Append to avoid loading all existing messages
268 msg.InstanceID = instance.ID
269 if err := s.conn.Create(&msg).Error; err != nil {
270 return fmt.Errorf("error adding status message: %w", err)
271 }
272
273 // Keep only the latest 30 status messages to prevent database bloat
274 const maxStatusMessages = 30
275 var count int64
276 if err := s.conn.Model(&InstanceStatusUpdate{}).Where("instance_id = ?", instance.ID).Count(&count).Error; err != nil {
277 return fmt.Errorf("error counting status messages: %w", err)
278 }
279
280 if count > maxStatusMessages {
281 // Get the ID of the 30th most recent message
282 var cutoffMsg InstanceStatusUpdate
283 if err := s.conn.Model(&InstanceStatusUpdate{}).
284 Select("id").
285 Where("instance_id = ?", instance.ID).
286 Order("id desc").
287 Offset(maxStatusMessages - 1).
288 Limit(1).
289 First(&cutoffMsg).Error; err != nil {
290 return fmt.Errorf("error finding cutoff message: %w", err)
291 }
292
293 // Delete all messages older than the cutoff
294 if err := s.conn.Where("instance_id = ? and id < ?", instance.ID, cutoffMsg.ID).Unscoped().Delete(&InstanceStatusUpdate{}).Error; err != nil {
295 return fmt.Errorf("error deleting old status messages: %w", err)
296 }
297 }
298
299 return nil
300}
301
302// validateAgentID checks agent ID consistency
303func (s *sqlDatabase) validateAgentID(currentAgentID, newAgentID int64) error {

Callers

nothing calls this directly

Calls 2

getInstanceMethod · 0.95
DeleteMethod · 0.65

Tested by

no test coverage detected