-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor > optimised publishing #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bedrock-adam
wants to merge
6
commits into
master
Choose a base branch
from
refactor/find_by_update_or_create_message_counter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor > optimised publishing #386
bedrock-adam
wants to merge
6
commits into
master
from
refactor/find_by_update_or_create_message_counter
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bedrock-adam
commented
Nov 23, 2025
| require_relative "outboxer/models/exception" | ||
| require_relative "outboxer/models/message" | ||
| require_relative "outboxer/models/message/count" | ||
| require_relative "outboxer/models/message/counter" |
Contributor
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should be a thread?
Contributor
Author
def rollup_counts(time: Time)
current_utc_time = time.now.utc
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
historic_hostname = Models::Message::Counter::HISTORIC_HOSTNAME
historic_process_id = Models::Message::Counter::HISTORIC_PROCESS_ID
historic_thread_id = Models::Message::Counter::HISTORIC_THREAD_ID
# 1. Lock or create the historic counter row.
historic_counter = Models::Message::Counter.lock.find_by(
hostname: historic_hostname,
process_id: historic_process_id,
thread_id: historic_thread_id
)
if historic_counter.nil?
historic_counter = Models::Message::Counter.create!(
hostname: historic_hostname,
process_id: historic_process_id,
thread_id: historic_thread_id,
queued_count: 0,
publishing_count: 0,
published_count: 0,
failed_count: 0,
created_at: current_utc_time,
updated_at: current_utc_time
)
end
# 2. Capture the exact thread_counter IDs to roll up (stable snapshot).
thread_counter_ids = Models::Message::Counter
.where.not(
hostname: historic_hostname,
process_id: historic_process_id,
thread_id: historic_thread_id
)
.pluck(:id)
# Nothing to do if no thread counters exist.
if thread_counter_ids.empty?
return {
queued: historic_counter.queued_count,
publishing: historic_counter.publishing_count,
published: historic_counter.published_count,
failed: historic_counter.failed_count
}
end
# 3. Lock exactly the rows we captured.
thread_counters = Models::Message::Counter
.where(id: thread_counter_ids)
.lock("FOR UPDATE")
.to_a
# 4. Compute the rollup totals.
totals = {
queued_count: 0,
publishing_count: 0,
published_count: 0,
failed_count: 0
}
thread_counters.each do |counter|
totals[:queued_count] += counter.queued_count
totals[:publishing_count] += counter.publishing_count
totals[:published_count] += counter.published_count
totals[:failed_count] += counter.failed_count
end
# 5. Update the historic counter by adding the totals.
historic_counter.update!(
queued_count: historic_counter.queued_count + totals[:queued_count],
publishing_count: historic_counter.publishing_count + totals[:publishing_count],
published_count: historic_counter.published_count + totals[:published_count],
failed_count: historic_counter.failed_count + totals[:failed_count],
updated_at: current_utc_time
)
# 6. Delete exactly the rows we rolled up.
Models::Message::Counter.where(id: thread_counter_ids).delete_all
# 7. Return updated totals.
{
queued: historic_counter.queued_count,
publishing: historic_counter.publishing_count,
published: historic_counter.published_count,
failed: historic_counter.failed_count
}
end
end
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Record messages updated to separate per thread model, to remove sweeper entirely
Also improves .count query performance