WPB-24076: Add meeting cleaner job in background-worker#5207
WPB-24076: Add meeting cleaner job in background-worker#5207blackheaven wants to merge 1 commit intodevelopfrom
background-worker#5207Conversation
1af1c4f to
1df93ce
Compare
| -- You should have received a copy of the GNU Affero General Public License along | ||
| -- with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| module Wire.MeetingsSubsystemCleaning.Interpreter where |
There was a problem hiding this comment.
Is there any specific reason that this has its own module or could this go into MeetingSubsystem?
| cleanupOldMeetingsImpl :: | ||
| ( Member Store.MeetingsStore r, | ||
| Member ConvStore.ConversationStore r | ||
| ) => | ||
| UTCTime -> | ||
| Int -> | ||
| Sem r Int64 | ||
| cleanupOldMeetingsImpl cutoffTime batchSize = do | ||
| -- 1. Fetch old meetings | ||
| oldMeetings <- Store.getOldMeetings cutoffTime batchSize | ||
|
|
||
| if null oldMeetings | ||
| then pure 0 | ||
| else do | ||
| -- 2. Extract meeting IDs and conversation IDs | ||
| let meetingIds = map (\Store.StoredMeeting {id = mid} -> mid) oldMeetings | ||
| convIds = map (\Store.StoredMeeting {conversationId = cid} -> cid) oldMeetings | ||
|
|
||
| -- 3. Delete meetings from database | ||
| deletedCount <- Store.deleteMeetingBatch meetingIds | ||
|
|
||
| -- 4. Delete associated conversations if they are meeting conversations | ||
| -- We need to check if conversation has GroupConvType = MeetingConversation | ||
| for_ (zip oldMeetings convIds) $ \(meeting, convId) -> do | ||
| maybeConv <- ConvStore.getConversation convId | ||
| case maybeConv of | ||
| Just conv | ||
| | conv.metadata.cnvmGroupConvType == Just MeetingConversation, | ||
| conv.id_ == convId, | ||
| meeting.conversationId == convId -> | ||
| ConvStore.deleteConversation convId | ||
| _ -> pure () | ||
|
|
||
| pure deletedCount |
There was a problem hiding this comment.
I would say it would be better to use the MeetingSubsystem to delete a meeting because that will take care of proper conversation deletion as well via the ConversationSubsystem.
There was a problem hiding this comment.
I guess the reason to have it's own logic here is the batch deletion on a DB level. But we should properly remove all conversation related data, so using the MeetingSubsystem is the price we have to pay, IMO.
| import Polysemy | ||
|
|
||
| data MeetingsSubsystemCleaning m a where | ||
| CleanupOldMeetings :: |
There was a problem hiding this comment.
Could we put this into the MeetingSubsystem?
| convIds = map (\Store.StoredMeeting {conversationId = cid} -> cid) oldMeetings | ||
|
|
||
| -- 3. Delete meetings from database | ||
| deletedCount <- Store.deleteMeetingBatch meetingIds |
There was a problem hiding this comment.
We are deleting meeting before conversation data. Instead we should use the MeetingSubsystem for correct and more robust deletion, see below.
| WHERE end_date < ($1 :: timestamptz) | ||
| ORDER BY end_date ASC |
There was a problem hiding this comment.
| WHERE end_date < ($1 :: timestamptz) | |
| ORDER BY end_date ASC | |
| WHERE end_time < ($1 :: timestamptz) | |
| ORDER BY end_time ASC |
| deleteStatement = | ||
| [rowsAffectedStatement| | ||
| DELETE FROM meetings | ||
| WHERE (id, domain) IN (SELECT * FROM unnest($1::uuid[])) |
There was a problem hiding this comment.
domain is not a column in the meetings table.
| . runInputConst env.hasqlPool | ||
| . interpretMeetingsStoreToPostgres | ||
| . runInputConst env.hasqlPool | ||
| . interpretConversationStoreToPostgres |
There was a problem hiding this comment.
This should be migration aware.
| . Log.field "batch_deleted" deletedCount | ||
| . Log.field "total_deleted" newTotal | ||
| -- Continue if we deleted a full batch (meaning there might be more) | ||
| if deletedCount >= fromIntegral batchSize |
There was a problem hiding this comment.
we should not allow batch size to be 0 or this will loop forever.
1df93ce to
525b82e
Compare
https://wearezeta.atlassian.net/browse/WPB-24076
Checklist
changelog.d