Commit Graph

15 Commits

Author SHA1 Message Date
Jonathan Hefner
682353e3de Add ActiveRecord.deprecator
This commit adds `ActiveRecord.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `activerecord/lib` with
`ActiveRecord.deprecator`.

Additionally, this commit adds `ActiveRecord.deprecator` to
`Rails.application.deprecators` so that it can be configured using e.g.
`config.active_support.report_deprecations`.
2022-10-25 14:23:08 -05:00
Jean Boussier
f56b4187be Fix Active Record :db_runtime metric
In https://github.com/rails/rails/pull/45796 I overlooked that
`ActiveRecord::LogSubscriber#sql` wasn't only logging the SQL query
but was also responsible for collecting the `:db_runtime` metric.

Ultimately I think it is cleaner to move this concern to `RuntimeRegistry`.
2022-09-15 10:43:33 +02:00
Ben Sheldon
8fceabd1e8 Defer constant loading of ActiveRecord::DestroyAssociationAsyncJob via a String instead of a class constant
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-13 11:58:20 -05:00
Jonathan Hefner
9574a39a7a Add db_runtime to Active Job instrumentation
This adds `:db_runtime` to `perform.active_job` notification payloads,
which is the total time taken by database queries while performing a
job.  This value can be used to better understand how a job's time is
spent.

This is similar to the `:db_runtime` value added to
`process_action.action_controller` notification payloads by
`ActiveRecord::Railties::ControllerRuntime`.

Closes #35354.

Co-authored-by: Cory Gwin <gwincr11@github.com>
2022-05-19 14:09:21 -05:00
Nick Holden
c773ae65af Add active_record.destroy_association_async_batch_size configuration
This allows applications to specify the maximum number of records that
will be destroyed in a single background job by the `dependent:
:destroy_async` association option. By default, the current behavior
will remain the same: when a parent record is destroyed, all dependent
records will be destroyed in a single background job. If the number of
dependent records is greater than this configuration, the records will
be destroyed in multiple background jobs.

At GitHub, we have a custom method for destroying associated records
in the background that we'd like to replace with
`dependent: :destroy_async`. Some associations have a large number of
dependent records, and our infrastructure requires that background jobs
complete quickly, so we limit the maximum number of dependent records
destroyed in a single background job and enqueue additional jobs when
the number of records exceeds that limit.
2022-03-09 11:13:16 -07:00
Rafael França
a13b135258
Revert "Run ActiveRecord -> ActiveJob integration tests in CI" 2021-06-15 11:19:04 -04:00
Alex Ghiculescu
3837a5c0c9 Run ActiveRecord -> ActiveJob integration tests in CI 2021-06-15 09:17:41 -05:00
Ashik Salman
8e1b191b37 Updated abbreviation for single table inheritance. 2021-04-16 11:53:05 +05:30
Ryuta Kamizono
043184d903 Fix end alignment 2021-03-21 15:46:36 +09:00
Rafael França
a04b301522
Merge pull request #41093 from gwincr11/action-job-transactions
Make destroy async transactional
2021-02-01 19:11:26 -05:00
Rafael Mendonça França
48a7760823
Refactoring the code to not add methods to all models
Only models that need to use the `:destroy_async` feature
need those methods so it is better to not add to all models.
2021-02-01 23:26:04 +00:00
Cory Gwin
8e4c0ddc34 add new module to handle queueing of jobs to enqueue after commit is successful 2021-01-27 15:33:25 -05:00
Cory Gwin
cfcd904436 Make destroy async transactional
Motivation:
  - A active record rollback could occur while enqueuing a job in this
    case the job would enqueue even though the database deletion
    rolledback putting things in a funky state. So this adds some
    functionality to ensure that deletions are not queued until after
    the db transaction has been commited.

Related Issues:
  - Sort of related to https://github.com/rails/rails/issues/26045

Changes:
  - add some syntatic sugar to transactions to make enqueueing a job a
    little better experience, this really hooks into the after destroy
    hook on the transaction.
  - Setup async destroy on associations to not enqueue until after a
    transaction has been committed.
2021-01-12 09:45:03 -05:00
Muhammad Usman
095878c6e4 Handle STI models for has_many dependent: :destroy_async
Update activerecord/test/activejob/destroy_association_async_test.rb

Co-authored-by: Ryuta Kamizono <kamipo@gmail.com>
2021-01-06 02:04:23 +01:00
George Claghorn
4cf7559280 Destroy associations in a background job.
Sometimes cascading association deletions can cause timeouts due to
an IO issue. Perhaps a model has associations that are destroyed on
deletion which in turn trigger other deletions and this can continue
down a complex tree. Along this tree you may also hit other IO
operations. Such deep deletions can lead to server timeouts while
awaiting completion and really the user may not notice all the
changes on their side immediately making them wait unnecesarially or
worse causing a timeout during the operation.

We now allow associations supporting the `dependent:` key to take `:destroy_async`,
which schedules a background job to destroy associations.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Co-authored-by: Cory Gwin @gwincr11 <gwincr11@github.com>
2020-09-24 14:24:15 -04:00