| 6104 | } |
| 6105 | |
| 6106 | func (p *Server) shouldPublishFinishedChatState( |
| 6107 | ctx context.Context, |
| 6108 | logger slog.Logger, |
| 6109 | updatedChat database.Chat, |
| 6110 | ) bool { |
| 6111 | latestChat, err := p.db.GetChatByID(ctx, updatedChat.ID) |
| 6112 | if err != nil { |
| 6113 | logger.Warn(ctx, "failed to re-read chat before publishing finished state", |
| 6114 | slog.F("chat_id", updatedChat.ID), |
| 6115 | slog.Error(err), |
| 6116 | ) |
| 6117 | return true |
| 6118 | } |
| 6119 | |
| 6120 | if latestChat.Status != updatedChat.Status || latestChat.WorkerID != updatedChat.WorkerID { |
| 6121 | logger.Debug(ctx, "skipping stale finished chat publish", |
| 6122 | slog.F("chat_id", updatedChat.ID), |
| 6123 | slog.F("expected_status", updatedChat.Status), |
| 6124 | slog.F("expected_worker_id", updatedChat.WorkerID), |
| 6125 | slog.F("latest_status", latestChat.Status), |
| 6126 | slog.F("latest_worker_id", latestChat.WorkerID), |
| 6127 | ) |
| 6128 | return false |
| 6129 | } |
| 6130 | |
| 6131 | return true |
| 6132 | } |
| 6133 | |
| 6134 | func (p *Server) processChat(ctx context.Context, chat database.Chat) { |
| 6135 | logger := p.logger.With(slog.F("chat_id", chat.ID)) |