good_job 3.27.4 → 3.28.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -0
  3. data/README.md +91 -2
  4. data/app/frontend/good_job/style.css +4 -0
  5. data/app/models/concerns/good_job/advisory_lockable.rb +7 -2
  6. data/app/models/concerns/good_job/filterable.rb +3 -2
  7. data/app/models/good_job/base_record.rb +4 -0
  8. data/app/models/good_job/batch_record.rb +8 -2
  9. data/app/models/good_job/discrete_execution.rb +11 -0
  10. data/app/models/good_job/execution.rb +6 -4
  11. data/app/models/good_job/job.rb +4 -4
  12. data/app/views/good_job/batches/_jobs.erb +23 -21
  13. data/app/views/good_job/batches/_table.erb +15 -15
  14. data/app/views/good_job/jobs/_executions.erb +31 -5
  15. data/app/views/good_job/jobs/_table.erb +156 -154
  16. data/app/views/good_job/shared/_filter.erb +9 -9
  17. data/app/views/good_job/shared/_navbar.erb +1 -1
  18. data/app/views/good_job/shared/_secondary_navbar.erb +1 -1
  19. data/app/views/layouts/good_job/application.html.erb +2 -2
  20. data/config/locales/de.yml +2 -0
  21. data/config/locales/en.yml +2 -0
  22. data/config/locales/es.yml +2 -0
  23. data/config/locales/fr.yml +2 -0
  24. data/config/locales/it.yml +2 -0
  25. data/config/locales/ja.yml +2 -0
  26. data/config/locales/ko.yml +2 -0
  27. data/config/locales/nl.yml +3 -1
  28. data/config/locales/pt-BR.yml +2 -0
  29. data/config/locales/ru.yml +2 -0
  30. data/config/locales/tr.yml +2 -0
  31. data/config/locales/uk.yml +2 -0
  32. data/lib/generators/good_job/templates/install/migrations/create_good_jobs.rb.erb +1 -0
  33. data/lib/generators/good_job/templates/update/migrations/10_create_good_job_execution_error_backtrace.rb.erb +15 -0
  34. data/lib/good_job/active_job_extensions/batches.rb +1 -1
  35. data/lib/good_job/active_job_extensions/concurrency.rb +2 -2
  36. data/lib/good_job/active_job_extensions/notify_options.rb +1 -1
  37. data/lib/good_job/capsule.rb +10 -6
  38. data/lib/good_job/version.rb +1 -1
  39. data/lib/good_job.rb +2 -2
  40. metadata +4 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa73a0d9ffc1feb2548c525af767537d79a8b6fdf4e03092c4963d91041eb73a
4
- data.tar.gz: 41365ce2085788ff587d85a96d6bf1568fb5861ea025e98c87990ae65485ffc8
3
+ metadata.gz: 3bbf6aa5c2cdbc7b2ff5b5f7dd31a219140934b799737c192aa8de55e5030bca
4
+ data.tar.gz: 1f7c614722781287cf6482af08fe97d4af5305fdfbd7583ce343277b7b8fe5a1
5
5
  SHA512:
6
- metadata.gz: a300b98ed92353715dff14d18940f0e9fa2896e1cda56428c026e62b162e3f7a2f5a07eba2a9501af2d0c1f4fa51f3c225a4ecc103f73e8eac862ec343902e6e
7
- data.tar.gz: 25241155b1d5e375a903cbeec98e88ecaaca2fc46b1134c92ed400fbf2b024a637744db072fe42fb9af486e7ce43cec5262c6c31adceb2f948af3aacb7cc9584
6
+ metadata.gz: 3f97f043eed52700808203c42ab9a714a7fdefdd33f3a201045a9a1649e675de6305386439f4707788a89842993902187c21b89939e4458e6a1750d25d6972d1
7
+ data.tar.gz: a3591c147f153964a0d929d8b49c43386b9f03bf53414471eed55d5312d9bb72e0fbd0ee49b966519570b4a13f33d4fa5ddda91c47d6fa31342af928cfa8185a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,57 @@
1
1
  # Changelog
2
2
 
3
+ ## [v3.28.1](https://github.com/bensheldon/good_job/tree/v3.28.1) (2024-04-24)
4
+
5
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v3.28.0...v3.28.1)
6
+
7
+ **Fixed bugs:**
8
+
9
+ - Ensure job context does not leak into extensions when`perform_now` is called within another job [\#1336](https://github.com/bensheldon/good_job/pull/1336) ([bensheldon](https://github.com/bensheldon))
10
+
11
+ **Closed issues:**
12
+
13
+ - Executing perform\_now on a good\_job with GoodJobs::ActiveJobExtensions::Concurrency can run twice [\#1335](https://github.com/bensheldon/good_job/issues/1335)
14
+ - `erb_lint` doesn't lint all files [\#1329](https://github.com/bensheldon/good_job/issues/1329)
15
+ - Server hangs after \#1297 [\#1301](https://github.com/bensheldon/good_job/issues/1301)
16
+ - Figure out how to use bind parameters for time queries [\#1285](https://github.com/bensheldon/good_job/issues/1285)
17
+
18
+ **Merged pull requests:**
19
+
20
+ - Upgrade development / demo Ruby to v3.3.1 [\#1333](https://github.com/bensheldon/good_job/pull/1333) ([bensheldon](https://github.com/bensheldon))
21
+ - Exploratory testing for presence of scheduled\_at when enqueuing [\#1332](https://github.com/bensheldon/good_job/pull/1332) ([bensheldon](https://github.com/bensheldon))
22
+ - Run `erb_lint` against all the views and partials [\#1331](https://github.com/bensheldon/good_job/pull/1331) ([bensheldon](https://github.com/bensheldon))
23
+
24
+ ## [v3.28.0](https://github.com/bensheldon/good_job/tree/v3.28.0) (2024-04-19)
25
+
26
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v3.27.4...v3.28.0)
27
+
28
+ **Implemented enhancements:**
29
+
30
+ - Store and optionally display the full execution backtrace [\#1328](https://github.com/bensheldon/good_job/pull/1328) ([Earlopain](https://github.com/Earlopain))
31
+ - Store error backtraces on discrete executions [\#1325](https://github.com/bensheldon/good_job/pull/1325) ([Intrepidd](https://github.com/Intrepidd))
32
+
33
+ **Fixed bugs:**
34
+
35
+ - add missing dropdown-item class [\#1327](https://github.com/bensheldon/good_job/pull/1327) ([patriciomacadden](https://github.com/patriciomacadden))
36
+
37
+ **Closed issues:**
38
+
39
+ - Proposal: Migrating Documentation to a Separate Website [\#1324](https://github.com/bensheldon/good_job/issues/1324)
40
+ - Potential documentation error in GoodJob::ActiveJobExtensions::NotifyOptions [\#1321](https://github.com/bensheldon/good_job/issues/1321)
41
+ - ActiveSupport::CurrentAttributes reset after `perform_later` [\#1320](https://github.com/bensheldon/good_job/issues/1320)
42
+ - Storing backtrace in database? [\#1162](https://github.com/bensheldon/good_job/issues/1162)
43
+ - Potential locking race condition when using cron scheduler across multiple processes [\#731](https://github.com/bensheldon/good_job/issues/731)
44
+
45
+ **Merged pull requests:**
46
+
47
+ - docs: corrected a typo regarding the use of GoodJob::ActiveJobExtensions::NotifyOptions [\#1322](https://github.com/bensheldon/good_job/pull/1322) ([pgvsalamander](https://github.com/pgvsalamander))
48
+ - Add "best practices" section to Readme [\#1318](https://github.com/bensheldon/good_job/pull/1318) ([bensheldon](https://github.com/bensheldon))
49
+ - Change ApplicationRecord to ApplicationJob for label documentation. [\#1317](https://github.com/bensheldon/good_job/pull/1317) ([frans-k](https://github.com/frans-k))
50
+ - Run test matrix against Ruby 3.3; remove pry [\#1315](https://github.com/bensheldon/good_job/pull/1315) ([bensheldon](https://github.com/bensheldon))
51
+ - Add `Rails.application.load_server` to Demo `config.ru`; quiet puma web-concurrency warnings [\#1314](https://github.com/bensheldon/good_job/pull/1314) ([bensheldon](https://github.com/bensheldon))
52
+ - Fix test leakage of configuration double [\#1312](https://github.com/bensheldon/good_job/pull/1312) ([bensheldon](https://github.com/bensheldon))
53
+ - Rewrite queries to all use bind parameters and prepare: true [\#1308](https://github.com/bensheldon/good_job/pull/1308) ([bensheldon](https://github.com/bensheldon))
54
+
3
55
  ## [v3.27.4](https://github.com/bensheldon/good_job/tree/v3.27.4) (2024-04-04)
4
56
 
5
57
  [Full Changelog](https://github.com/bensheldon/good_job/compare/v3.27.3...v3.27.4)
data/README.md CHANGED
@@ -72,6 +72,11 @@ For more of the story of GoodJob, read the [introductory blog post](https://isla
72
72
  - [Write tests](#write-tests)
73
73
  - [PgBouncer compatibility](#pgbouncer-compatibility)
74
74
  - [CLI HTTP health check probes](#cli-http-health-check-probes)
75
+ - [Doing your best job with GoodJob](#doing-your-best-job-with-goodjob)
76
+ - [Sizing jobs: mice and elephants](#sizing-jobs-mice-and-elephants)
77
+ - [Isolating by total latency](#isolating-by-total-latency)
78
+ - [Configuring your queues](#configuring-your-queues)
79
+ - [Additional observations](#additional-observations)
75
80
  - [Contribute](#contribute)
76
81
  - [Gem development](#gem-development)
77
82
  - [Development setup](#development-setup)
@@ -500,12 +505,12 @@ Higher priority numbers run first in all versions of GoodJob v3.x and below. Goo
500
505
  Labels are the recommended way to add context or metadata to specific jobs. For example, all jobs that have a dependency on an email service could be labeled `email`. Using labels requires adding the Active Job extension `GoodJob::ActiveJobExtensions::Labels` to your job class.
501
506
 
502
507
  ```ruby
503
- class ApplicationRecord < ActiveJob::Base
508
+ class ApplicationJob < ActiveJob::Base
504
509
  include GoodJob::ActiveJobExtensions::Labels
505
510
  end
506
511
 
507
512
  # Add a default label to every job within the class
508
- class WelcomeJob < ApplicationRecord
513
+ class WelcomeJob < ApplicationJob
509
514
  self.good_job_labels = ["email"]
510
515
 
511
516
  def perform
@@ -1527,6 +1532,90 @@ gem 'webrick'
1527
1532
 
1528
1533
  If WEBrick is configured to be used, but the dependency is not found, GoodJob will log a warning and fallback to the default probe server.
1529
1534
 
1535
+ ## Doing your best job with GoodJob
1536
+
1537
+ _This section explains how to use GoodJob the most efficiently and performantly, according to its maintainers. GoodJob is very flexible and you don’t necessarily have to use it this way, but the concepts explained here are part of GoodJob’s design intent._
1538
+
1539
+ Background jobs are hard. There are two extremes:
1540
+
1541
+ - **Throw resources (compute, servers, money) at it** by creating dedicated processes (or servers) for each type of job or queue and scaling them independently to achieve the lowest latency and highest throughput.
1542
+ - **Do the best you can in a small budget** by creating dedicated _thread pools_ within a process for each type of job or queue to produce quality-of-service and compromise maximum latency (or tail latency) because of shared resources and thread contention. You can even run them in the web process if you’re really cheap.
1543
+
1544
+ This section will largely focused on optimizing within the latter small-budget scenario, but the concepts and explanation should help you optimize the big-budget scenario too.
1545
+
1546
+ Let’s start with anti-patterns, and then the rest of this section will explain an alternative:
1547
+
1548
+ - **Don’t use functional names for your queues** like `mailers` or `sms` or `turbo` or `batch`. Instead name them after the total latency target (the total duration within queue and executing till finish) you expect for that job e.g.`latency_30s` or `latency_5m` or `literally_whenever`.
1549
+ - **Priority can’t fix a lack of capacity.** Priority rules (i.e. weighing or ordering which jobs or queues execute first) only works when there is capacity available to execute that _next_ job. When all capacity is in-use, priority cannot preempt a job that is already executing ("head-of-line blocking").
1550
+
1551
+ The following will explain methods to create homogenous workloads (based on latency) and increase execution capacity when queuing latency causes the jobs to exceed their total latency target.
1552
+
1553
+ ### Sizing jobs: mice and elephants
1554
+
1555
+ Queuing theory will refer to fast/small/low-latency tasks as **Mice** (e.g. a password reset email, an MFA token via SMS) and slow/big/high-latency tasks as **Elephants** (e.g. sending an email newsletter to 10k recipients, a batched update that touches every record in the database).
1556
+
1557
+ Explicitly group your jobs by their latency: how quickly you expect them to finish to achieve your expected quality of service. This should be their **total latency** (or duration) which is the sum of: **queuing latency** which is how long the job waits in queue until execution capacity becomes available (which ideally should be zero, because you have idle capacity and can start executing a job immediately as soon as it is enqueued or upon its scheduled time) and **execution latency** which is how long the job’s execution takes (e.g. the email being sent). Example: I expect this Password Reset Email Job to have a total latency of 30 seconds or less.
1558
+
1559
+ In a working application, you likely will have more gradations than just small and big or slow and fast (analogously: badgers, wildebeests; maybe even tardigrades or blue whales for tiny and huge, respectively), but there will regardless be a relatively small and countable number of discrete latency buckets to organize your jobs into.
1560
+
1561
+ ### Isolating by total latency
1562
+
1563
+ The most efficient workloads are homogenous (similar) workloads. If you know every job to be executed will take about the same amount of time, you can estimate the maximum delay for a new job at the back of the queue and have that drive decisions about capacity. Alternatively, if those jobs are heterogenous (mixed) it’s possible that a very slow/long-duration job could hold everything back for much longer than anticipated and it’s sorta random. That’s bad!
1564
+
1565
+ A fun visual image here for a single-file queue is a doorway: If you only have 1 doorway, it must be big enough to fit an elephant. But if an elephant is going through the door (and it will go through slowly!) no mice can fit through the door until the elephant is fully clear. Your mice will be delayed!
1566
+
1567
+ Priority will not help when an elephant is in the doorway. Yes, you could say mice have a higher priority than elephants and always allow any mouse to go _before_ any elephant in queue will start. But once an elephant *has started* going through the door, any subsequent mouse who arrives must wait for the elephant to egress regardless of their priority. In Active Job and Ruby, it’s really hard to stop or cancel or preempt a running job (unless you’ve already architected that into your jobs, like with the [`job-iteration`](https://github.com/Shopify/job-iteration) library)
1568
+
1569
+ The best solution is to have a 2nd door, but only sized for mice, so an elephant can’t ever block it. With a mouse-sized doorway _and_ an elephant-sized doorway, mice can still go through the big elephant door when an elephant isn’t using it. Each door has a _maximum_ size (or “latency”) we want it to accept, and smaller is ok, just not larger.
1570
+
1571
+ ### Configuring your queues
1572
+
1573
+ If we wanted to capture the previous 2-door scenario in GoodJob, we’d configure the queues like this;
1574
+
1575
+ ```ruby
1576
+ config.good_job.queues = "mice:1; elephant,mice:1"
1577
+ ```
1578
+
1579
+ This configuration creates two isolated thread pools (separated by a semicolon) each with 1 thread each (the number after the colon). The 2nd thread pool recognizes that both elephants and mice can use that isolated thread pool; if there is an influx of mice, it's possible to use the elephant’s thread pool if an elephant isn't already in progress.
1580
+
1581
+ So what if we add an intermediately-sized `badgers` ? In that case, we can make 3 distinct queues:
1582
+
1583
+ ```ruby
1584
+ config.good_job.queues = "mice:1; badgers,mice:1; elephants,badgers,mice:1"
1585
+ ```
1586
+
1587
+ In this case, we make a mouse sized queue, a badger sized queue, and an elephant sized queue. We can simplify this even further:
1588
+
1589
+ ```ruby
1590
+ config.good_job.queues = "mice:1; badgers,mice:1; *:1"
1591
+ ```
1592
+
1593
+ Using the wildcard `*` for any queue also helps ensure that if a job is enqueued to a newly declared queue (maybe via a dependency or just inadvertently) it will still get executed until you notice and decide on its appropriate latency target.
1594
+
1595
+ In these examples, the order doesn’t matter; it just is maybe more readable to go from the lowest-latency to largest-latency pool (the semicolon groups), and then within a pool to list the largest allowable latency first (the commas). Nothing here is about “job priority” or “queue priority”, this is wholly about grouping.
1596
+
1597
+ In your application, not the zoo, you’ll want to enqueue your `PaswordResetJob` on the `mice` queue, your `CreateComplicatedObjectJob` on the `badger` queue, and your `AuditEveryAccountEverJob` on the `elephant` queue. But you want to name your queues by latency, so that ends up being:
1598
+
1599
+ ```ruby
1600
+ config.good_job.queues = "latency_30s:1; latency_2m,latency_30s:1; *:1"
1601
+ ```
1602
+
1603
+ And you likely want to have more than one thread (though more than 3-5 threads per process will cause thread contention and slow everything down a bit):
1604
+
1605
+ ```ruby
1606
+ config.good_job.queues = "latency_30s:2; latency_2m,latency_30s:2; *:2"
1607
+ ```
1608
+
1609
+ ### Additional observations
1610
+
1611
+ - Unlike GoodJob, other Active Job backends may treat a "queue" and an "isolated execution pool" as one and the same. GoodJob allows composing multiple Active Job queues into the same pool for flexibility and to make it easier to migrate from functionally-named queues to latency-based ones.
1612
+ - You don't *have* to name your queues explicitly like `latency_30s` but it makes it easier to identify outliers and communicate your operational targets. Many people push back on this; that's ok. An option to capture functional details is to use GoodJob's Labels feature instead of encoding them in the queue name.
1613
+ - The downside of organizing your jobs like this is that you may have jobs with the same latency target but wildly different operational parameters, like being coupled to another system that has limited throughput or questionable reliability. GoodJob offers Concurrency and Throttling Controls, but isolation is always the most performant and reliable option, though it requires dedicated resources and costs more.
1614
+ - Observe, monitor, and adapt your job queues over time. You likely have incomplete information about the execution latency of your jobs inclusive of all dependencies across all scenarios. You should expect to adjust your queues and grouping over time as you observe their behavior.
1615
+ - If you find you have unreliable external dependencies that introduce latency, you may also want to further isolate your jobs based on those dependencies, for example, isolating `latency_10s_email_service` to its own execution pool.
1616
+ - Scale on queue latency. Per the previous point in which you do not have complete control over execution latency, you do have control over the queue latency. If queue latency is causing your jobs to miss their total latency target, you must add more capacity (e.g. processes or servers.
1617
+ - This is all largely about latency-based queue design. It’s possible to go further and organize by latency _and_ parallelism. For that I recommend Nate Berkopec’s [*Complete Guide to Rails Performance*](https://www.railsspeed.com/) which covers things like Amdahl’s Law.
1618
+
1530
1619
  ## Contribute
1531
1620
 
1532
1621
  <!-- Please keep this section in sync with CONTRIBUTING.md -->
@@ -42,3 +42,7 @@
42
42
  .min-w-auto {
43
43
  min-width: auto;
44
44
  }
45
+
46
+ .w-fit-content {
47
+ width: fit-content
48
+ }
@@ -46,14 +46,19 @@ module GoodJob
46
46
  cte_query = cte_query.limit(select_limit) if select_limit
47
47
  cte_type = supports_cte_materialization_specifiers? ? :MATERIALIZED : :""
48
48
  composed_cte = Arel::Nodes::As.new(cte_table, Arel::Nodes::UnaryOperation.new(cte_type, cte_query.arel))
49
+
50
+ lock_condition = "#{function}(('x' || substr(md5(#{connection.quote(table_name)} || '-' || #{connection.quote_table_name(cte_table.name)}.#{connection.quote_column_name(column)}::text), 1, 16))::bit(64)::bigint)"
49
51
  query = cte_table.project(cte_table[:id])
50
52
  .with(composed_cte)
51
- .where(Arel.sql("#{function}(('x' || substr(md5(#{connection.quote(table_name)} || '-' || #{connection.quote_table_name(cte_table.name)}.#{connection.quote_column_name(column)}::text), 1, 16))::bit(64)::bigint)"))
53
+ .where(defined?(Arel::Nodes::BoundSqlLiteral) ? Arel::Nodes::BoundSqlLiteral.new(lock_condition, [], {}) : Arel::Nodes::SqlLiteral.new(lock_condition))
52
54
 
53
55
  limit = original_query.arel.ast.limit
54
56
  query.limit = limit.value if limit.present?
55
57
 
56
- unscoped.where(arel_table[primary_key].in(query)).merge(original_query.only(:order))
58
+ # Arel.sql and the IN clause prevent this from being preparable
59
+ # That's why this is manually composed of BoundSqlLiteral's and an InfixOperation
60
+ # to sidestep anywhere in Arel where the `collector.preparable = false` is set
61
+ unscoped.where(Arel::Nodes::InfixOperation.new("IN", arel_table[primary_key], query)).merge(original_query.only(:order))
57
62
  end)
58
63
 
59
64
  # Joins the current query with Postgres's +pg_locks+ table (it provides
@@ -17,9 +17,9 @@ module GoodJob
17
17
  scope :display_all, (lambda do |after_scheduled_at: nil, after_id: nil|
18
18
  query = order(Arel.sql('COALESCE(scheduled_at, created_at) DESC, id DESC'))
19
19
  if after_scheduled_at.present? && after_id.present?
20
- query = query.where(Arel.sql('(COALESCE(scheduled_at, created_at), id) < (:after_scheduled_at, :after_id)'), after_scheduled_at: after_scheduled_at, after_id: after_id)
20
+ query = query.where Arel::Nodes::Grouping.new([coalesce_scheduled_at_created_at, arel_table["id"]]).lt(Arel::Nodes::Grouping.new([bind_value('coalesce', after_scheduled_at, ActiveRecord::Type::DateTime), bind_value('id', after_id, ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid)]))
21
21
  elsif after_scheduled_at.present?
22
- query = query.where(Arel.sql('(COALESCE(scheduled_at, created_at)) < (:after_scheduled_at)'), after_scheduled_at: after_scheduled_at)
22
+ query = query.where coalesce_scheduled_at_created_at.lt(bind_value('coalesce', after_scheduled_at, ActiveRecord::Type::DateTime))
23
23
  end
24
24
  query
25
25
  end)
@@ -34,6 +34,7 @@ module GoodJob
34
34
  query = query.to_s.strip
35
35
  next if query.blank?
36
36
 
37
+ # TODO: turn this into proper bind parameters in Arel
37
38
  tsvector = "(to_tsvector('english', id::text) || to_tsvector('english', COALESCE(active_job_id::text, '')) || to_tsvector('english', serialized_params) || to_tsvector('english', COALESCE(error, ''))#{" || to_tsvector('english', COALESCE(array_to_string(labels, ' '), ''))" if labels_migrated?})"
38
39
  to_tsquery_function = database_supports_websearch_to_tsquery? ? 'websearch_to_tsquery' : 'plainto_tsquery'
39
40
  where("#{tsvector} @@ #{to_tsquery_function}(?)", query)
@@ -40,6 +40,10 @@ module GoodJob
40
40
  end
41
41
  end
42
42
 
43
+ def self.bind_value(name, value, type_class)
44
+ Arel::Nodes::BindParam.new(ActiveRecord::Relation::QueryAttribute.new(name, value, type_class.new))
45
+ end
46
+
43
47
  ActiveSupport.run_load_hooks(:good_job_base_record, self)
44
48
  end
45
49
  end
@@ -18,6 +18,8 @@ module GoodJob
18
18
  scope :not_discarded, -> { where(discarded_at: nil) }
19
19
  scope :succeeded, -> { finished.not_discarded }
20
20
 
21
+ scope :finished_before, ->(timestamp) { where(arel_table['finished_at'].lteq(bind_value('finished_at', timestamp, ActiveRecord::Type::DateTime))) }
22
+
21
23
  alias_attribute :enqueued?, :enqueued_at
22
24
  alias_attribute :discarded?, :discarded_at
23
25
  alias_attribute :finished?, :finished_at
@@ -25,9 +27,13 @@ module GoodJob
25
27
  scope :display_all, (lambda do |after_created_at: nil, after_id: nil|
26
28
  query = order(created_at: :desc, id: :desc)
27
29
  if after_created_at.present? && after_id.present?
28
- query = query.where(Arel.sql('(created_at, id) < (:after_created_at, :after_id)'), after_created_at: after_created_at, after_id: after_id)
30
+ query = if Gem::Version.new(Rails.version) < Gem::Version.new('7.0.0.a') || Concurrent.on_jruby?
31
+ query.where(Arel.sql('(created_at, id) < (:after_created_at, :after_id)'), after_created_at: after_created_at, after_id: after_id)
32
+ else
33
+ query.where Arel::Nodes::Grouping.new([arel_table["created_at"], arel_table["id"]]).lt(Arel::Nodes::Grouping.new([bind_value('created_at', after_created_at, ActiveRecord::Type::DateTime), bind_value('id', after_id, ActiveRecord::Type::String)]))
34
+ end
29
35
  elsif after_created_at.present?
30
- query = query.where(Arel.sql('(after_created_at) < (:after_created_at)'), after_created_at: after_created_at)
36
+ query = query.where arel_table["created_at"].lt(bind_value('created_at', after_created_at, ActiveRecord::Type::DateTime))
31
37
  end
32
38
  query
33
39
  end)
@@ -21,6 +21,13 @@ module GoodJob # :nodoc:
21
21
  false
22
22
  end
23
23
 
24
+ def self.backtrace_migrated?
25
+ return true if columns_hash["error_backtrace"].present?
26
+
27
+ migration_pending_warning!
28
+ false
29
+ end
30
+
24
31
  def number
25
32
  serialized_params.fetch('executions', 0) + 1
26
33
  end
@@ -58,5 +65,9 @@ module GoodJob # :nodoc:
58
65
  _good_job_execution: attributes.except('serialized_params'),
59
66
  })
60
67
  end
68
+
69
+ def filtered_error_backtrace
70
+ Rails.backtrace_cleaner.clean(error_backtrace || [])
71
+ end
61
72
  end
62
73
  end
@@ -95,7 +95,7 @@ module GoodJob
95
95
  # @!method only_scheduled
96
96
  # @!scope class
97
97
  # @return [ActiveRecord::Relation]
98
- scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(Arel::Nodes::BindParam.new(ActiveModel::Attribute.with_cast_value("scheduled_at", Time.current, ActiveModel::Type::DateTime.new)))).or(where(scheduled_at: nil)) }
98
+ scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(bind_value('scheduled_at', DateTime.current, ActiveRecord::Type::DateTime))).or(where(scheduled_at: nil)) }
99
99
 
100
100
  # Order executions by priority (highest priority first).
101
101
  # @!method priority_ordered
@@ -161,7 +161,7 @@ module GoodJob
161
161
  # @param timestamp (Float)
162
162
  # Get jobs that finished before this time (in epoch time).
163
163
  # @return [ActiveRecord::Relation]
164
- scope :finished, ->(timestamp = nil) { timestamp ? where(arel_table['finished_at'].lteq(timestamp)) : where.not(finished_at: nil) }
164
+ scope :finished, ->(timestamp = nil) { timestamp ? where(arel_table['finished_at'].lteq(bind_value('finished_at', timestamp, ActiveRecord::Type::DateTime))) : where.not(finished_at: nil) }
165
165
 
166
166
  # Get Jobs that started but not finished yet.
167
167
  # @!method running
@@ -290,11 +290,12 @@ module GoodJob
290
290
  query = advisory_unlocked.unfinished.schedule_ordered
291
291
 
292
292
  after ||= Time.current
293
- after_query = query.where('scheduled_at > ?', after).or query.where(scheduled_at: nil).where('created_at > ?', after)
293
+ after_bind = bind_value('scheduled_at', after, ActiveRecord::Type::DateTime)
294
+ after_query = query.where(arel_table['scheduled_at'].gt(after_bind)).or query.where(scheduled_at: nil).where(arel_table['created_at'].gt(after_bind))
294
295
  after_at = after_query.limit(limit).pluck(:scheduled_at, :created_at).map { |timestamps| timestamps.compact.first }
295
296
 
296
297
  if now_limit&.positive?
297
- now_query = query.where('scheduled_at < ?', Time.current).or query.where(scheduled_at: nil)
298
+ now_query = query.where(arel_table['scheduled_at'].lt(bind_value('scheduled_at', Time.current, ActiveRecord::Type::DateTime))).or query.where(scheduled_at: nil)
298
299
  now_at = now_query.limit(now_limit).pluck(:scheduled_at, :created_at).map { |timestamps| timestamps.compact.first }
299
300
  end
300
301
 
@@ -460,6 +461,7 @@ module GoodJob
460
461
  if discrete_execution
461
462
  discrete_execution.error = error_string
462
463
  discrete_execution.error_event = result.error_event
464
+ discrete_execution.error_backtrace = job_error.backtrace if discrete_execution.class.backtrace_migrated?
463
465
  end
464
466
  else
465
467
  job_attributes[:error] = nil
@@ -44,14 +44,14 @@ module GoodJob
44
44
  # @!scope class
45
45
  # @param timestamp (DateTime, Time)
46
46
  # @return [ActiveRecord::Relation]
47
- scope :finished_before, ->(timestamp) { where(arel_table['finished_at'].lteq(timestamp)) }
47
+ scope :finished_before, ->(timestamp) { where(arel_table['finished_at'].lteq(bind_value('finished_at', timestamp, ActiveRecord::Type::DateTime))) }
48
48
 
49
49
  # First execution will run in the future
50
- scope :scheduled, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.gt(DateTime.current)).where(params_execution_count.lt(2)) }
50
+ scope :scheduled, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.gt(bind_value('coalesce', Time.current, ActiveRecord::Type::DateTime))).where(params_execution_count.lt(2)) }
51
51
  # Execution errored, will run in the future
52
- scope :retried, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.gt(DateTime.current)).where(params_execution_count.gt(1)) }
52
+ scope :retried, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.gt(bind_value('coalesce', Time.current, ActiveRecord::Type::DateTime))).where(params_execution_count.gt(1)) }
53
53
  # Immediate/Scheduled time to run has passed, waiting for an available thread run
54
- scope :queued, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.lteq(DateTime.current)).joins_advisory_locks.where(pg_locks: { locktype: nil }) }
54
+ scope :queued, -> { where(finished_at: nil).where(coalesce_scheduled_at_created_at.lteq(bind_value('coalesce', Time.current, ActiveRecord::Type::DateTime))).joins_advisory_locks.where(pg_locks: { locktype: nil }) }
55
55
  # Advisory locked and executing
56
56
  scope :running, -> { where(finished_at: nil).joins_advisory_locks.where.not(pg_locks: { locktype: nil }) }
57
57
  # Finished executing (succeeded or discarded)
@@ -2,14 +2,14 @@
2
2
  <div class="list-group list-group-flush text-nowrap table-jobs" role="table">
3
3
  <header class="list-group-item bg-body-tertiary">
4
4
  <div class="row small text-muted text-uppercase align-items-center">
5
- <div class="col-4"><%=t "good_job.models.batch.jobs" %></div>
6
- <div class="d-none d-lg-block col-lg-1 text-lg-center"><%=t "good_job.models.job.queue" %></div>
7
- <div class="d-none d-lg-block col-lg-1 text-lg-end"><%=t "good_job.models.job.priority" %></div>
8
- <div class="d-none d-lg-block col-lg-1 text-lg-end"><%=t "good_job.models.job.attempts" %></div>
5
+ <div class="col-4"><%= t("good_job.models.batch.jobs") %></div>
6
+ <div class="d-none d-lg-block col-lg-1 text-lg-center"><%= t "good_job.models.job.queue" %></div>
7
+ <div class="d-none d-lg-block col-lg-1 text-lg-end"><%= t "good_job.models.job.priority" %></div>
8
+ <div class="d-none d-lg-block col-lg-1 text-lg-end"><%= t "good_job.models.job.attempts" %></div>
9
9
  <div class="col text-end">
10
10
  <%= tag.button type: "button", class: "btn btn-sm text-muted", role: "button",
11
11
  data: { bs_toggle: "collapse", bs_target: ".job-params" },
12
- aria: { expanded: false, controls: jobs.map { |job| "##{dom_id(job, "params")}" }.join(" ") } do %>
12
+ aria: { expanded: false, controls: jobs.map { |job| "##{dom_id(job, 'params')}" }.join(" ") } do %>
13
13
  <%= render_icon "info" %>
14
14
  <span class="visually-hidden">Inspect</span>
15
15
  <% end %>
@@ -26,22 +26,24 @@
26
26
  <%= tag.h5 tag.code(link_to(job.display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %>
27
27
  </div>
28
28
  <div class="col-4 col-lg-1 text-lg-center">
29
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.job.queue" %></div>
29
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.queue" %></div>
30
30
  <span class="badge bg-primary text-dark font-monospace"><%= job.queue_name %></span>
31
31
  </div>
32
32
  <div class="col-4 col-lg-1 text-lg-end">
33
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.job.priority" %>Priority</div>
33
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.priority" %>Priority</div>
34
34
  <span class="font-monospace fw-bold"><%= job.priority %></span>
35
35
  </div>
36
36
  <div class="col-4 col-lg-1 text-lg-end">
37
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.job.attempts" %></div>
37
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.attempts" %></div>
38
38
  <% if job.executions_count > 0 && job.status != :finished %>
39
- <%= tag.span job.executions_count, class: "badge rounded-pill bg-danger", data: {
40
- bs_toggle: "popover",
41
- bs_trigger: "hover focus click",
42
- bs_placement: "bottom",
43
- bs_content: job.recent_error
44
- } %>
39
+ <%= tag.span job.executions_count, class: "badge rounded-pill bg-danger",
40
+ data: {
41
+ bs_toggle: "popover",
42
+ bs_trigger: "hover focus click",
43
+ bs_placement: "bottom",
44
+ bs_content: job.recent_error,
45
+ }
46
+ %>
45
47
  <% else %>
46
48
  <span class="badge bg-secondary rounded-pill"><%= job.executions_count %></span>
47
49
  <% end %>
@@ -53,33 +55,33 @@
53
55
  <div class="dropdown float-end">
54
56
  <button class="d-flex align-items-center btn btn-sm" type="button" id="<%= dom_id(job, :actions) %>" data-bs-toggle="dropdown" aria-expanded="false">
55
57
  <%= render_icon :dots %>
56
- <span class="visually-hidden"><%=t ".actions.title" %></span>
58
+ <span class="visually-hidden"><%= t ".actions.title" %></span>
57
59
  </button>
58
60
  <ul class="dropdown-menu shadow" aria-labelledby="<%= dom_id(job, :actions) %>">
59
61
  <li>
60
62
  <% job_reschedulable = job.status.in? [:scheduled, :retried, :queued] %>
61
63
  <%= link_to reschedule_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job_reschedulable}", title: t(".actions.reschedule"), data: { confirm: t(".actions.confirm_reschedule"), disable: true } do %>
62
64
  <%= render_icon "skip_forward" %>
63
- <%=t "good_job.actions.reschedule" %>
65
+ <%= t "good_job.actions.reschedule" %>
64
66
  <% end %>
65
67
  </li>
66
68
  <li>
67
69
  <% job_discardable = job.status.in? [:scheduled, :retried, :queued] %>
68
70
  <%= link_to discard_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job_discardable}", title: t(".actions.discard"), data: { confirm: t(".actions.confirm_discard"), disable: true } do %>
69
71
  <%= render_icon "stop" %>
70
- <%=t "good_job.actions.discard" %>
72
+ <%= t "good_job.actions.discard" %>
71
73
  <% end %>
72
74
  </li>
73
75
  <li>
74
76
  <%= link_to retry_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job.status == :discarded}", title: t(".actions.retry"), data: { confirm: t(".actions.confirm_retry"), disable: true } do %>
75
77
  <%= render_icon "arrow_clockwise" %>
76
- <%=t "good_job.actions.retry" %>
78
+ <%= t "good_job.actions.retry" %>
77
79
  <% end %>
78
80
  </li>
79
81
  <li>
80
82
  <%= link_to job_path(job.id), method: :delete, class: "dropdown-item #{'disabled' unless job.status.in? [:discarded, :finished]}", title: t(".actions.destroy"), data: { confirm: t(".actions.confirm_destroy"), disable: true } do %>
81
83
  <%= render_icon "trash" %>
82
- <%=t "good_job.actions.destroy" %>
84
+ <%= t "good_job.actions.destroy" %>
83
85
  <% end %>
84
86
  </li>
85
87
 
@@ -89,7 +91,7 @@
89
91
  data: { bs_toggle: "collapse" },
90
92
  aria: { expanded: false, controls: dom_id(job, "params") } do %>
91
93
  <%= render_icon "info" %>
92
- <%=t "good_job.actions.inspect" %>
94
+ <%= t "good_job.actions.inspect" %>
93
95
  <% end %>
94
96
  </li>
95
97
  </ul>
@@ -103,7 +105,7 @@
103
105
  <% end %>
104
106
  <% else %>
105
107
  <div class="list-group-item py-4 text-center text-muted">
106
- <%=t ".no_jobs_found" %>
108
+ <%= t ".no_jobs_found" %>
107
109
  </div>
108
110
  <% end %>
109
111
  </div>
@@ -2,18 +2,18 @@
2
2
  <div class="list-group list-group-flush text-nowrap table-batches" role="table">
3
3
  <header class="list-group-item bg-body-tertiary">
4
4
  <div class="row small text-muted text-uppercase align-items-center">
5
- <div class="col-4"><%=t "good_job.models.batch.name" %></div>
6
- <div class="col-lg-1 d-none d-lg-block"><%=t "good_job.models.batch.created" %></div>
7
- <div class="col-lg-1 d-none d-lg-block"><%=t "good_job.models.batch.enqueued" %></div>
8
- <div class="col-lg-1 d-none d-lg-block"><%=t "good_job.models.batch.discarded" %></div>
9
- <div class="col-lg-1 d-none d-lg-block"><%=t "good_job.models.batch.finished" %></div>
10
- <div class="col-lg-1 d-none d-lg-block"><%=t "good_job.models.batch.jobs" %></div>
5
+ <div class="col-4"><%= t "good_job.models.batch.name" %></div>
6
+ <div class="col-lg-1 d-none d-lg-block"><%= t "good_job.models.batch.created" %></div>
7
+ <div class="col-lg-1 d-none d-lg-block"><%= t "good_job.models.batch.enqueued" %></div>
8
+ <div class="col-lg-1 d-none d-lg-block"><%= t "good_job.models.batch.discarded" %></div>
9
+ <div class="col-lg-1 d-none d-lg-block"><%= t "good_job.models.batch.finished" %></div>
10
+ <div class="col-lg-1 d-none d-lg-block"><%= t "good_job.models.batch.jobs" %></div>
11
11
  <div class="col text-end">
12
12
  <%= tag.button type: "button", class: "btn btn-sm text-muted", role: "button",
13
13
  data: { bs_toggle: "collapse", bs_target: ".batch-properties" },
14
- aria: { expanded: false, controls: batches.map { |batch| "##{dom_id(batch, "params")}" }.join(" ") } do %>
14
+ aria: { expanded: false, controls: batches.map { |batch| "##{dom_id(batch, 'params')}" }.join(" ") } do %>
15
15
  <%= render_icon "info" %>
16
- <span class="visually-hidden"><%=t "good_job.actions.inspect" %></span>
16
+ <span class="visually-hidden"><%= t "good_job.actions.inspect" %></span>
17
17
  <% end %>
18
18
  </div>
19
19
  </div>
@@ -33,29 +33,29 @@
33
33
  <% end %>
34
34
  </div>
35
35
  <div class="col-6 col-lg-1 text-wrap">
36
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.batch.created_at" %></div>
36
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.batch.created_at" %></div>
37
37
  <%= relative_time(batch.created_at) %>
38
38
  </div>
39
39
  <div class="col-6 col-lg-1 text-wrap">
40
40
  <% if batch.enqueued_at %>
41
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.batch.enqueued_at" %></div>
41
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.batch.enqueued_at" %></div>
42
42
  <%= relative_time(batch.enqueued_at) %>
43
43
  <% end %>
44
44
  </div>
45
45
  <div class="col-6 col-lg-1 text-wrap">
46
46
  <% if batch.discarded_at %>
47
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.batch.discarded_at" %></div>
47
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.batch.discarded_at" %></div>
48
48
  <%= relative_time(batch.discarded_at) %>
49
49
  <% end %>
50
50
  </div>
51
51
  <div class="col-6 col-lg-1 text-wrap">
52
52
  <% if batch.finished_at %>
53
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.batch.finished_at" %></div>
53
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.batch.finished_at" %></div>
54
54
  <%= relative_time(batch.finished_at) %>
55
55
  <% end %>
56
56
  </div>
57
57
  <div class="col-6 col-lg-1">
58
- <div class="d-lg-none small text-muted mt-1"><%=t "good_job.models.batch.jobs" %></div>
58
+ <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.batch.jobs" %></div>
59
59
  <%= batch.jobs.count %>
60
60
  </div>
61
61
  <div class="col text-end">
@@ -64,7 +64,7 @@
64
64
  data: { bs_toggle: "collapse", bs_target: "##{dom_id(batch, 'properties')}" },
65
65
  aria: { expanded: false, controls: dom_id(batch, "state") } do %>
66
66
  <%= render_icon "info" %>
67
- <span class="visually-hidden"><%=t "good_job.actions.inspect" %></span>
67
+ <span class="visually-hidden"><%= t "good_job.actions.inspect" %></span>
68
68
  <% end %>
69
69
  </div>
70
70
  </div>
@@ -75,7 +75,7 @@
75
75
  <% end %>
76
76
  <% else %>
77
77
  <div class="list-group-item py-4 text-center text-muted">
78
- <%=t ".no_batches_found" %>
78
+ <%= t ".no_batches_found" %>
79
79
  </div>
80
80
  <% end %>
81
81
  </div>