Skip to content

Commit f74d43d

Browse files
committed
Enable autotrimming
1 parent 7d39483 commit f74d43d

File tree

9 files changed

+81
-8
lines changed

9 files changed

+81
-8
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ production:
6262
The options are:
6363

6464
- `connects_to` - set the Active Record database configuration for the Solid Cable models. All options available in Active Record can be used here.
65-
- `polling_interval` - sets the frequency of the polling interval.
66-
- `message_retention` - sets the retention time for messages kept in the database. Used as the cut-off when trimming is performed.
65+
- `polling_interval` - sets the frequency of the polling interval. (Defaults to
66+
0.1.seconds)
67+
- `message_retention` - sets the retention time for messages kept in the database. Used as the cut-off when trimming is performed. (Defaults to 1.day)
68+
- `autotrim` - sets wether you want Solid Cable to handle autotrimming messages.
69+
(Defaults to true)
6770

6871
## Trimming
6972

70-
Currently, messages are kept around forever, and have to be manually pruned via the `SolidCable::PruneJob.perform_later` job.
71-
This job uses the `message_retention` setting to determine how long messages are to be kept around.
73+
Messages are autotrimmed based upon the `message_retention` setting to determine how long messages are to be kept around. If no `message_retention` is given or parsing fails, it defaults to `1.day`. Messages are trimmed when a subscriber unsubscribes.
74+
75+
Autotrimming can negatively impact performance depending on your workload because it is doing a delete on ubsubscribe. If
76+
you would prefer, you can disable autotrimming by setting `autotrim: false` and you can manually enqueue the job later, `SolidCable::PruneJob.perform_later`, or run it on a recurring interval out of band.
7277

7378
## License
7479

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

33
module SolidCable
4-
class PruneJob < ActiveJob::Base
4+
class TrimJob < ActiveJob::Base
55
def perform
6-
::SolidCable::Message.prunable.delete_all
6+
::SolidCable::Message.trimmable.delete_all
77
end
88
end
99
end

app/models/solid_cable/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SolidCable
44
class Message < SolidCable::Record
5-
scope :prunable, lambda {
5+
scope :trimmable, lambda {
66
where(created_at: ..::SolidCable.message_retention.ago)
77
}
88
scope :broadcastable, lambda { |channels, last_id|

lib/action_cable/subscription_adapter/solid_cable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def add_channel(channel, on_success)
6464

6565
def remove_channel(channel)
6666
channels.delete(channel)
67+
68+
::SolidCable::TrimJob.perform_now if ::SolidCable.autotrim?
6769
end
6870

6971
def invoke_callback(*)

lib/solid_cable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def message_retention
2222
parse_duration(cable_config.message_retention, default: 1.day)
2323
end
2424

25+
def autotrim?
26+
cable_config.autotrim != false
27+
end
28+
2529
private
2630

2731
def cable_config

test/config_helpers.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module ConfigHelpers
4+
extend ActiveSupport::Concern
5+
6+
class ConfigStub
7+
def initialize(**)
8+
@config = ActiveSupport::OrderedOptions.new.
9+
update({ adapter: :test }.merge(**))
10+
end
11+
12+
def config_for(_file)
13+
@config
14+
end
15+
end
16+
17+
def with_cable_config(**)
18+
Rails.stub(:application, ConfigStub.new(**)) { yield }
19+
end
20+
end

test/lib/action_cable/subscription_adapter/solid_cable_test.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
require "active_support/core_ext/hash/indifferent_access"
77
require "pathname"
8+
require "config_helpers"
89

910
class ActionCable::SubscriptionAdapter::SolidCableTest < ActionCable::TestCase
11+
include ConfigHelpers
1012

1113
WAIT_WHEN_EXPECTING_EVENT = 1
1214
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.2
@@ -28,7 +30,8 @@ def setup
2830
end
2931

3032
def cable_config
31-
{ adapter: "solid_cable", polling_interval: "0.01.seconds" }
33+
{ adapter: "solid_cable", message_retention: "1.second",
34+
polling_interval: "0.01.seconds" }
3235
end
3336

3437
def teardown
@@ -82,6 +85,24 @@ def test_broadcast_after_unsubscribe
8285
assert_empty keep_queue
8386
end
8487

88+
def test_trims_after_unsubscribe
89+
with_cable_config message_retention: "1.second" do
90+
keep_queue = nil
91+
subscribe_as_queue("channel") do |queue|
92+
keep_queue = queue
93+
94+
@tx_adapter.broadcast("channel", "hello world")
95+
96+
assert_equal 1, SolidCable::Message.where(channel: "channel").count
97+
assert_equal "hello world", queue.pop
98+
sleep 2
99+
end
100+
sleep 3
101+
102+
assert_equal 0, SolidCable::Message.where(channel: "channel").count
103+
end
104+
end
105+
85106
def test_multiple_broadcast
86107
subscribe_as_queue("channel") do |queue|
87108
@tx_adapter.broadcast("channel", "bananas")

test/solid_cable_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "config_helpers"
45

56
class SolidCableTest < ActiveSupport::TestCase
7+
include ConfigHelpers
8+
69
test "it has a version number" do
710
assert SolidCable::VERSION
811
end
12+
13+
test "autotrimming when nothing is set" do
14+
assert_not Rails.application.config_for("cable").key?(:autotrim)
15+
assert SolidCable.autotrim?
16+
end
17+
18+
test "autotrimming when set to false" do
19+
with_cable_config autotrim: false do
20+
assert_not SolidCable.autotrim?
21+
end
22+
end
23+
24+
test "autotrimming when set to true" do
25+
with_cable_config autotrim: true do
26+
assert SolidCable.autotrim?
27+
end
28+
end
929
end

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"../test/dummy/db/migrate", __dir__
99
)]
1010
require "rails/test_help"
11+
require "minitest/autorun"
1112

1213
# Load fixtures from the engine
1314
if ActiveSupport::TestCase.respond_to?(:fixture_paths=)

0 commit comments

Comments
 (0)