Skip to content

Commit 3cedb20

Browse files
feat: add priority field to server for queue ordering
Adds a priority field to servers that controls the order in which queued messages are processed. Messages from servers with higher priority values are sent first. Includes: - Database migration to add priority column - Validation for priority (0-32767) - UI field in server form - Queue ordering by server priority Cherry-picked from postalserver/postal PR postalserver#3446 Co-authored-by: Xyaren <Xyaren@users.noreply.github.com> Co-authored-by: openhands <openhands@all-hands.dev>
1 parent dd832e9 commit 3cedb20

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

app/controllers/servers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def unsuspend
9696
private
9797

9898
def safe_params(*extras)
99-
params.require(:server).permit(:name, :mode, :ip_pool_id, *extras)
99+
params.require(:server).permit(:name, :mode, :priority, :ip_pool_id, *extras)
100100
end
101101

102102
end

app/lib/worker/jobs/process_queued_messages_job.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ def local_ip?(ip)
4040
end
4141

4242
# Obtain a queued message from the database for processing
43+
# Messages are prioritized based on the server's priority (higher priority first)
44+
# and then by the message ID (ascending order).
4345
#
4446
# @return [void]
4547
def lock_message_for_processing
46-
QueuedMessage.where(ip_address_id: [nil, @ip_addresses])
48+
QueuedMessage.joins(:server)
49+
.where(ip_address_id: [nil, @ip_addresses])
4750
.where(locked_by: nil, locked_at: nil)
4851
.ready_with_delayed_retry
52+
.order("servers.priority DESC, queued_messages.id ASC")
4953
.limit(1)
5054
.update_all(locked_by: @locker, locked_at: @lock_time)
5155
end

app/models/server.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class Server < ApplicationRecord
7777
validates :name, presence: true, uniqueness: { scope: :organization_id, case_sensitive: false }
7878
validates :mode, inclusion: { in: MODES }
7979
validates :permalink, presence: true, uniqueness: { scope: :organization_id, case_sensitive: false }, format: { with: /\A[a-z0-9-]*\z/ }, exclusion: { in: RESERVED_PERMALINKS }
80+
validates :priority, presence: true, numericality: {
81+
only_integer: true,
82+
greater_than_or_equal_to: 0,
83+
less_than_or_equal_to: 32767,
84+
message: "must be a whole number between 0 and 32,767"
85+
}
8086
validate :validate_ip_pool_belongs_to_organization
8187

8288
before_validation(on: :create) do

app/views/servers/_form.html.haml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
e-mail will be routed normally to the intended recipients. When in <b>Development</b> mode,
2121
outgoing & incoming mail will be held and only visible in the web interface and will not be
2222
sent to any recipients or HTTP endpoints.
23+
.fieldSet__field
24+
= f.label :priority, "Sending Priority", :class => 'fieldSet__label'
25+
.fieldSet__input
26+
= f.text_field :priority,
27+
:class => 'input input--text',
28+
:placeholder => "e.g. 10"
29+
%p.fieldSet__text
30+
Set a priority for this server's outgoing mail. Messages from servers with a higher number will be sent first.
31+
The default is <b>0</b>.
2332

2433
- if Postal.ip_pools?
2534
.fieldSet__field
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class AddPriorityToServer < ActiveRecord::Migration[7.0]
4+
5+
def change
6+
add_column :servers, :priority, :integer, limit: 2, unsigned: true, default: 0
7+
end
8+
9+
end

0 commit comments

Comments
 (0)