thecore_backend_commons 3.15.0 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c25e75a330b000da97adb51aadde4893de944605914b1d307febcba8ba310739
4
- data.tar.gz: 702b620b89c37ecb791bfc4644438aa0057111ddb236ef7cd8100f2947950475
3
+ metadata.gz: 72f6c21d7349acafc03685f38433748768fa7908b1668177f5e595d62fccf754
4
+ data.tar.gz: 3053800cad2e66bd8a7559515a2aab2f521d827358f426a8ad56e919654ab0c2
5
5
  SHA512:
6
- metadata.gz: c16a2c450be82f46f8113ba82d2939c44197f07f60d0bb9dd2d4ae2d9c414b092c7c58c888acae959ec1a6d9c8c82cd18db7f388f7e075d089a64fa7e56bd4f8
7
- data.tar.gz: 83c0155ac3b4b2f85a6258a4f3b31ab2acca3f1c76f64127e05468f12d0cdaec2f0826a9dc6ca7e60af9122b9f3a6f646647453b61a9b90814384b710a20f08d
6
+ metadata.gz: 30d673fd3205f8499e4e84ab6f1e4a3827fb830a478561d958dc5bc3af50f490b1e033476c5a25d4d7f2ad270bbb3ad95177f19a73f2b8d5541f6efbea1ab854
7
+ data.tar.gz: 8a4e4342bb6081235dd4db7019d59451b79a593d4410ed9c1db6eb38cc07dbe9e42130986fc25826c10519e397d4d71c77ee4d65c559209677e5ee8ee5c525b0
data/README.md CHANGED
@@ -14,6 +14,10 @@ Record-change messages (`ActivityLogChannel`, the `messages` room) are **off by
14
14
  `action`) and whether it succeeded, never the record's data: clients read the data through the REST
15
15
  API.
16
16
 
17
+ Action Cable runs on **Solid Cable** in the app's own database (no Redis): the host's
18
+ `config/cable.yml` is written by `bin/rails generate thecore_backend_commons:install` and the table
19
+ comes with `bin/rails db:migrate`. Details: `docs/agents/action-cable.md`.
20
+
17
21
  ---
18
22
 
19
23
  ## Web Push notifications (VAPID)
@@ -1,5 +1,5 @@
1
1
  class PushDispatchJob < ApplicationJob
2
- queue_as :"#{ENV.fetch('COMPOSE_PROJECT_NAME', 'default')}_default"
2
+ queue_as { ThecoreBackgroundJobs.queue_name(:default) }
3
3
 
4
4
  def perform(push_message_id)
5
5
  message = PushMessage.find_by(id: push_message_id)
@@ -13,7 +13,8 @@ module BaseApplicationRecordConcern
13
13
 
14
14
  after_commit :manage_assets
15
15
 
16
- # Broadcast messages via ActionCable for create, update, destroy actions
16
+ # Broadcast messages via ActionCable for create, update, destroy actions. The "valid: false"
17
+ # message waits for the transaction around the save to end (see validation_ko).
17
18
  after_validation :validation_ko
18
19
 
19
20
  after_commit :message_ok
@@ -48,10 +49,20 @@ module BaseApplicationRecordConcern
48
49
  # turns them on, and never carrying the record itself.
49
50
  # Validation runs before any transaction callback knows the action: a new record is a create,
50
51
  # anything else an update (a destroy is never validated).
52
+ # The message leaves after the rollback (#11): with Solid Cable a broadcast is a row in the app's
53
+ # database, and one written inside the save's transaction would be rolled back with the invalid
54
+ # save. It is registered on the transaction that is open now: the save's own one (always rolled
55
+ # back for an invalid record), or the caller's outer transaction, whichever way that one ends.
56
+ # Outside any transaction (a plain valid?) it leaves at once. The outcome is captured now, since
57
+ # the rollback restores the record's state.
51
58
  def validation_ko
52
59
  return unless errors.any?
53
60
 
54
- broadcast_record_change(action: new_record? ? :create : :update, success: false, valid: false, errors: errors.full_messages.uniq)
61
+ outcome = { action: new_record? ? :create : :update, success: false, valid: false, errors: errors.full_messages.uniq }
62
+ announce = -> { broadcast_record_change(**outcome) }
63
+ transaction = self.class.current_transaction
64
+ transaction.after_rollback(&announce)
65
+ transaction.after_commit(&announce)
55
66
  end
56
67
 
57
68
  def message_ok
@@ -0,0 +1,15 @@
1
+ # Solid Cable's table (solid_cable 4.1.0's db/cable_schema.rb), in the app's own database: no second
2
+ # database, no connects_to and no database.yml changes (thecore ADR 0017 stage 2, #11). The
3
+ # production adapter of every host's config/cable.yml (docs/agents/action-cable.md).
4
+ class CreateSolidCableTables < ActiveRecord::Migration[7.1]
5
+ def change
6
+ create_table "solid_cable_messages" do |t|
7
+ t.binary "channel", limit: 1024, null: false
8
+ t.binary "payload", limit: 536_870_912, null: false
9
+ t.datetime "created_at", null: false
10
+ t.integer "channel_hash", limit: 8, null: false
11
+ t.index ["channel_hash"], name: "index_solid_cable_messages_on_channel_hash"
12
+ t.index ["created_at"], name: "index_solid_cable_messages_on_created_at"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require "rails/generators"
2
+
3
+ module ThecoreBackendCommons
4
+ module Generators
5
+ # `bin/rails generate thecore_backend_commons:install`: writes the host's config/cable.yml, Solid
6
+ # Cable in the app's own database in production (#11, thecore ADR 0017 stage 2). The host owns
7
+ # the file, and this is the only content it should have, so a Redis one is replaced.
8
+ class InstallGenerator < Rails::Generators::Base
9
+ source_root File.expand_path("templates", __dir__)
10
+ desc "Writes config/cable.yml: Solid Cable in the app's database in production, async in " \
11
+ "development, test in test."
12
+
13
+ def create_cable_yml
14
+ copy_file "cable.yml", "config/cable.yml", force: true
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ # Action Cable adapters (thecore_backend_commons, thecore ADR 0017 stage 2). Production: Solid Cable,
2
+ # its table (solid_cable_messages, from the gem's migration) in the app's own database, no
3
+ # connects_to: the web process and the job worker broadcast to the same table, every web process
4
+ # polls it. Written by `bin/rails generate thecore_backend_commons:install`.
5
+ development:
6
+ adapter: async
7
+
8
+ test:
9
+ adapter: test
10
+
11
+ production:
12
+ adapter: solid_cable
13
+ polling_interval: 0.1.seconds
14
+ message_retention: 1.day
@@ -1,3 +1,3 @@
1
1
  module ThecoreBackendCommons
2
- VERSION = "3.15.0".freeze
2
+ VERSION = "3.16.0".freeze
3
3
  end
@@ -2,6 +2,7 @@ require "ostruct"
2
2
  require "web-push" # gem for VAPID web push (pushpad/web-push, actively maintained)
3
3
  require "thecore_auth_commons"
4
4
  require "thecore_background_jobs"
5
+ require "solid_cable" # Action Cable production adapter (#11): loads its engine and SolidCable::Message
5
6
  require "rails-i18n"
6
7
  require "devise-i18n"
7
8
  require "http_accept_language"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_backend_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.15.0
4
+ version: 3.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: solid_cable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: ostruct
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -251,7 +265,10 @@ files:
251
265
  - db/migrate/20260625000001_add_sender_user_id_to_push_messages.rb
252
266
  - db/migrate/20260629000001_add_message_type_to_push_messages.rb
253
267
  - db/migrate/20260908000010_add_time_zone_columns_to_push_messages.rb
268
+ - db/migrate/20261205000001_create_solid_cable_tables.rb
254
269
  - db/seeds.rb
270
+ - lib/generators/thecore_backend_commons/install/install_generator.rb
271
+ - lib/generators/thecore_backend_commons/install/templates/cable.yml
255
272
  - lib/tasks/thecore_backend_commons_tasks.rake
256
273
  - lib/thecore_backend_commons.rb
257
274
  - lib/thecore_backend_commons/activity_log.rb