♊️ GemiNews 🗞️ (dev)

Demo 1: Embeddings + Recommendation Demo 2: Bella RAGa Demo 3: NewRetriever Demo 4: Assistant function calling

🗞️Deferring jobs enqueueing to after the transaction commit, queries count in rendering logs and more

🗿Semantically Similar Articles (by :title_embedding)

Deferring jobs enqueueing to after the transaction commit, queries count in rendering logs and more

2024-04-05 - Wojtek (from Ruby on Rails)

Hi, Wojtek here exploring this week’s changes.

Hi, Wojtek here exploring this week’s changes. Rails World 2024 edition website is now live With tickets going on sale in April. Allow to register transaction callbacks outside of a record ActiveRecord::Base.transaction now yields an ActiveRecord::Transaction object, which allows to register callbacks on it. Article.transaction do |transaction| article.update(published: true) transaction.after_commit do PublishNotificationMailer.with(article: article).deliver_later end end Added ActiveRecord::Base.current_transaction which also allows to register callbacks on it. Article.current_transaction.after_commit do PublishNotificationMailer.with(article: article).deliver_later end Add ActiveRecord.after_all_transactions_commit callback. Useful for code that may run either inside or outside a transaction and need to perform works after the state changes have been properly peristed. def publish_article(article) article.update(published: true) ActiveRecord.after_all_transactions_commit do PublishNotificationMailer.with(article: article).deliver_later end end Automatically delay Active Job enqueues to after commit A common mistake with Active Job is to enqueue jobs from inside a transaction, causing them to potentially be picked and ran by another process, before the transaction is committed, which result in various errors. Topic.transaction do topic = Topic.create NewTopicNotificationJob.perform_later(topic) end Now Active Job will automatically defer the enqueuing to after the transaction is committed, and drop the job if the transaction is rolled back. Various queue implementations can choose to disable this behavior, and users can disable it, or force it on a per job basis: class NewTopicNotificationJob < ApplicationJob self.enqueue_after_transaction_commit = :never # or :always or :default end Add queries count to template rendering instrumentation There is often a need to quickly see how many SQL queries the current action produced. For example, to quickly check if N+1 was solved or if the caching is working and so the number of queries reduced etc. This can be done manually by inspecting the logs and counting the number of queries, but for largish actions with tens-hundreds of SQL queries this is not a simple task. # Before Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms | Allocations: 112788) # After Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms (2 queries, 1 cached) | Allocations: 112788) Add the ability to ignore counter cache columns while they are backfilling Starting to use counter caches on existing large tables can be troublesome, because the column values must be backfilled separately of the column addition (to not lock the table for too long) and before the use of :counter_cache (otherwise methods like size/any?, which use counter caches internally, can produce incorrect results). People usually use database triggers or callbacks on child associations while backfilling before introducing a counter cache configuration to the association. Now, to safely backfill the column, while keeping the column updated with child records added/removed, use: class Comment < ApplicationRecord belongs_to :post, counter_cache: { active: false } end While the counter cache is not “active”, the methods like size/any? will not use it, but get the results directly from the database. After the counter cache column is backfilled, simply remove the { active: false } part from the counter cache definition, and it will now be used by the mentioned methods. Retry Actionable Error when running tests Allow Actionable Errors encountered when running tests to be retried. This feature will only be present on interactive terminals. Raise named exception when database reports an invalid version When the MySQL database returns an invalid version string the ActiveRecord::ActiveRecordError error will now be raised. Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone Previously the copy would still share the internal silencers and filters array, causing state to leak. You can view the whole list of changes here. We had 16 contributors to the Rails codebase this past week! Until next time! Subscribe to get these updates mailed to you.

[Technology] 🌎 https://rubyonrails.org/2024/4/5/this-week-in-rails [🧠] [v2] article_embedding_description: {:llm_project_id=>"Unavailable", :llm_dimensions=>nil, :article_size=>9052, :llm_embeddings_model_name=>"textembedding-gecko"}
[🧠] [v1/3] title_embedding_description: {:ricc_notes=>"[embed-v3] Fixed on 9oct24. Only seems incompatible at first glance with embed v1.", :llm_project_id=>"unavailable possibly not using Vertex", :llm_dimensions=>nil, :article_size=>9052, :poly_field=>"title", :llm_embeddings_model_name=>"textembedding-gecko"}
[🧠] [v1/3] summary_embedding_description: {:ricc_notes=>"[embed-v3] Fixed on 9oct24. Only seems incompatible at first glance with embed v1.", :llm_project_id=>"unavailable possibly not using Vertex", :llm_dimensions=>nil, :article_size=>9052, :poly_field=>"summary", :llm_embeddings_model_name=>"textembedding-gecko"}
[🧠] As per bug https://github.com/palladius/gemini-news-crawler/issues/4 we can state this article belongs to titile/summary version: v3 (very few articles updated on 9oct24)

🗿article.to_s

------------------------------
Title: Deferring jobs enqueueing to after the transaction commit, queries count in rendering logs and more
Summary: Hi, Wojtek here exploring this week’s changes.

[content]
Hi, Wojtek here exploring this week’s changes.

Rails World 2024 edition website is now live
With tickets going on sale in April.

Allow to register transaction callbacks outside of a record
ActiveRecord::Base.transaction now yields an ActiveRecord::Transaction object, which allows to register callbacks on it.
Article.transaction do |transaction|
  article.update(published: true)
  transaction.after_commit do
    PublishNotificationMailer.with(article: article).deliver_later
  end
end


Added ActiveRecord::Base.current_transaction which also allows to register callbacks on it.
Article.current_transaction.after_commit do
  PublishNotificationMailer.with(article: article).deliver_later
end


Add ActiveRecord.after_all_transactions_commit callback.

Useful for code that may run either inside or outside a transaction and need to perform works after the state changes have been properly peristed.
def publish_article(article)
  article.update(published: true)
  ActiveRecord.after_all_transactions_commit do
    PublishNotificationMailer.with(article: article).deliver_later
  end
end


Automatically delay Active Job enqueues to after commit
A common mistake with Active Job is to enqueue jobs from inside a transaction, causing them to potentially be picked and ran by another process, before the transaction is committed, which result in various errors.

Topic.transaction do
  topic = Topic.create
  NewTopicNotificationJob.perform_later(topic)
end


Now Active Job will automatically defer the enqueuing to after the transaction is committed, and drop the job if the transaction is rolled back.

Various queue implementations can choose to disable this behavior, and users can disable it, or force it on a per job basis:
class NewTopicNotificationJob < ApplicationJob
  self.enqueue_after_transaction_commit = :never # or :always or :default
end


Add queries count to template rendering instrumentation
There is often a need to quickly see how many SQL queries the current action produced. For example, to quickly check if N+1 was solved or if the caching is working and so the number of queries reduced etc. This can be done manually by inspecting the logs and counting the number of queries, but for largish actions with tens-hundreds of SQL queries this is not a simple task.

# Before
Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms | Allocations: 112788)
# After
Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms (2 queries, 1 cached) | Allocations: 112788)


Add the ability to ignore counter cache columns while they are backfilling
Starting to use counter caches on existing large tables can be troublesome, because the column values must be backfilled separately of the column addition (to not lock the table for too long) and before the use of :counter_cache (otherwise methods like size/any?, which use counter caches internally, can produce incorrect results). People usually use database triggers or callbacks on child associations while backfilling before introducing a counter cache configuration to the association.

Now, to safely backfill the column, while keeping the column updated with child records added/removed, use:
class Comment < ApplicationRecord
  belongs_to :post, counter_cache: { active: false }
end


While the counter cache is not “active”, the methods like size/any? will not use it, but get the results directly from the database. After the counter cache column is backfilled, simply remove the { active: false } part from the counter cache definition, and it will now be used by the mentioned methods.

Retry Actionable Error when running tests
Allow Actionable Errors encountered when running tests to be retried. This feature will only be present on interactive terminals.

Raise named exception when database reports an invalid version
When the MySQL database returns an invalid version string the ActiveRecord::ActiveRecordError error will now be raised.

Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone
 Previously the copy would still share the internal silencers and filters array, causing state to leak.

You can view the whole list of changes here.
We had 16 contributors to the Rails codebase this past week!

Until next time!

Subscribe to get these updates mailed to you.
[/content]

Author: Wojtek
PublishedDate: 2024-04-05
Category: Technology
NewsPaper: Ruby on Rails
Tags: news
{"id"=>7883,
"title"=>"Deferring jobs enqueueing to after the transaction commit, queries count in rendering logs and more",
"summary"=>"Hi, Wojtek here exploring this week’s changes.",
"content"=>"

Hi, Wojtek here exploring this week’s changes.

\n\n

Rails World 2024 edition website is now live
\nWith tickets going on sale in April.

\n\n

Allow to register transaction callbacks outside of a record
\nActiveRecord::Base.transaction now yields an ActiveRecord::Transaction object, which allows to register callbacks on it.

\n
Article.transaction do |transaction|\n  article.update(published: true)\n  transaction.after_commit do\n    PublishNotificationMailer.with(article: article).deliver_later\n  end\nend\n
\n\n

Added ActiveRecord::Base.current_transaction which also allows to register callbacks on it.

\n
Article.current_transaction.after_commit do\n  PublishNotificationMailer.with(article: article).deliver_later\nend\n
\n\n

Add ActiveRecord.after_all_transactions_commit callback.

\n\n

Useful for code that may run either inside or outside a transaction and need to perform works after the state changes have been properly peristed.

\n
def publish_article(article)\n  article.update(published: true)\n  ActiveRecord.after_all_transactions_commit do\n    PublishNotificationMailer.with(article: article).deliver_later\n  end\nend\n
\n\n

Automatically delay Active Job enqueues to after commit
\nA common mistake with Active Job is to enqueue jobs from inside a transaction, causing them to potentially be picked and ran by another process, before the transaction is committed, which result in various errors.

\n\n
Topic.transaction do\n  topic = Topic.create\n  NewTopicNotificationJob.perform_later(topic)\nend\n
\n\n

Now Active Job will automatically defer the enqueuing to after the transaction is committed, and drop the job if the transaction is rolled back.

\n\n

Various queue implementations can choose to disable this behavior, and users can disable it, or force it on a per job basis:

\n
class NewTopicNotificationJob < ApplicationJob\n  self.enqueue_after_transaction_commit = :never # or :always or :default\nend\n
\n\n

Add queries count to template rendering instrumentation
\nThere is often a need to quickly see how many SQL queries the current action produced. For example, to quickly check if N+1 was solved or if the caching is working and so the number of queries reduced etc. This can be done manually by inspecting the logs and counting the number of queries, but for largish actions with tens-hundreds of SQL queries this is not a simple task.

\n\n
# Before\nCompleted 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms | Allocations: 112788)\n# After\nCompleted 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms (2 queries, 1 cached) | Allocations: 112788)\n
\n\n

Add the ability to ignore counter cache columns while they are backfilling
\nStarting to use counter caches on existing large tables can be troublesome, because the column values must be backfilled separately of the column addition (to not lock the table for too long) and before the use of :counter_cache (otherwise methods like size/any?, which use counter caches internally, can produce incorrect results). People usually use database triggers or callbacks on child associations while backfilling before introducing a counter cache configuration to the association.

\n\n

Now, to safely backfill the column, while keeping the column updated with child records added/removed, use:

\n
class Comment < ApplicationRecord\n  belongs_to :post, counter_cache: { active: false }\nend\n
\n\n

While the counter cache is not “active”, the methods like size/any? will not use it, but get the results directly from the database. After the counter cache column is backfilled, simply remove the { active: false } part from the counter cache definition, and it will now be used by the mentioned methods.

\n\n

Retry Actionable Error when running tests
\nAllow Actionable Errors encountered when running tests to be retried. This feature will only be present on interactive terminals.

\n\n

Raise named exception when database reports an invalid version
\nWhen the MySQL database returns an invalid version string the ActiveRecord::ActiveRecordError error will now be raised.

\n\n

Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone
\n Previously the copy would still share the internal silencers and filters array, causing state to leak.

\n\n

You can view the whole list of changes here.\nWe had 16 contributors to the Rails codebase this past week!

\n\n

Until next time!

\n\n

Subscribe to get these updates mailed to you.

",
"author"=>"Wojtek",
"link"=>"https://rubyonrails.org/2024/4/5/this-week-in-rails",
"published_date"=>Fri, 05 Apr 2024 00:00:00.000000000 UTC +00:00,
"image_url"=>nil,
"feed_url"=>"https://rubyonrails.org/2024/4/5/this-week-in-rails",
"language"=>nil,
"active"=>true,
"ricc_source"=>"feedjira::v1",
"created_at"=>Sat, 06 Apr 2024 20:19:58.534779000 UTC +00:00,
"updated_at"=>Mon, 21 Oct 2024 19:29:25.028501000 UTC +00:00,
"newspaper"=>"Ruby on Rails",
"macro_region"=>"Technology"}
Edit this article
Back to articles