Feat: remove rooms with no action
This commit is contained in:
		| @@ -2,6 +2,8 @@ package crons | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"database/sql" | ||||
| 	"errors" | ||||
| 	"gralias/repos" | ||||
| 	"log/slog" | ||||
| 	"time" | ||||
| @@ -25,6 +27,7 @@ func (cm *CronManager) Start() { | ||||
| 		for range ticker.C { | ||||
| 			cm.CleanupRooms() | ||||
| 			cm.CleanupActions() | ||||
| 			cm.CleanupInactiveRooms() | ||||
| 		} | ||||
| 	}() | ||||
| } | ||||
| @@ -133,3 +136,51 @@ func (cm *CronManager) CleanupActions() { | ||||
| 		cm.log.Error("failed to commit transaction for actions cleanup", "err", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (cm *CronManager) CleanupInactiveRooms() { | ||||
| 	ctx, tx, err := cm.repo.InitTx(context.Background()) | ||||
| 	if err != nil { | ||||
| 		cm.log.Error("failed to init transaction for inactive rooms cleanup", "err", err) | ||||
| 		return | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		if r := recover(); r != nil { | ||||
| 			if err := tx.Rollback(); err != nil { | ||||
| 				cm.log.Error("failed to rollback transaction for inactive rooms cleanup", "err", err) | ||||
| 			} | ||||
| 			panic(r) | ||||
| 		} | ||||
| 	}() | ||||
| 	rooms, err := cm.repo.RoomList(ctx) | ||||
| 	if err != nil { | ||||
| 		cm.log.Error("failed to get rooms list for inactive rooms cleanup", "err", err) | ||||
| 		if err := tx.Rollback(); err != nil { | ||||
| 			cm.log.Error("failed to rollback transaction for inactive rooms cleanup", "err", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 	for _, room := range rooms { | ||||
| 		lastActionTime, err := cm.repo.ActionGetLastTimeByRoomID(ctx, room.ID) | ||||
| 		if err != nil && !errors.Is(err, sql.ErrNoRows) { | ||||
| 			cm.log.Error("failed to get last action time for room", "room_id", room.ID, "err", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if lastActionTime.IsZero() && time.Since(room.CreatedAt) > time.Hour { | ||||
| 			cm.log.Info("deleting inactive room (no actions)", "room_id", room.ID, "created_at", room.CreatedAt) | ||||
| 			if err := cm.repo.RoomDeleteByID(ctx, room.ID); err != nil { | ||||
| 				cm.log.Error("failed to delete inactive room (no actions)", "room_id", room.ID, "err", err) | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
| 		if !lastActionTime.IsZero() && time.Since(lastActionTime) > time.Hour && time.Since(room.CreatedAt) > time.Hour { | ||||
| 			cm.log.Info("deleting inactive room (last action older than 1 hour)", "room_id", room.ID, "last_action_time", lastActionTime) | ||||
| 			if err := cm.repo.RoomDeleteByID(ctx, room.ID); err != nil { | ||||
| 				cm.log.Error("failed to delete inactive room (last action older than 1 hour)", "room_id", room.ID, "err", err) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	if err := tx.Commit(); err != nil { | ||||
| 		cm.log.Error("failed to commit transaction for inactive rooms cleanup", "err", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder