Skip to content

Commit dd6743d

Browse files
committed
add background job to sync message type across messages if they are identical
1 parent 98d10fa commit dd6743d

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

app/controllers/trained_messages_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def bulk_update
8888

8989
messages.update_all(message_type: new_message_type_symbol, updated_at: Time.current)
9090

91+
# Update all messages to the same message type if they have same
92+
# message hash
93+
message_hashes_to_sync = messages.pluck(:message_hash).uniq
94+
message_hashes_to_sync.each do |hash|
95+
MessageTypeSyncJob.perform_later(hash, new_message_type_symbol)
96+
end
97+
9198
messages.each do |message|
9299
if message.spam? || message.ham?
93100
BatchProcessor.add_to_batch(

app/jobs/message_type_sync_job.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MessageTypeSyncJob < ApplicationJob
2+
queue_as :low_priority
3+
4+
def perform(message_hash, new_message_type_symbol)
5+
new_message_type = new_message_type_symbol.to_s
6+
7+
# 1. Update all messages with the same hash
8+
new_type_value = TrainedMessage.message_types[new_message_type]
9+
10+
# Select only messages whose type will *actually* change to prevent unnecessary updates
11+
messages_to_sync = TrainedMessage.where(message_hash: message_hash)
12+
.where.not(message_type: new_message_type)
13+
14+
messages_to_sync.update_all(message_type: new_type_value, updated_at: Time.current)
15+
16+
# 2. Trigger Retraining and Ban Checks for all updated messages
17+
messages_to_sync.each do |message|
18+
if message.spam? || message.ham?
19+
BatchProcessor.add_to_batch(
20+
"classifier_training_batch_key",
21+
"ClassifierTrainerJob",
22+
message,
23+
{},
24+
batch_size: 100,
25+
batch_window: 5.minutes
26+
)
27+
end
28+
end
29+
end
30+
end

app/models/trained_message.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Source
3737
after_update :retrain_classifier, if: :trainable_type_changed?
3838
after_create :should_ban_user, if: :trainable?
3939
after_update :should_ban_user, if: :trainable_type_changed?
40-
40+
after_update :sync_message_type_by_hash, if: :saved_change_to_message_type?
4141

4242
def should_ban_user
4343
if [ GroupClassifierState::TELEGRAM_DATA_COLLECTOR_GROUP_ID, GroupClassifierState::USER_NAME_CLASSIFIER_GROUP_ID ].include? self.group_id
@@ -99,4 +99,10 @@ def trainable_type_changed?
9999
def set_message_hash
100100
self.message_hash = Digest::SHA256.hexdigest(message.to_s)
101101
end
102+
103+
def sync_message_type_by_hash
104+
if spam? || ham?
105+
MessageTypeSyncJob.perform_later(self.message_hash, self.message_type.to_sym)
106+
end
107+
end
102108
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class MessageTypeSyncJobTest < ActiveJob::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)